| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1711 人关注过本帖
标题:建立25个子窗口平分主窗口,每个子窗口的位置都是一样的,求解!
只看楼主 加入收藏
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
收藏
 问题点数:0 回复次数:2 
建立25个子窗口平分主窗口,每个子窗口的位置都是一样的,求解!
程序代码:
把一个窗口分成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);
}
搜索更多相关主题的帖子: 新建 
2013-05-29 17:05
ydown
Rank: 2
等 级:论坛游民
帖 子:31
专家分:92
注 册:2013-5-27
收藏
得分:0 
程序代码:
        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
2013-06-08 13:59
疯狂的小白
Rank: 2
等 级:论坛游民
帖 子:8
专家分:10
注 册:2013-5-29
收藏
得分:0 
回复 2楼 ydown
正解
2013-06-08 19:48
快速回复:建立25个子窗口平分主窗口,每个子窗口的位置都是一样的,求解!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029693 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved