通知图标接收到 WM_LBUTTONDBLCLK 但未接收到 WM_CONTEXTMENU

发布于 2024-12-19 20:01:41 字数 7922 浏览 2 评论 0原文

我向基于对话框的应用程序添加了一个通知图标,当双击该图标时,它收到了 WM_LBUTTONDBLCLK,但当右键单击该图标或用键盘突出显示该图标并且上下文菜单键为按下。我根据 Windows 7.1 SDK 示例中的示例使用通知图标。所以,我不知道我哪里出了问题,也不知道为什么这不起作用。

注意:如果我将 WM_CONTEXTMENU 更改为 WM_RBUTTONUP,它会收到事件,但光标坐标错误。

/******************************************************************************/
/* Menu Resource                                                              */
/******************************************************************************/
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_TRAYMENU MENU
{
    POPUP ""
    {
        MENUITEM "&Show Status Window", IDM__SHOW_STATUS_WINDOW
        MENUITEM "&About", IDM__ABOUT
        MENUITEM SEPARATOR
        MENUITEM "&Exit", IDM__EXIT
    }
}

/******************************************************************************/
/* WinMain()                                                                  */
/******************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{

    // ... code unrelated to icon

    // Enable Visual Styles
    InitCommonControls();

    // create the main dialog
    if( NULL == (hWnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MAINDLG),NULL,(DLGPROC)WndProc)) )
    {
        MessageBox( NULL, "Error creating the main dialog!", NULL, MB_OK | MB_ICONERROR );
        return -1;
    }

    // ... code unrelated to icon

    MSG msg;
    while( GetMessage(&msg,NULL,0,0) && IsWindow(hWnd) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0; 

}    
/******************************************************************************/
/* WndProc()                                                                  */
/******************************************************************************/
BOOL CALLBACK WndProc(HWND hWndDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{

    switch(Message)
    {
        case WM_INITDIALOG:
        {

            // ... code unrelated to icon
            hIcon = (HICON)LoadImage( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_DDCMP), IMAGE_ICON, 16, 16, LR_DEFAULTSIZE );

            // Setup the system tray icon
            memset( &nid, 0, sizeof(NOTIFYICONDATA) );
            nid.cbSize = sizeof(NOTIFYICONDATA);
            nid.hWnd = hWndDlg;
            nid.uID = 0xDDC;
            nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP;
            nid.uCallbackMessage = WM_APP + 0xDDC;
            nid.hIcon = hIcon;
            strcpy( nid.szTip, "DDCMP Driver" );
            Shell_NotifyIcon( NIM_ADD, &nid );

            // ... code unrelated to icon

            return true;
        } break;

        case WM_APP + 0xDDC:
        {
            switch( LOWORD(lParam) )
            {
                case WM_CONTEXTMENU:
                {
                    MessageBox( hWndDlg, "This message box never shows up.", NULL, MB_OK | MB_SYSTEMMODAL );
                    HMENU hMenu = LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_TRAYMENU));
                    if( hMenu )
                    {
                        HMENU hSubMenu = GetSubMenu(hMenu,0);
                        if( hSubMenu )
                        {
                            SetForegroundWindow( hWndDlg );
                            POINT pt = { LOWORD(wParam), HIWORD(wParam) };
                            UINT uFlags = TPM_RIGHTBUTTON;
                            if( 0 != GetSystemMetrics(SM_MENUDROPALIGNMENT) )
                                uFlags |= TPM_RIGHTALIGN;
                            else
                                uFlags |= TPM_LEFTALIGN;
                            TrackPopupMenuEx( hSubMenu, uFlags, pt.x, pt.y, hWndDlg, NULL );

                        }
                        DestroyMenu( hMenu );
                    }
                } break;
                case WM_LBUTTONDBLCLK:
                    if( IsWindowVisible(hWndDlg) )
                        ShowWindow( hWnd, SW_HIDE );
                    else
                        ShowWindow( hWnd, SW_SHOW );
                    break;
            }
            return true;
        } break;

        case WM_CLOSE:
            ShowWindow( hWndDlg, SW_HIDE );
            break;

        case WM_DESTROY:
        case WM_QUIT:
        {
            Shell_NotifyIcon( NIM_DELETE, &nid );

            // ... code unrelated to icon

            return true;
        } break;
    }

    return false;
}

