Win32 - 儿童窗口绘画

时间:2015-06-30 02:56:49

标签: c++ windows winapi

我有一个包含两个子窗口(Child1和Child2)的主窗口。在Child1上移动Child2,然后将Child1移开,Child 2客户区和非客户区被绘制到Child 1上。

请参见此处的图片:child windows

请帮忙吗?

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndChildProc1(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndChildProc2(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int nCmdShow)
{
    WNDCLASS    wndclass;
    HWND        hwndMain, hwndChild1, hwndChild2;
    MSG         msg;

    //register main window class
    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hIcon          = 0;
    wndclass.hCursor        = LoadCursor(0, IDC_ARROW);
    wndclass.hInstance      = hInstance;
    wndclass.hbrBackground  = (HBRUSH) COLOR_WINDOW;
    wndclass.lpszMenuName   = 0;
    wndclass.lpszClassName  = TEXT("MainWindow");

    if(!RegisterClass(&wndclass)) return 0;

    //register child window class
    wndclass.lpfnWndProc    = WndChildProc1;
    wndclass.lpszClassName  = TEXT("ChildWindow1");

    if(!RegisterClass(&wndclass)) return 0;

    wndclass.lpfnWndProc    = WndChildProc2;
    wndclass.lpszClassName  = TEXT("ChildWindow2");

    if(!RegisterClass(&wndclass)) return 0;

    //create main window
    hwndMain = CreateWindow(TEXT("MainWindow"), TEXT("Main Window"),
                            WS_OVERLAPPEDWINDOW,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            CW_USEDEFAULT, CW_USEDEFAULT,
                            0, 0, hInstance, 0);

    if(!hwndMain) return 0;

    //create child windows
    hwndChild1 = CreateWindow(TEXT("ChildWindow1"), TEXT("Child Window 1"),
                              WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
                              10, 10, 200, 300,
                              hwndMain, 0, hInstance, 0);

    hwndChild2 = CreateWindow(TEXT("ChildWindow2"), TEXT("Child Window 2"),
                              WS_CHILD | WS_CAPTION | WS_SYSMENU | WS_VISIBLE,
                              250, 10, 200, 300,
                              hwndMain, 0, hInstance, 0);

    ShowWindow(hwndMain, SW_SHOW);
    UpdateWindow(hwndMain);

    while(GetMessage(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK WndChildProc1(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK WndChildProc2(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

感谢。

0 个答案:

没有答案