工具提示着色不起作用

时间:2014-11-08 12:56:15

标签: c++ winapi tooltip

我是c ++ win32编程的新手,我在Windows 7 X64 SP1上使用visual studio 2013。

我在下面的链接中使用了此代码,为DialogBox中的按钮创建工具提示,并成功运行。

Tooltip for button

但是当我想要更改背景颜色或前景色时,它不起作用并显示默认颜色。

SendMessage(hwndTip, TTM_SETTIPBKCOLOR, 0xFF326FD8, 0);
//SendMessage(hwndTip, TTM_SETTIPBKCOLOR, (WPARAM)CreateSolidBrush(RGB(255,255, 255), 0);
SendMessage(hwndTip, TTM_SETTIPTEXTCOLOR, (WPARAM)CreateSolidBrush(RGB(255,255, 255)), 0);

因为在这个链接中:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb760411%28v=vs.85%29.aspx

MSDN表示“启用视觉样式时,此消息无效。”所以我改变了

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

到:

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);

但没有任何成功。

我也用过:

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

在我的代码中,所以我删除它以查看工具提示着色是否有效,但工具提示根本没有出现!

问题是什么?

感谢。

更新1:

这是我的完整源代码,希望能解决问题:

Main.cpp:

        #include <Windows.h>
        #include <stdlib.h>
        #include <tchar.h>
        #include <memory>
        #include <malloc.h>
        #include "resource.h"
        #include <CommCtrl.h>
        #include <Uxtheme.h>

        HINSTANCE hInst;                                 

        BOOL                InitInstance(HINSTANCE, int);
        INT_PTR CALLBACK    MainWin(HWND, UINT, WPARAM, LPARAM);


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


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


        int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance,_In_ LPTSTR lpCmdLine,_In_ int nCmdShow)
        {
            UNREFERENCED_PARAMETER(hPrevInstance);
            UNREFERENCED_PARAMETER(lpCmdLine);

            MSG msg;

            if (!InitInstance(hInstance, nCmdShow))
            {
                return FALSE;
            }

            while (GetMessage(&msg, NULL, 0, 0)>0)
            {
                if (!IsDialogMessage(msg.hwnd,&msg))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }

            return (int)msg.wParam;
        }



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

            HWND hwndTool = GetDlgItem(hDlg, toolID);
            if (!hwndTool)
            {
                return NULL;
            }

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

            if (!hwndTip)
            {
                return NULL;
            }

            TOOLINFO toolInfo = { 0 };
            toolInfo.cbSize = sizeof(toolInfo);
            toolInfo.hwnd = hDlg;
            toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
            toolInfo.uId = (UINT_PTR)hwndTool;
            toolInfo.lpszText = pszText;

            if (!SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo))
            {
                DestroyWindow(hwndTip);
                return NULL;
            }
            SendMessage(hwndTip, TTM_SETTIPBKCOLOR, 0xFF326FD8, 0);
            return hwndTip;
        }


        BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
        {
            HWND hWnd;

            hWnd = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MainDlg), NULL, MainWin, NULL);

            if (!hWnd)
            {
                return FALSE;
            }

            ShowWindow(hWnd, nCmdShow);
            SetWindowTheme(hWnd, L"", L"");
            return TRUE;
        }

        INT_PTR CALLBACK MainWin(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
        {
            UNREFERENCED_PARAMETER(lParam);

            switch (message)
            {
            case WM_INITDIALOG:
            {
                CreateToolTip(IDC_TestBtn, hDlg, hInst, L"TESTTOOLTIP");
                return (INT_PTR)TRUE;
            }
                break;
            case WM_CLOSE:
                DestroyWindow(hDlg);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            case WM_COMMAND:
                if (LOWORD(wParam) == IDC_TestBtn)
                {
                    MessageBox(NULL, L"TEST", L"TEST", MB_OK);
                    return (INT_PTR)TRUE;
                }

            }
            return (INT_PTR)FALSE;
        }

resource.h:

        #define IDD_MainDlg                     103
        #define IDC_TestBtn                     104


        #ifdef APSTUDIO_INVOKED
        #ifndef APSTUDIO_READONLY_SYMBOLS
        #define _APS_NEXT_RESOURCE_VALUE        106
        #define _APS_NEXT_COMMAND_VALUE         40001
        #define _APS_NEXT_CONTROL_VALUE         1006
        #define _APS_NEXT_SYMED_VALUE           101
        #endif
        #endif

resource.rc的资源脚本:

        // Microsoft Visual C++ generated resource script.
        //
        #include "resource.h"

        #define APSTUDIO_READONLY_SYMBOLS
        /////////////////////////////////////////////////////////////////////////////
        //
        // Generated from the TEXTINCLUDE 2 resource.
        //
        #include "winres.h"

        /////////////////////////////////////////////////////////////////////////////
        #undef APSTUDIO_READONLY_SYMBOLS

        /////////////////////////////////////////////////////////////////////////////
        // English (United States) resources

        #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
        LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US

        /////////////////////////////////////////////////////////////////////////////
        //
        // DESIGNINFO
        //

        #ifdef APSTUDIO_INVOKED
        GUIDELINES DESIGNINFO
        BEGIN
            IDD_MainDlg, DIALOG
            BEGIN
            END
        END
        #endif    // APSTUDIO_INVOKED


        /////////////////////////////////////////////////////////////////////////////
        //
        // Dialog
        //

        IDD_MainDlg DIALOGEX 0, 0, 149, 75
        STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
        EXSTYLE WS_EX_TOOLWINDOW
        FONT 8, "MS Shell Dlg", 400, 0, 0x1
        BEGIN
            PUSHBUTTON      "testbtn",IDC_TestBtn,41,12,57,49
        END

        #endif    // English (United States) resources
        /////////////////////////////////////////////////////////////////////////////

0 个答案:

没有答案