简体   繁体   English

winapi - 丰富的编辑控件 - 如何使用上下文菜单粘贴文本

[英]winapi - rich edit control - how to paste a text using a context menu

I create an edit window this way:我以这种方式创建了一个编辑窗口:

hwndEdit = CreateWindowEx(
   0, 
   MSFTEDIT_CLASS, 
   TEXT("EDIT"),
   WS_BORDER | WS_CHILD | ES_LEFT,
   20, 
   20, 
   100,
   30,
   gHwnd, 
   NULL, 
   hInst, 
   NULL);

I can paste a text using a keyboard shortcut (ctrl + v) but when I use a right mouse button a context menu is not displayed (for a standard edit control it works).我可以使用键盘快捷键 (ctrl + v) 粘贴文本,但是当我使用鼠标右键时,不会显示上下文菜单(对于标准编辑控件,它可以工作)。 I couldn't find any c/c++ example code.我找不到任何 c/c++ 示例代码。 How to enable/implement a context menu for a rich edit control ?如何为富编辑控件启用/实现上下文菜单?

It seems that you have created a custom edit window.您似乎已经创建了一个自定义编辑窗口。

You can handle right click on the edit window via checking for WM_CONTEXTMENU in your WndProc.您可以通过检查 WndProc 中的WM_CONTEXTMENU来处理编辑窗口上的右键单击。

Grab the handle to the window via the wParam parameter, compare it to your edit window to see if the user right clicked the edit window.通过 wParam 参数获取窗口句柄,将其与您的编辑窗口进行比较,以查看用户是否右键单击了编辑窗口。

From there, create the popupmenu via CreatePopupMenu() .从那里,通过CreatePopupMenu()创建popupmenu 菜单

Insert/Append into the menu via InsertMenu() / AppendMenu() .通过InsertMenu() / AppendMenu()插入/追加到菜单中。

Finally, call TrackPopupMenu() .最后,调用TrackPopupMenu()

Code:代码:

#define IDC_PASTE 102

case WM_CONTEXTMENU:
        if ((HWND)wParam == hwndEdit) 
        {
            m_hMenu = CreatePopupMenu();
            InsertMenu(m_hMenu, 0, MF_BYCOMMAND | MF_STRING | MF_ENABLED, IDC_PASTE, L"Paste");
            TrackPopupMenu(m_hMenu, TPM_TOPALIGN | TPM_LEFTALIGN, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), 0, hWnd, NULL);
    }

Next you need to handle the paste message.接下来您需要处理粘贴消息。 As can be seen from the problem, your paste shortcut is still useful, so you can use SendInput to simulate paste.从问题中可以看出,你的粘贴快捷方式还是有用的,所以可以使用SendInput来模拟粘贴。

Code:代码:

 case WM_COMMAND:
        {
            int wmId = LOWORD(wParam);
            // Parse the menu selections:
            switch (wmId)
            {
            case IDC_PASTE:
            {
                SetForegroundWindow(hwndEdit);
                INPUT ip;
                ip.type = INPUT_KEYBOARD;
                ip.ki.wScan = 0;
                ip.ki.time = 0;
                ip.ki.dwExtraInfo = 0;
                // Press the "Ctrl" key
                ip.ki.wVk = VK_CONTROL;
                ip.ki.dwFlags = 0; // 0 for key press
                SendInput(1, &ip, sizeof(INPUT));

                // Press the "V" key
                ip.ki.wVk = 'V';
                ip.ki.dwFlags = 0; // 0 for key press
                SendInput(1, &ip, sizeof(INPUT));

                // Release the "V" key
                ip.ki.wVk = 'V';
                ip.ki.dwFlags = KEYEVENTF_KEYUP;
                SendInput(1, &ip, sizeof(INPUT));

                // Release the "Ctrl" key
                ip.ki.wVk = VK_CONTROL;
                ip.ki.dwFlags = KEYEVENTF_KEYUP;
                SendInput(1, &ip, sizeof(INPUT));
            }
            break;
            ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM