BG MVC Model View Controller eğitim serisi yayında...

Ana page > Programlama > Windows API Programlama > Tooltip

Tooltip

► Kontroller

Windows API ile bir tooltip kontrolü oluşturmak için CreateWindowEx() fonksiyonunun ikinci parametresinde TOOLTIPS_CLASS değerini kullanmamız gerekir. Bu ifadenin tanımı da commctrl.h başlık dosyasında yer aldığından bu dosya programımıza dahil edilmelidir.

Bir kontrol için tooltip oluşturma

1. Öncelikle Burada gösterildiği gibi bir Windows API projesi oluşturalım. Projeyle birlikte otomatik olarak oluşturulan main.c dosyasının başlangıç kısmını aşağıdaki şekilde düzenleyelim:


#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#define IDC_BUTTON 101

#include <tchar.h>
#include <windows.h>
#include <commctrl.h> /* TOOLTIPS_CLASS için */

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Tooltip oluşturma fonksiyonu bildirimi */
HWND CreateToolTip (int toolID, HWND hDlg, PTSTR pszText);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

/* Buton ve Tooltip pencere oluşturma */
HWND hwndButton, hwndToolTip;
HINSTANCE g_hInst;

int WINAPI WinMain (HINSTANCE hThisInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpszArgument,
                int nCmdShow)

Aşağıdaki satır ile commctrl.h başlık dosyası ile Tooltip kontrolü oluşturma ile ilgili fonksiyonların ve değerlerin kullanımı sağlanır.


#include <commctrl.h>

Aşağıdaki satır ile Tooltip kontrolü oluşturmak için kullanılacak CreateToolTip() fonksiyonunun tanımlaması programa eklenir.


HWND CreateToolTip (int toolID, HWND hDlg, PTSTR pszText);

Aşağıdaki satırlar ile bir adet Buton kontrolü ve bir adet Tooltip kontrolü ile bir adet global HINSTANCE değişkeni oluşturulur.


HWND hwndButton, hwndToolTip;
HINSTANCE g_hInst;

2. Global HINSTANCE değişkeni oluşturma

WinMain() içinde ShowWindow() fonksiyonunun çağrıldığı satırdan önce aşağıdaki satırı ekleyerek g_hInst değişkenine program hThisInstance değişken değerini atayalım.


g_hInst = hThisInstance;

/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);

3. Windows Procedure düzenleme

WindowProcedure içinde oluşturacağımız WM_CREATE seçeneğine aşağıdaki satırları ekleyelim:


/*  This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
        case WM_CREATE:
             /* Buton oluşturma */
             hwndButton = CreateWindowEx(0, "BUTTON", "Button",
                            WS_CHILD | WS_VISIBLE | BS_NOTIFY, 20, 20, 120, 25,
                            hwnd, (HMENU) IDC_BUTTON, NULL, NULL);
             /* Buton için ipucu penceresi oluşturma */
             hwndToolTip = CreateToolTip(IDC_BUTTON, hwnd, (PTSTR) "Buton ipucu mesajı");
             /* Tooltip kontrolünü aktif hale getirme */
             if (hwndToolTip) SendMessage(hwndToolTip, TTM_ACTIVATE, TRUE, 0);
             break;

Önce Buton kontrolü, daha sonra CreateToolTip() fonksiyonu ile Buton için kullanacağımız Tooltip kontrolü oluşturulur.

TTM_ACTIVATE mesajı Tooltip penceresine gönderilerek Tooltip penceresi aktif hale getirilir.

4. CreateToolTip() fonksiyonunu oluşturma

Buton için kullanacağımız Tooltip kontrolünü oluşturacak CreateToolTip() fonksiyonunu programa ekleyelim:


HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText)
{
  if (!toolID || !hDlg || !pszText) {
      return FALSE;
  }
  /* Tooltip gösterilecek pencere değerini alma */
  HWND hwndTool = GetDlgItem(hDlg, toolID);

  /* Tooltip oluşturma */
  HWND hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL,
                          WS_POPUP | TTS_ALWAYSTIP,
                          CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                          hDlg, NULL, g_hInst, NULL);

  if (!hwndTool || !hwndTip) {
      return (HWND)NULL;
  }

  /* Tooltip değerini pencereye bağlama */
  TOOLINFO ti = { 0 };
  ti.cbSize = sizeof(ti);
  ti.hwnd = hDlg;
  ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
  ti.uId = (UINT_PTR)hwndTool;
  ti.lpszText = pszText;
  SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);

  return hwndTip;
}

5. Kodların son hali (main.c dosyası)

Yukarıdaki kodları eklediğimizde, main.c dosyasının en son hali aşağıdaki şekilde olacaktır.

main.c


#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#define IDC_BUTTON 101

#include <tchar.h>
#include <windows.h>
#include <commctrl.h> /* TOOLTIPS_CLASS için */

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Tooltip oluşturma fonksiyonu bildirimi */
HWND CreateToolTip (int toolID, HWND hDlg, PTSTR pszText);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

/* Buton ve Tooltip pencere oluşturma */
HWND hwndButton, hwndToolTip;
HINSTANCE g_hInst;

int WINAPI WinMain (HINSTANCE hThisInstance,
                HINSTANCE hPrevInstance,
                LPSTR lpszArgument,
                int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Tooltip kontrolü oluşturma"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           385,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    g_hInst = hThisInstance;

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
        case WM_CREATE:
             /* Buton oluşturma */
             hwndButton = CreateWindowEx(0, "BUTTON", "Button",
                            WS_CHILD | WS_VISIBLE | BS_NOTIFY, 20, 20, 120, 25,
                            hwnd, (HMENU) IDC_BUTTON, NULL, NULL);
             /* Buton için ipucu penceresi oluşturma */
             hwndToolTip = CreateToolTip(IDC_BUTTON, hwnd, (PTSTR) "Buton ipucu mesajı");
             /* Tooltip kontrolünü aktif hale getirme */
             if (hwndToolTip) SendMessage(hwndToolTip, TTM_ACTIVATE, TRUE, 0);
             break;

        case WM_DESTROY:
             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
             break;
        default:                        /* for messages that we don't deal with */
             return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText)
{
  if (!toolID || !hDlg || !pszText) {
      return FALSE;
  }
  /* Tooltip gösterilecek pencere değerini alma */
  HWND hwndTool = GetDlgItem(hDlg, toolID);

  /* Tooltip oluşturma */
  HWND hwndTip = CreateWindowEx(0, TOOLTIPS_CLASS, NULL,
                          WS_POPUP | TTS_ALWAYSTIP,
                          CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
                          hDlg, NULL, g_hInst, NULL);

  if (!hwndTool || !hwndTip) {
      return (HWND)NULL;
  }

  /* Tooltip değerini pencereye bağlama */
  TOOLINFO ti = { 0 };
  ti.cbSize = sizeof(ti);
  ti.hwnd = hDlg;
  ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
  ti.uId = (UINT_PTR)hwndTool;
  ti.lpszText = pszText;
  SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&ti);

  return hwndTip;
}

Program derleyip çalıştırdığımızda aşağıdakine benzer bir ekran görüntüsü karşımıza gelecektir. Fareyi buton kontrolünün üzerine getirdiğimizde ipucu penceresi ekranda görünecek, fareyi buton kontrolü üzerinden çektiğimizde ipucu penceresi kaybolacaktır.

6. Proje dosyaları ve .exe dosya

Programın kaynak kodları

Programın exe dosyası