c – 通知图标接收WM_LBUTTONDBLCLK但不接收WM_CONTEXTMENU

c – 通知图标接收WM_LBUTTONDBLCLK但不接收WM_CONTEXTMENU,第1张

概述我在基于对话框的应用程序中添加了一个通知图标,当双击该图标时它收到了WM_LBUTTONDBLCLK,但是当右键单击图标或者用键盘突出显示图标并且上下文菜单键是上传时,它没有收到WM_CONTEXTMENU按下.我根据 Windows 7.1 SDK示例中的示例使用了通知图标.所以,我不知道我哪里出错了,或者为什么这不起作用. 注意:如果我将WM_CONTEXTMENU更改为WM_RBUTTONU 我在基于对话框的应用程序中添加了一个通知图标,当双击该图标时它收到了WM_LbuttonDBLCLK,但是当右键单击图标或者用键盘突出显示图标并且上下文菜单键是上传时,它没有收到WM_CONTEXTMENU按下.我根据 Windows 7.1 SDK示例中的示例使用了通知图标.所以,我不知道我哪里出错了,或者为什么这不起作用.

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

/******************************************************************************//* Menu Resource                                                              *//******************************************************************************/LANGUAGE LANG_NEUTRAL,SUBLANG_NEUTRALIDR_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!",MB_OK | MB_ICONERROR );        return -1;    }    // ... code unrelated to icon    MSG msg;    while( GetMessage(&msg,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,LR_DEFAulTSIZE );            // Setup the system tray icon            memset( &nID,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.",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,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,lParam);    }    return 0;}
解决方法 通知图标多年来改变了行为.出于与预先存在的代码兼容的原因,您必须选择加入新行为.如果您不选择加入,则不会收到WM_CONTEXTMENU消息.相反,你必须回应WM_RbuttonUP.即使您从键盘调用上下文菜单,系统仍会发送WM_RbuttonUP.您必须通过调用GetCursorPos获取光标位置,以便知道显示菜单的位置.

您可以通过在NIM_ADD调用后调用Shell_NotifyIcon传递NIM_SETVERSION,按照documentation中的描述选择新行为(和WM_CONTEXTMENU).据推测,您正在查看的SDK示例是在某处做的.我的猜测是你的代码遗漏了什么.

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

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.
总结

以上是内存溢出为你收集整理的c – 通知图标接收WM_LBUTTONDBLCLK但不接收WM_CONTEXTMENU全部内容,希望文章能够帮你解决c – 通知图标接收WM_LBUTTONDBLCLK但不接收WM_CONTEXTMENU所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1214360.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存