如何用C++从单独的程序打开记事本中的系统菜单?

问题描述 投票:0回答:1

我想让记事本中的系统菜单弹出,如这里所示。

Notepad system menu screenshot

不一定是帮助菜单,任何菜单都可以。

这段代码将窗口带到前台,并记录了 0x204a4 0x2bd041f 0 但却打不开菜单。

#include <iostream>
#include <windows.h>

int main() {
    HWND hWnd = FindWindow(NULL, "Untitled - Notepad");
    SetForegroundWindow(hWnd);
    HMENU hMenu = GetSystemMenu(hWnd, FALSE);
    int flag = TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON, 452, 335, NULL, hWnd, NULL);
    std::cout << hWnd << " " << hMenu << " " << flag << std::endl; // 0x204a4 0x2bd041f 0
    SendMessage(hWnd, WM_SYSCOMMAND, flag, 0);
}

g++ main.cpp


更新。

这是我更新的代码 它打开了错误的菜单。

Wrong menu opens

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow) {
    const wchar_t CLASS_NAME[] = L"My Window Class";
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    RegisterClass(&wc);
    HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"My Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
    if (hwnd == NULL) {
        return 0;
    }
    ShowWindow(hwnd, nCmdShow);
    //--------------------------------------------------------------
    HWND hWndNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (!hWndNotepad) {
        MessageBox(hwnd, L"Notepad window handle not found.", L"Error", MB_OK | MB_ICONERROR);
    }
    if (!SetForegroundWindow(hWndNotepad)) {
        MessageBox(hwnd, L"Unable to bring Notepad window to front.", L"Error", MB_OK | MB_ICONERROR);
    }
    HMENU hMenu = GetSystemMenu(hWndNotepad, FALSE);
    if (!hMenu) {
        MessageBox(hwnd, L"Notepad menu handle not found.", L"Error", MB_OK | MB_ICONERROR);
    }
    TrackPopupMenuEx(hMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON, 452, 335, hwnd, NULL);
    //--------------------------------------------------------------
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
        EndPaint(hwnd, &ps);
        break;
    }
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
c++ windows win32gui
1个回答
0
投票

Screenshot of program opening Notepad File menu without bringing Notepad to foreground.

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow) {
    const wchar_t CLASS_NAME[] = L"My Window Class";
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    RegisterClass(&wc);
    HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"My Window", WS_OVERLAPPEDWINDOW, 683 - 100, 360 - 150, 300, 200, NULL, NULL, hInstance, NULL);
    if (hwnd == NULL) {
        return 0;
    }
    ShowWindow(hwnd, nCmdShow);
    //--------------------------------------------------------------
    HWND hWndNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (!hWndNotepad) {
        MessageBox(hwnd, L"Notepad window handle not found.", L"Error", MB_OK | MB_ICONERROR);
        return 0;
    }
    HMENU hMenu = GetMenu(hWndNotepad);
    if (!hMenu) {
        MessageBox(hwnd, L"Notepad menu handle not found.", L"Error", MB_OK | MB_ICONERROR);
        return 0;
    }
    tagTPMPARAMS tpm_params;
    tpm_params.cbSize = sizeof(tagTPMPARAMS);
    GetWindowRect(hWndNotepad, &tpm_params.rcExclude);
    TrackPopupMenuEx(GetSubMenu(hMenu, 0), NULL, tpm_params.rcExclude.left, tpm_params.rcExclude.top, hwnd, NULL);
    //--------------------------------------------------------------
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_PAINT: {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
        EndPaint(hwnd, &ps);
        break;
    }
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

g++ main.cpp -mwindows

© www.soinside.com 2019 - 2024. All rights reserved.