DEV Community

海前 王
海前 王

Posted on

win32 mapwindowpoints

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

// Global variables for window handles
HWND hWndMain;
HWND hWndChild;

// Function to create a console window
void CreateConsole() {
    // Allocate a console for the calling process
    AllocConsole();

    // Redirect standard output to the console
    FILE* pFile;
    freopen_s(&pFile, "CONOUT$", "w", stdout);

    // Optionally, you can redirect standard input and error as well
    freopen_s(&pFile, "CONIN$", "r", stdin);
    freopen_s(&pFile, "CONOUT$", "w", stderr);

    std::cout << "Console created." << std::endl;
}

// Function to handle window messages
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                HDC hdc = BeginPaint(hwnd, &ps);
                // Draw a simple rectangle in the child window
                RECT rect = { 50, 50, 200, 200 };
                FillRect(hdc, &rect, (HBRUSH)(COLOR_WINDOW + 1));
                EndPaint(hwnd, &ps);
            }
            return 0;
        case WM_LBUTTONDOWN:
            {
                POINT points[2];
                points[0].x = LOWORD(lParam);
                points[0].y = HIWORD(lParam);
                points[1].x = points[0].x + 50; // Just an example offset
                points[1].y = points[0].y + 50;

                std::cout << "Source window coordinates:" << std::endl;
                std::cout << "Point 0: (" << points[0].x << ", " << points[0].y << ")" << std::endl;
                std::cout << "Point 1: (" << points[1].x << ", " << points[1].y << ")" << std::endl;

                // Map points from the main window to the child window
                MapWindowPoints(hWndMain, hWndChild, points, 2);

                std::cout << "Target window coordinates:" << std::endl;
                std::cout << "Point 0: (" << points[0].x << ", " << points[0].y << ")" << std::endl;
                std::cout << "Point 1: (" << points[1].x << ", " << points[1].y << ")" << std::endl;
            }
            return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

// Function to register and create windows
void CreateWindows(HINSTANCE hInstance) {
    // Register the main window class
    const char CLASS_NAME[] = "Main Window Class";

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the main window
    hWndMain = CreateWindowEx(
        0,
        CLASS_NAME,
        "Main Window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    ShowWindow(hWndMain, SW_SHOW);
    UpdateWindow(hWndMain);

    // Create the child window
    hWndChild = CreateWindowEx(
        0,
        CLASS_NAME,
        "Child Window",
        WS_CHILD | WS_VISIBLE,
        50, 50, 300, 300,
        hWndMain,
        NULL,
        hInstance,
        NULL
    );
}

// Entry point of the program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    MSG msg;

    // Create a console window for output
    CreateConsole();

    CreateWindows(hInstance);

    // Main message loop
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)