0

我想知道控制台窗口何时移动,因此我创建了一个新的仅消息窗口来获取控制台的消息,但我不知道它是否有效,因为显然从未收到过消息。

#define WINVER 0x0501
#include <windows.h>
#include <iostream>

WNDPROC glpfnConsoleWindow; // NEW
using namespace std;

LRESULT APIENTRY MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_SIZE:
        cout<<"Window moved"<<endl;
        break;
        case WM_DESTROY:
        PostQuitMessage(0);
        break;
        default:
        // NEW
        return CallWindowProc(glpfnConsoleWindow, hwnd, uMsg, wParam, lParam);
        //return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG Msg;
    const char lpcszClassName[] = "messageClass";
    WNDCLASSEX  WindowClassEx;

    // == NEW
    HWND consHwnd;
    consHwnd = GetConsoleWindow();
    glpfnConsoleWindow = (WNDPROC)SetWindowLong(consHwnd, GWL_WNDPROC, (LONG)MainWndProc);
    // ==

    ZeroMemory(&WindowClassEx, sizeof(WNDCLASSEX));
    WindowClassEx.cbSize        = sizeof(WNDCLASSEX);
    WindowClassEx.lpfnWndProc   = MainWndProc;
    WindowClassEx.hInstance     = hInstance;
    WindowClassEx.lpszClassName = lpcszClassName;

    if (RegisterClassEx(&WindowClassEx) != 0)
    {
        // Create a message-only window
    hwnd = CreateWindowEx(0, lpcszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
        if (hwnd != NULL)
        cout<<"Window created"<<endl;
        else
        UnregisterClass(lpcszClassName, hInstance);
    }
    ShowWindow(hwnd,nCmdShow);
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
     return (int)Msg.wParam;
}
4

1 回答 1

0

也许您应该处理WM_MOVE,根据文档,它是在窗口移动后发送的。WM_SIZE 在大小改变时发送。

我认为您的问题是WM_MOVEWM_SIZE消息将进入控制台窗口而不是隐藏窗口。

我怀疑你必须调用GetConsoleWindow来获取控制台窗口句柄,然后调用SetWindowLong将你的窗口过程附加到控制台窗口。确保将消息传递到原始窗口过程。

于 2013-05-18T22:11:23.027 回答