注册 登录
编程论坛 C++ Builder

建立25个子窗口平分主窗口,每个子窗口的位置都是一样的,求解!

疯狂的小白 发布于 2013-05-29 17:05, 1711 次点击
程序代码:
把一个窗口分成25个子窗口,创建在主窗口的WM_CREATE里新建25个子窗口,但是在子窗口的回调函数中显示,子窗口的位置每次都是一样的,照着书检查了很多次,搞了一天,为解决问题,求助!
#include <Windows.h>
LRESULT CALLBACK wndprog(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK childprog(HWND,UINT,WPARAM,LPARAM);
HINSTANCE hinst;
int idfocus = 0;
TCHAR szchild[] = TEXT("child!");
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,  int nShowCmd )
{
    TCHAR szappname[] = TEXT("mywindow");
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    wndclass.style = CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc = wndprog;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szappname;

    if ( !RegisterClass(&wndclass))
    {
        MessageBox(NULL,"creat window failed!",szappname,MB_ICONERROR);
        return 0 ;
    }
    // 设置子窗口类属性
    wndclass.lpfnWndProc = childprog;
    wndclass.cbWndExtra = sizeof(long);
    wndclass.hIcon = NULL;
    wndclass.lpszClassName = szchild;
    if ( !RegisterClass(&wndclass))
    {
        MessageBox(NULL,"creat window failed!",szappname,MB_ICONERROR);
        return 0 ;
    }
    // 保存实例
    hinst = hInstance;

    hwnd = CreateWindow(szappname,TEXT("This is my window !"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,
        CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,    NULL,
        hInstance,    NULL);

    ShowWindow(hwnd,nShowCmd);
    UpdateWindow(hwnd);

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

    return msg.wParam;
}

LRESULT CALLBACK wndprog(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
    static HWND hchild[5][5];
    static int part = 5;
    static int xadd,yadd;
    int x = 0, y = 0;

    switch (message)
    {
    case WM_CREATE:
        xadd = LOWORD(lparam)/ part;
        yadd = HIWORD(lparam)/part;
        for (  x = 0; x < 5; x++)
        {
            for (  y = 0; y < 5; y++)
                hchild[x][y] = CreateWindow(szchild, NULL, WS_CHILDWINDOW | WS_VISIBLE,
                                                            0,0,0,0,                    //x*xadd, y*yadd, xadd, yadd,不知为何这四个参数全为0,样例代码正常运行,我的就不行。
                                                            hwnd,(HMENU)( x*5+y ) , hinst, NULL);      
        }
        return 0;
    case WM_SIZE:
        xadd = LOWORD(lparam)/ part; // 把窗口分成5分
        yadd = HIWORD(lparam)/part;
      
        for ( int i = 0; i < 5; i++)
        {
            for ( int j = 0; j < 5; j++)
                MoveWindow(hchild[i][j], x * xadd, y * yadd, xadd, yadd,TRUE);
        }
        return 0 ;
    case WM_SETFOCUS:
        SetFocus(GetDlgItem(hwnd,idfocus));   
        break;
    case WM_KEYDOWN:
        {
            x = idfocus / 5;
            y = idfocus % 5;

            switch(wparam)
            {
            case VK_LEFT:        x--;    break;
            case VK_RIGHT:        x++; break;
            case VK_UP:            y--;    break;
            case VK_DOWN:    y++; break;
            default: return 0;
            }
        }
        x = (x + 5) %5;
        y = ( y+5 )  %5;
        idfocus = x * 5 + y;
        SetFocus(GetDlgItem(hwnd,idfocus));   
        return 0 ;
    case WM_LBUTTONDOWN:
        MessageBeep(0);

        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hwnd,message,wparam,lparam);
}

LRESULT CALLBACK childprog(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    int idchild;
    POINT pt;

    switch(message)
    {
    case WM_CREATE:
        SetWindowLong(hwnd,0,0);
        return 0 ;
    case WM_SETFOCUS:
        idfocus = GetWindowLong(hwnd,GWL_ID);
      
    case WM_KILLFOCUS:
        InvalidateRect(hwnd,NULL,TRUE);
        break;
    case WM_PAINT:
        {
            hdc = BeginPaint(hwnd,&ps);
            GetClientRect(hwnd,&rect);
            Rectangle(hdc,0,0,rect.right,rect.bottom);
            // 若为焦点窗口,涂色
            idchild = GetWindowLong(hwnd,GWL_ID); //// 经过测试,子窗口ID正常,
            pt.x = pt.y = 0;        //////但是窗口位置始终相同
            ClientToScreen(hwnd,&pt);//////
            if ( hwnd = GetFocus())
                FillRect(hdc,&rect,(HBRUSH)GetStockObject(GRAY_BRUSH));

            if ( GetWindowLong(hwnd,0) ) // 绘制对角线
            {
                MoveToEx(hdc,0, 0, NULL);
                LineTo(hdc, rect.right, rect.bottom);
                MoveToEx(hdc, rect.right,0, NULL);
                LineTo(hdc, 0, rect.bottom);
            }
           
            EndPaint(hwnd,&ps);
        }      
        break;
    case WM_KEYDOWN:
        if ( wparam != VK_SPACE && wparam != VK_RETURN)
        {
            SendMessage(GetParent(hwnd),message,wparam,lparam);
            return 0 ;
        }
        // 没有break
    case WM_LBUTTONDOWN:
        {
            SetWindowLong(hwnd,0, 1 ^ GetWindowLong(hwnd,0));
            SetFocus(hwnd);
            InvalidateRect(hwnd,NULL,FALSE);
        }
        return 0;
    }
    return DefWindowProc(hwnd,message,wparam,lparam);
}
2 回复
#2
ydown2013-06-08 13:59
程序代码:
        for (int m = 0; m < 5; m++)
        {
            for (int n = 0; n < 5; n++)
                MoveWindow(hchild[m][n], m * xadd, n * yadd, xadd, yadd,TRUE);
        }

将i,j替换成m,n
#3
疯狂的小白2013-06-08 19:48
回复 2楼 ydown
正解
1