2

在 Windows 7 之前,解决方案很简单。只需添加您自己的菜单并编写您自己的“撤消、重做、剪切、复制、粘贴、删除、全选”菜单项。但是现在这已经不可能了,因为菜单已经变得非常复杂,包含 unicode 和输入消息的东西。

4

2 回答 2

3

您需要对编辑控件进行子类化,然后使用挂钩,以下是示例代码:

首先是对编辑控件进行子类化,通常在WndProc

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch (Message)
    {
    case WM_CREATE:
    {
        HANDLE m_editControl; /* Supposing it is created */
        SetWindowSubclass(m_editControl, EditSubclassProc, 0, NULL);
    }
    break;
    default:
        /* default message handling */
    }
}

然后在子类函数中:

LRESULT CALLBACK EditSubclassProc(HWND hWndEdit, UINT Msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIDSubclass, DWORD_PTR dwRefData)
{
    LRESULT ret{};

    switch (Msg)
    {
    case WM_CONTEXTMENU:        
    {
        HWINEVENTHOOK hWinEventHook{ SetWinEventHook(EVENT_SYSTEM_MENUPOPUPSTART, EVENT_SYSTEM_MENUPOPUPSTART, NULL,
            [](HWINEVENTHOOK hWinEventHook, DWORD Event, HWND hWnd, LONG idObject, LONG idChild, DWORD idEventThread, DWORD dwmsEventTime)
            {
                if (idObject == OBJID_CLIENT && idChild == CHILDID_SELF)
                {
                    HMENU hMenuContextEdit{ (HMENU)SendMessage(hWnd, MN_GETHMENU, NULL, NULL) };

                    // Do what you want to do
                }
            },
            GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_OUTOFCONTEXT) };

        ret = DefSubclassProc(hWndEditMessage, Msg, wParam, lParam);

        UnhookWinEvent(hWinEventHook);
    }
    break;
    default:
        {
            ret = DefSubclassProc(hWndEdit, Msg, wParam, lParam);
        }
        break;
    }

    return ret;
}
于 2019-04-18T16:53:23.230 回答
1

好的,我找到了怎么做

static bool is_first_time;

case WM_CONTEXTMENU: {
   is_first_time = true;
   original_window_proc(message,wparam,lparam);
   break;

case WM_ENTERIDLE:
    if (wparam == MSGF_MENU) {
        if (is_first_time) {
            is_first_time = false; 
            MENUBARINFO mbi;
            memset(&mbi, 0, sizeof(MENUBARINFO));
            mbi.cbSize = sizeof(MENUBARINFO);
            GetMenuBarInfo((HWND)lparam, OBJID_CLIENT, 0, &mbi);
            if (::IsMenu((HMENU)mbi.hMenu)) {
               .... add your menu items here
            }
        }
    }

不幸的是,这不起作用,因为代码使用TrackPopupMenuandTPM_RETURNCMD标志TPM_NONOTIFY。因此,您可以添加新的菜单项,但无法处理命令。糟糕的微软,非常糟糕的设计。

于 2011-06-09T14:08:42.833 回答