如何自定义工具提示的外观? (C ++ WINAPI)

时间:2016-06-15 19:03:35

标签: c++ winapi tooltip

我能够创建一个显示简单消息的工具提示,但是我想通过阅读标题来猜测,自定义它添加一些奇特的颜色,字体或类似的东西。 那是我的代码:

// Balloon.cpp : definisce il punto di ingresso dell'applicazione.
//

 #include "stdafx.h"
 #include "Balloon.h"
 #include <Commctrl.h>

 #pragma comment(lib, "ComCtl32.lib")

 #pragma comment(linker, \
"\"/manifestdependency:type='Win32' "\
"name='Microsoft.Windows.Common-Controls' "\
"version='6.0.0.0' "\
"processorArchitecture='*' "\
"publicKeyToken='6595b64144ccf1df' "\
"language='*'\"")


#define MAX_LOADSTRING 100

 // Variabili globali:
 HINSTANCE hInst;                                // istanza corrente
 WCHAR szTitle[MAX_LOADSTRING];                  // Testo della barra del titolo
  WCHAR szWindowClass[MAX_LOADSTRING];            // nome della classe di finestre principale

 // Dichiarazioni con prototipo delle funzioni incluse in questo modulo di codice:
 ATOM                MyRegisterClass(HINSTANCE hInstance);
 BOOL                InitInstance(HINSTANCE, int);
 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
 INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

 int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                 _In_opt_ HINSTANCE hPrevInstance,
                 _In_ LPWSTR    lpCmdLine,
                 _In_ int       nCmdShow)
 {
    UNREFERENCED_PARAMETER(hPrevInstance);
     UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: inserire qui il codice.




// Inizializzare le stringhe globali
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_BALLOON, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Eseguire l'inizializzazione dall'applicazione:
if (!InitInstance (hInstance, nCmdShow))
{
    return FALSE;
}

HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BALLOON));

MSG msg;

// Ciclo di messaggi principale:
while (GetMessage(&msg, nullptr, 0, 0))
{
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

return (int) msg.wParam;
}



//
//  FUNZIONE: MyRegisterClass()
//
//  SCOPO: registra la classe di finestre.
//
 ATOM MyRegisterClass(HINSTANCE hInstance)
 {
WNDCLASSEXW wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style          = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra     = 0;
wcex.cbWndExtra     = 0;
wcex.hInstance      = hInstance;
wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BALLOON));
wcex.hCursor        = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = MAKEINTRESOURCEW(IDC_BALLOON);
wcex.lpszClassName  = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassExW(&wcex);
 }

 HWND CreateToolTip(int toolID, HINSTANCE hInst, HWND hDlg, PTSTR pszText) {
if (!toolID || !hDlg || !pszText) {
    return FALSE;
}

HWND hwndTool = GetDlgItem(hDlg, toolID);

HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
    WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON,
    CW_USEDEFAULT, CW_USEDEFAULT,
    CW_USEDEFAULT, CW_USEDEFAULT,
    hDlg, NULL,
    hInst, NULL);

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

TOOLINFO toolInfo;
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hDlg;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hwndTool;
toolInfo.lpszText = pszText;
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);

return hwndTip;
}

//
//   FUNZIONE: InitInstance(HINSTANCE, int)
//
//   SCOPO: salva l'handle di istanza e crea la finestra principale
//
//   COMMENTI:
//
//        In questa funzione l'handle di istanza viene salvato in una variabile globale e
//        la finestra di programma principale viene creata e visualizzata.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
 hInst = hInstance; // Memorizzare l'handle di istanza nella variabile globale

 HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);

  if (!hWnd)
  {
  return FALSE;
  }

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);

  return TRUE;
}

//
//  FUNZIONE: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  SCOPO:  elabora i messaggi per la finestra principale.
//
//  WM_COMMAND - elabora il menu dell'applicazione
//  WM_PAINT - disegna la finestra principale
//  WM_DESTROY - inserisce un messaggio di uscita e restituisce un risultato
//
//
HWND mybutton, tooltip_mess;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
    {
        int wmId = LOWORD(wParam);
        // Analizzare le selezioni di menu:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    break;
case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        // TODO: aggiungere qui il codice di disegno che usa HDC...
        EndPaint(hWnd, &ps);
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
    /*EDITBALLOONTIP ebt;

    ebt.cbStruct = sizeof(ebt);
    ebt.pszText = L" Tooltip text! ";
    ebt.pszTitle = L" Tooltip title!!! ";
    ebt.ttiIcon = TTI_ERROR_LARGE;    // tooltip icon

    SendMessage(hWnd, EM_SHOWBALLOONTIP, 0, (LPARAM)&ebt);

    */
case WM_CREATE:
    mybutton = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        L"BUTTON",
        L"My Button",
        WS_VISIBLE | WS_CHILD,
        10, 10, 100, 24,
        hWnd,
        (HMENU)11,
        hInst,
        NULL);
    tooltip_mess = CreateToolTip(11, hInst, hWnd, L"Tooltip message");

    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

 // Gestore dei messaggi della finestra Informazioni su.
 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
 {
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
    return (INT_PTR)TRUE;

case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    {
        EndDialog(hDlg, LOWORD(wParam));
        return (INT_PTR)TRUE;
    }
    break;
}
return (INT_PTR)FALSE;
 }

这让我获得了这样的结果:

Tracking tooltip

这有点难过......我的想法是得到这样的东西:

Target tooltip

我找到了关于EDITBALLOONTIP

的网络信息

它似乎提供了我想要的东西,但我意识到你只能与某个按钮联合使用它,而我想在玩家做错行动时弹出我的工具提示。

请问有谁能以正确的方式向我发表讲话,甚至给我一些可以在我的案例中做的事情,请呢?

提前致谢。

1 个答案:

答案 0 :(得分:0)

CMFCToolTipInfo有更多选项可以使用MFC功能包自定义外观。

示例:

CMFCToolTipInfo params;
params.m_bBoldLabel = FALSE;
params.m_bDrawDescription = TRUE;
params.m_bDrawIcon = TRUE;
params.m_bRoundedCorners = TRUE;
params.m_bDrawSeparator = FALSE
params.m_clrFill = RGB (100, 100, 100);
params.m_clrFillGradient = RGB (100, 100, 100);
params.m_clrText = RGB (255, 255, 255); //RGB (61, 83, 80);
params.m_clrBorder =  RGB (255, 255, 255);
params.m_bBalloonTooltip = FALSE;
params.m_bVislManagerTheme = FALSE;

m_ToolTip.SetParams (&params);
相关问题