4

根据我所看到的一切,当我在应用程序窗口中左键单击时,以下 C++ 程序应该从托盘图标显示气球工具提示,但它不起作用。谁能告诉我我错过了什么?

这是在带有 Shell32.dll 6.0 版的 XP 上(使用 DllGetVersion 验证)。

谢谢!

    #include "stdafx.h"
    #include "shellapi.h"

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
        MSG msg;

        WNDCLASS wc;
        memset(&wc, 0, sizeof(wc));
        wc.lpfnWndProc = WndProc;
        wc.hInstance = hInstance;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszClassName = "sysTrayTest";
        RegisterClass(&wc);

        HWND hWnd = CreateWindow("sysTrayTest", "", 
                                WS_OVERLAPPEDWINDOW,
                                CW_USEDEFAULT, 0, 500, 500, 
                                NULL, NULL, hInstance, NULL);
        if (hWnd)
        {
            ShowWindow(hWnd, nCmdShow);
            while (GetMessage(&msg, NULL, 0, 0))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        return 0;
    }

    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_DESTROY:
            {
                NOTIFYICONDATA nid;
                memset(&nid, 0, sizeof(NOTIFYICONDATA));
                nid.cbSize = sizeof(NOTIFYICONDATA);
                nid.hWnd = hWnd;
                nid.uID = 1;
                Shell_NotifyIcon(NIM_DELETE, &nid);

                PostQuitMessage(0);
            }
            break;

            case WM_CREATE:
            {
                NOTIFYICONDATA nid;
                memset(&nid, 0, sizeof(NOTIFYICONDATA));
                nid.cbSize = sizeof(NOTIFYICONDATA);
                nid.hWnd = hWnd;
                nid.uID = 1;
                nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
                nid.uCallbackMessage = WM_USER + 200;
                nid.hIcon = LoadIcon(NULL, IDI_INFORMATION);
                lstrcpy (nid.szTip, "Test Tip");
                Shell_NotifyIcon(NIM_ADD, &nid);
            }
            break;

            case WM_LBUTTONDOWN:
            {
                NOTIFYICONDATA nid;
                memset(&nid, 0, sizeof(NOTIFYICONDATA));
                nid.cbSize = sizeof(NOTIFYICONDATA);
                nid.hWnd = hWnd;
                nid.uID = 1;
                nid.uFlags = NIF_INFO;
                lstrcpy(nid.szInfo, "Test balloon tip");
                lstrcpy(nid.szInfoTitle, "Test Title");
                nid.dwInfoFlags = NIIF_INFO;
                nid.uTimeout = 15000;
                Shell_NotifyIcon(NIM_MODIFY, &nid);
            }
            break;

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

3 回答 3

13

呸,我想通了。出于某种原因,我的标题...

大小(通知图标数据)== 508

然而...

NOTIFYICONDATA _V3_大小 == 504
NOTIFYICONDATA _V2_大小 == 488
NOTIFYICONDATA _V1_大小 == 88

如果我指定 V2 或 V3 而不是 sizeof(NOTIFYICONDATA) 气球提示显示就好了。

于 2009-04-22T21:58:18.347 回答
1

您是否在...下检查了注册表

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced

... 对于 EnableBalloonTips?用户关闭是很常见的事情。

于 2009-04-22T04:47:43.300 回答
1

问题是您假设 Windows 会WM_LBUTTONDOWN在用户单击图标时向您发送 a,但这是不正确的。WM_LBUTTONDOWN仅当用户在 hWnd 的客户区域内单击时才会发送,如果您仔细阅读文档,NOTIFYICONDATA您会意识到当用户单击图标时,Windows 会向您发送一条WM_USER+20消息(根据您的代码),并且在 lParam 参数中您会得到通知WM_LBUTTONDOWN

于 2011-12-01T18:17:34.793 回答