这是 Windows 7.1 SDK 示例中的 WndProc

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND s_hwndFlyout = NULL;
    static BOOL s_fCanShowFlyout = TRUE;

    switch (message)
    {
    case WM_CREATE:
        // add the notification icon
        if (!AddNotificationIcon(hwnd))
        {
            MessageBox(hwnd,
                L"Please read the ReadMe.txt file for troubleshooting",
                L"Error adding icon", MB_OK);
            return -1;
        }
        break;
    case WM_COMMAND:
        {
            int const wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_LOWINK:
                ShowLowInkBalloon();
                break;

            case IDM_NOINK:
                ShowNoInkBalloon();
                break;

            case IDM_PRINTJOB:
                ShowPrintJobBalloon();
                break;

            case IDM_OPTIONS:
                // placeholder for an options dialog
                MessageBox(hwnd,  L"Display the options dialog here.", L"Options", MB_OK);
                break;

            case IDM_EXIT:
                DestroyWindow(hwnd);
                break;

            case IDM_FLYOUT:
                s_hwndFlyout = ShowFlyout(hwnd);
                break;

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

    case WMAPP_NOTIFYCALLBACK:
        switch (LOWORD(lParam))
        {
        case NIN_SELECT:
            // for NOTIFYICON_VERSION_4 clients, NIN_SELECT is prerable to listening to mouse clicks and key presses
            // directly.
            if (IsWindowVisible(s_hwndFlyout))
            {
                HideFlyout(hwnd, s_hwndFlyout);
                s_hwndFlyout = NULL;
                s_fCanShowFlyout = FALSE;
            }
            else if (s_fCanShowFlyout)
            {
                s_hwndFlyout = ShowFlyout(hwnd);
            }
            break;

        case NIN_BALLOONTIMEOUT:
            RestoreTooltip();
            break;

        case NIN_BALLOONUSERCLICK:
            RestoreTooltip();
            // placeholder for the user clicking on the balloon.
            MessageBox(hwnd, L"The user clicked on the balloon.", L"User click", MB_OK);
            break;


        // 
        //
        // As you can very plainly see, the Windows SDK Sample ONLY used WM_CONTEXTMNEU
        // 
        //

        case WM_CONTEXTMENU:
            {
                POINT const pt = { LOWORD(wParam), HIWORD(wParam) };
                ShowContextMenu(hwnd, pt);
            }
            break;
        }
        break;

    case WMAPP_HIDEFLYOUT:
        HideFlyout(hwnd, s_hwndFlyout);
        s_hwndFlyout = NULL;
        s_fCanShowFlyout = FALSE;
        break;

    case WM_TIMER:
        if (wParam == HIDEFLYOUT_TIMER_ID)
        {
            // please see the comment in HideFlyout() for an explanation of this code.
            KillTimer(hwnd, HIDEFLYOUT_TIMER_ID);
            s_fCanShowFlyout = TRUE;
        }
        break;
    case WM_DESTROY:
        DeleteNotificationIcon();
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

I added a notification icon to my dialog based application, and it received WM_LBUTTONDBLCLK when the icon is double clicked on, but it is not receiving WM_CONTEXTMENU when the icon is right clicked or when the icon is highlighted with the keyboard and the context menu key is pressed. I based my usage of the notification icon on the example in the Windows 7.1 SDK Samples. So, I have no idea where I'm going wrong or why this isn't working.

Note: If I change WM_CONTEXTMENU to WM_RBUTTONUP, it receives the event, but the cursor coordinates are wrong.

/******************************************************************************/
/* Menu Resource                                                              */
/******************************************************************************/
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_TRAYMENU MENU
{
    POPUP ""
    {
        MENUITEM "&Show Status Window", IDM__SHOW_STATUS_WINDOW
        MENUITEM "&About", IDM__ABOUT
        MENUITEM SEPARATOR
        MENUITEM "&Exit", IDM__EXIT
    }
}

/******************************************************************************/
/* WinMain()                                                                  */
/******************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{

    // ... code unrelated to icon

    // Enable Visual Styles
    InitCommonControls();

    // create the main dialog
    if( NULL == (hWnd=CreateDialog(hInstance,MAKEINTRESOURCE(IDD_MAINDLG),NULL,(DLGPROC)WndProc)) )
    {
        MessageBox( NULL, "Error creating the main dialog!", NULL, MB_OK | MB_ICONERROR );
        return -1;
    }

    // ... code unrelated to icon

    MSG msg;
    while( GetMessage(&msg,NULL,0,0) && IsWindow(hWnd) )
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0; 

}    
/******************************************************************************/
/* WndProc()                                                                  */
/******************************************************************************/
BOOL CALLBACK WndProc(HWND hWndDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{

    switch(Message)
    {
        case WM_INITDIALOG:
        {

            // ... code unrelated to icon
            hIcon = (HICON)LoadImage( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_DDCMP), IMAGE_ICON, 16, 16, LR_DEFAULTSIZE );

            // Setup the system tray icon
            memset( &nid, 0, sizeof(NOTIFYICONDATA) );
            nid.cbSize = sizeof(NOTIFYICONDATA);
            nid.hWnd = hWndDlg;
            nid.uID = 0xDDC;
            nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP;
            nid.uCallbackMessage = WM_APP + 0xDDC;
            nid.hIcon = hIcon;
            strcpy( nid.szTip, "DDCMP Driver" );
            Shell_NotifyIcon( NIM_ADD, &nid );

            // ... code unrelated to icon

            return true;
        } break;

        case WM_APP + 0xDDC:
        {
            switch( LOWORD(lParam) )
            {
                case WM_CONTEXTMENU:
                {
                    MessageBox( hWndDlg, "This message box never shows up.", NULL, MB_OK | MB_SYSTEMMODAL );
                    HMENU hMenu = LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_TRAYMENU));
                    if( hMenu )
                    {
                        HMENU hSubMenu = GetSubMenu(hMenu,0);
                        if( hSubMenu )
                        {
                            SetForegroundWindow( hWndDlg );
                            POINT pt = { LOWORD(wParam), HIWORD(wParam) };
                            UINT uFlags = TPM_RIGHTBUTTON;
                            if( 0 != GetSystemMetrics(SM_MENUDROPALIGNMENT) )
                                uFlags |= TPM_RIGHTALIGN;
                            else
                                uFlags |= TPM_LEFTALIGN;
                            TrackPopupMenuEx( hSubMenu, uFlags, pt.x, pt.y, hWndDlg, NULL );

                        }
                        DestroyMenu( hMenu );
                    }
                } break;
                case WM_LBUTTONDBLCLK:
                    if( IsWindowVisible(hWndDlg) )
                        ShowWindow( hWnd, SW_HIDE );
                    else
                        ShowWindow( hWnd, SW_SHOW );
                    break;
            }
            return true;
        } break;

        case WM_CLOSE:
            ShowWindow( hWndDlg, SW_HIDE );
            break;

        case WM_DESTROY:
        case WM_QUIT:
        {
            Shell_NotifyIcon( NIM_DELETE, &nid );

            // ... code unrelated to icon

            return true;
        } break;
    }

    return false;
}

