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