用win32写了一个图片窗口程序,上面的按钮都是图片,通过鼠标区域检测来实现功能。想给这些按钮加上tooltips,但是他们不像dialog上面的按钮那样,有ID,可以使用tooltips。请问有什么好的方法实现?

解决方案 »

  1.   

    可以指定ToolTip只在圖片窗口的某一個區域内出現void CreateMyTooltip (HWND hwnd)
    {
                     // struct specifying control classes to register
        INITCOMMONCONTROLSEX iccex; 
        HWND hwndTT;                 // handle to the ToolTip control
              // struct specifying info about tool in ToolTip control
        TOOLINFO ti;
        unsigned int uid = 0;       // for ti initialization
        char strTT[30] = "This is your ToolTip string.";
        LPTSTR lptstr = strTT;
        RECT rect;                  // for client area coordinates    /* INITIALIZE COMMON CONTROLS */
        iccex.dwICC = ICC_WIN95_CLASSES;
        iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
        InitCommonControlsEx(&iccex);    /* CREATE A TOOLTIP WINDOW */
        hwndTT = CreateWindowEx(WS_EX_TOPMOST,
            TOOLTIPS_CLASS,
            NULL,
            WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            hwnd,
            NULL,
            ghThisInstance,
            NULL
            );    SetWindowPos(hwndTT,
            HWND_TOPMOST,
            0,
            0,
            0,
            0,
            SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);    /* GET COORDINATES OF THE MAIN CLIENT AREA */
        GetClientRect (hwnd, &rect);    /* INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE */
        ti.cbSize = sizeof(TOOLINFO);
        ti.uFlags = TTF_SUBCLASS;
        ti.hwnd = hwnd;
        ti.hinst = ghThisInstance;
        ti.uId = uid;
        ti.lpszText = lptstr;
        
        // ToolTip control will cover the whole window
        // 改這裡就行 坐標相對于指定窗口客戶區的左上角
        ti.rect.left = rect.left;    
        ti.rect.top = rect.top;
        ti.rect.right = rect.right;
        ti.rect.bottom = rect.bottom;    /* SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW */
        SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti); }