This is the WndProc from the Windows 7.1 SDK Sample

    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND s_hwndFlyout = NULL;
    static BOOL s_fCanShowFlyout = TRUE;

    switch (message)
    {
    case WM_CREATE:
        // add the notification icon
        if (!AddNotificationIcon(hwnd))
        {
            MessageBox(hwnd,
                L"Please read the ReadMe.txt file for troubleshooting",
                L"Error adding icon", MB_OK);
            return -1;
        }
        break;
    case WM_COMMAND:
        {
            int const wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDM_LOWINK:
                ShowLowInkBalloon();
                break;

            case IDM_NOINK:
                ShowNoInkBalloon();
                break;

            case IDM_PRINTJOB:
                ShowPrintJobBalloon();
                break;

            case IDM_OPTIONS:
                // placeholder for an options dialog
                MessageBox(hwnd,  L"Display the options dialog here.", L"Options", MB_OK);
                break;

            case IDM_EXIT:
                DestroyWindow(hwnd);
                break;

            case IDM_FLYOUT:
                s_hwndFlyout = ShowFlyout(hwnd);
                break;

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

    case WMAPP_NOTIFYCALLBACK:
        switch (LOWORD(lParam))
        {
        case NIN_SELECT:
            // for NOTIFYICON_VERSION_4 clients, NIN_SELECT is prerable to listening to mouse clicks and key presses
            // directly.
            if (IsWindowVisible(s_hwndFlyout))
            {
                HideFlyout(hwnd, s_hwndFlyout);
                s_hwndFlyout = NULL;
                s_fCanShowFlyout = FALSE;
            }
            else if (s_fCanShowFlyout)
            {
                s_hwndFlyout = ShowFlyout(hwnd);
            }
            break;

        case NIN_BALLOONTIMEOUT:
            RestoreTooltip();
            break;

        case NIN_BALLOONUSERCLICK:
            RestoreTooltip();
            // placeholder for the user clicking on the balloon.
            MessageBox(hwnd, L"The user clicked on the balloon.", L"User click", MB_OK);
            break;


        // 
        //
        // As you can very plainly see, the Windows SDK Sample ONLY used WM_CONTEXTMNEU
        // 
        //

        case WM_CONTEXTMENU:
            {
                POINT const pt = { LOWORD(wParam), HIWORD(wParam) };
                ShowContextMenu(hwnd, pt);
            }
            break;
        }
        break;

    case WMAPP_HIDEFLYOUT:
        HideFlyout(hwnd, s_hwndFlyout);
        s_hwndFlyout = NULL;
        s_fCanShowFlyout = FALSE;
        break;

    case WM_TIMER:
        if (wParam == HIDEFLYOUT_TIMER_ID)
        {
            // please see the comment in HideFlyout() for an explanation of this code.
            KillTimer(hwnd, HIDEFLYOUT_TIMER_ID);
            s_fCanShowFlyout = TRUE;
        }
        break;
    case WM_DESTROY:
        DeleteNotificationIcon();
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单挑你×的.吻 2024-12-26 20:01:41

我认为您应该尝试将 NOTIFYICONDATA 结构的 uVersion 成员更改为 NOTIFYICON_VERSION_4,即 文档 指出此成员值将告诉 uCallbackMessage 参数在传递到回调函数时将如何解释。

您还可以看看这个: Win32 中 Shell_NotifyIcon 的基本使用

我做了一些研究,以下内容应该对您有用:

memset(&nid, 0, sizeof(NOTIFYICONDATA));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWndDlg;
nid.uID = 0xDDC;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP;
nid.uCallbackMessage = WM_APP + 0xDDC;
nid.hIcon = hIcon;
nid.uVersion = NOTIFYICON_VERSION_4;
strcpy(nid.szTip, "DDCMP Driver");
Shell_NotifyIcon(NIM_ADD, &nid);
Shell_NotifyIcon(NIM_SETVERSION, &nid);

NIM_SETVERSION(MSDN):

仅限 Shell32.dll 版本 5.0 及更高版本。指示通知
区域根据指定的版本号进行操作
lpdata 指向的结构体的 uVersion 成员。版本
number 指定哪些成员被识别。

I think you should try changing the uVersion member of the NOTIFYICONDATA structure to NOTIFYICON_VERSION_4, the documentation states that this members value will tell how the uCallbackMessage parameters will be interpreted when passed on to your callback function.

You can also have a look at this: Basic use of Shell_NotifyIcon in Win32

I did a bit of research and the following should work for you:

memset(&nid, 0, sizeof(NOTIFYICONDATA));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWndDlg;
nid.uID = 0xDDC;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP;
nid.uCallbackMessage = WM_APP + 0xDDC;
nid.hIcon = hIcon;
nid.uVersion = NOTIFYICON_VERSION_4;
strcpy(nid.szTip, "DDCMP Driver");
Shell_NotifyIcon(NIM_ADD, &nid);
Shell_NotifyIcon(NIM_SETVERSION, &nid);

NIM_SETVERSION (MSDN):

Shell32.dll version 5.0 and later only. Instructs the notification
area to behave according to the version number specified in the
uVersion member of the structure pointed to by lpdata. The version
number specifies which members are recognized.

傲鸠 2024-12-26 20:01:41

多年来,通知图标的行为已经发生了变化。出于与现有代码兼容的原因,您必须选择新行为。如果您不选择加入,则不会收到 WM_CONTEXTMENU 消息。相反,您必须响应 WM_RBUTTONUP。即使您从键盘调用上下文菜单,系统仍然会发送WM_RBUTTONUP。您必须通过调用 GetCursorPos 来获取光标位置,以便知道在哪里显示菜单。

您可以按照 文档,通过调用 Shell_NotifyIcon 传递NIM_SETVERSIONNIM_ADD 调用之后。大概您正在查看的 SDK 示例在某处执行此操作。我的猜测是,这就是您的代码中缺少的内容。

文档的关键摘录位于备注部分:

从 Windows 2000(Shell32.dll 版本 5.0)开始,Shell_NotifyIcon 鼠标和键盘事件的处理方式与 Microsoft Windows NT 4.0、Windows 95 和 Windows 98 上的早期 Shell 版本不同。差异包括以下内容:

  • 如果用户使用键盘选择通知图标的快捷菜单,Shell 现在会向关联的应用程序发送 WM_CONTEXTMENU 消息。早期版本发送 WM_RBUTTONDOWN 和 WM_RBUTTONUP 消息。
  • 如果用户使用键盘选择通知图标并使用空格键或 ENTER 键激活它,则 5.0 版 Shell 会向关联的应用程序发送 NIN_KEYSELECT 通知。早期版本发送 WM_RBUTTONDOWN 和 WM_RBUTTONUP 消息。
  • 如果用户使用鼠标选择通知图标并使用 ENTER 键激活它,Shell 现在会向关联的应用程序发送 NIN_SELECT 通知。早期版本发送 WM_RBUTTONDOWN 和 WM_RBUTTONUP 消息。

The notify icon has changed behaviour over the years. For reasons of compatibility with pre-existing code, you must opt-in to the new behaviour. If you don't opt-in then you don't get sent WM_CONTEXTMENU messages. Instead you have to respond to WM_RBUTTONUP. Even if you invoke the context menu from the keyboard, the system still sends WM_RBUTTONUP. You have to obtain the cursor position, in order to know where to show the menu, by calling GetCursorPos.

You can opt in to the new behaviour (and WM_CONTEXTMENU) as described in the documentation, by calling Shell_NotifyIcon passing NIM_SETVERSION after the NIM_ADD call. Presumably the SDK sample you are looking at does this somewhere. My guess is that is what is missing from your code.

The key extract from the documentation is in the remarks section:

As of Windows 2000 (Shell32.dll version 5.0), Shell_NotifyIcon mouse and keyboard events are handled differently than in earlier Shell versions found on Microsoft Windows NT 4.0, Windows 95, and Windows 98. The differences include the following:

  • If a user selects a notify icon's shortcut menu with the keyboard, the Shell now sends the associated application a WM_CONTEXTMENU message. Earlier versions send WM_RBUTTONDOWN and WM_RBUTTONUP messages.
  • If a user selects a notify icon with the keyboard and activates it with the SPACEBAR or ENTER key, the version 5.0 Shell sends the associated application an NIN_KEYSELECT notification. Earlier versions send WM_RBUTTONDOWN and WM_RBUTTONUP messages.
  • If a user selects a notify icon with the mouse and activates it with the ENTER key, the Shell now sends the associated application an NIN_SELECT notification. Earlier versions send WM_RBUTTONDOWN and WM_RBUTTONUP messages.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文