鱼C论坛

 找回密码
 立即注册
查看: 2262|回复: 3

我的窗口不见了?

[复制链接]
发表于 2019-9-3 17:53:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我跟着教程视频敲了一遍代码(Windows SDK里的Checker3),结果运行时发现窗口没有显示出来,只是在下方菜单里出现了窗口图标。想请问大佬们这是什么原因呢?该怎么解决呢


如果需要代码的话
#include <windows.h>

#define DIVISIONS 5

TCHAR szChildClass[] = TEXT("CHECKER3_CHILD");                        //子窗口类名 

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT("My Windows");
        HWND hwnd;
        MSG msg;
        WNDCLASS wndclass;
        
        //主窗口 
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc = WndProc;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance = hInstance;
        wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
        wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName = NULL;
        wndclass.lpszClassName = szAppName; 
        
        if (!RegisterClass(&wndclass))
        {
                        MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
                        return 0;
        }
        
        //子窗口 
        wndclass.lpfnWndProc = ChildWndProc;
        wndclass.cbWndExtra = sizeof(long);                                //功能彩蛋
        wndclass.hIcon = NULL;
        wndclass.lpszClassName = szChildClass;
        
        RegisterClass(&wndclass);
        
        hwnd = CreateWindow(                                        //创建主窗口 
                        szAppName,
                        TEXT("哈哈哈"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
        
        ShowWindow(hwnd,iCmdShow);
        UpdateWindow(hwnd);
        
        while (GetMessage(&msg, NULL, 0, 0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }

        return msg.wParam;
        
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        static HWND hwndChild[DIVISIONS][DIVISIONS];
        int cxBlock,cyBlock,x,y;
        switch(message)
        {
                case WM_CREATE:
                        for(x = 0;x < DIVISIONS;x++)
                        {
                                for(y = 0;y < DIVISIONS;y++)
                                {
                                        hwndChild[x][y] = CreateWindow(
                                                                                szChildClass,                                //
                                                                                NULL,                                                //标题 
                                                                                WS_CHILDWINDOW | WS_VISIBLE,//类型 
                                                                                0,0,0,0,                                        //初始坐标、尺寸 
                                                                                hwnd,                                                //父窗口句柄
                                                                                (HMENU)(y << 8 | x),
                                                                                                                //窗口菜单句柄(每个窗口唯一) 
                                                                                (HINSTANCE)GetWindowLongPtr(hwnd,GWL_HINSTANCE),
                                                                                                                                        //窗口实例句柄
                                                                                NULL);                                                //创建参数
                                }
                        }
                        return 0;
                
                case WM_SIZE:
                        cxBlock = LOWORD(lParam) / DIVISIONS;
                        cyBlock = HIWORD(lParam) / DIVISIONS;
                        
                        for(x = 0;x < DIVISIONS;x++)
                        {
                                for(y = 0;y < DIVISIONS;y++)
                                {
                                        MoveWindow(hwndChild[x][y],x * cxBlock,y * cyBlock,
                                                                cxBlock,cyBlock,TRUE);                //修改窗口尺寸及位置 
                                }
                        }
                
                case WM_LBUTTONDOWN:
                        MessageBeep(0);
                        return 0;
                
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        
        }
        
}

LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        
        switch(message)
        {
                case WM_CREATE:
                        SetWindowLongPtr(hwnd,0,0);
                        return 0;
                
                case WM_LBUTTONDOWN:
                         SetWindowLongPtr(hwnd,0,1 ^ GetWindowLongPtr(hwnd,0));
                         InvalidateRect(hwnd,NULL,FALSE);
                         return 0;
                
                case WM_PAINT:
                        hdc = BeginPaint(hwnd,&ps);
                        
                        GetClientRect(hwnd,&rect);
                        Rectangle(hwnd,0,0,rect.right,rect.bottom);
                        
                        if(GetWindowLongPtr(hwnd,0))
                        {
                                MoveToEx(hdc,0,0,NULL);
                                LineTo(hdc,rect.right,rect.bottom);
                                MoveToEx(hdc,0,rect.bottom,NULL);
                                LineTo(hdc,rect.right,0);
                        }
                        
                        EndPaint(hwnd,&ps);
                        return 0;
        }
        
        return DefWindowProc(hwnd,message,wParam,lParam);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-3 23:28:06 | 显示全部楼层
本帖最后由 superbe 于 2019-9-3 23:30 编辑

有几个地方打错了,见下面红色行

#include <windows.h>

#define DIVISIONS 5

TCHAR szChildClass[] = TEXT("CHECKER3_CHILD");                        //子窗口类名

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT("My Windows");
        HWND hwnd;
        MSG msg;
        WNDCLASS wndclass;
        
        //主窗口
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc = WndProc;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance = hInstance;
        wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
        wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wndclass.lpszMenuName = NULL;
        wndclass.lpszClassName = szAppName;
        
        if (!RegisterClass(&wndclass))
        {
                        MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
                        return 0;
        }
        
        //子窗口
        wndclass.lpfnWndProc = ChildWndProc;
        wndclass.cbWndExtra = sizeof(long);                                //功能彩蛋
        wndclass.hIcon = NULL;
        wndclass.lpszClassName = szChildClass;
        
        RegisterClass(&wndclass);
        
        hwnd = CreateWindow(                                        //创建主窗口
                        szAppName,
                        TEXT("哈哈哈"),
                        WS_OVERLAPPEDWINDOW,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        CW_USEDEFAULT,
                        NULL,
                        NULL,
                        hInstance,
                        NULL);
        
        ShowWindow(hwnd,iCmdShow);
        UpdateWindow(hwnd);
        
        while (GetMessage(&msg, NULL, 0, 0))
        {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }

        return msg.wParam;
        
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        static HWND hwndChild[DIVISIONS][DIVISIONS];
        int cxBlock,cyBlock,x,y;
        switch(message)
        {
                case WM_CREATE:
                        for(x = 0;x < DIVISIONS;x++)
                        {
                                for(y = 0;y < DIVISIONS;y++)
                                {
                                        hwndChild[x][y] = CreateWindow(
                                                                                szChildClass,                                //
                                                                                NULL,                                                //标题
                                                                                WS_CHILDWINDOW | WS_VISIBLE,//类型
                                                                                0,0,0,0,                                        //初始坐标、尺寸
                                                                                hwnd,                                                //父窗口句柄
                                                                                (HMENU)(y << 8 | x),
                                                                                                                //窗口菜单句柄(每个窗口唯一)
                                                                                (HINSTANCE)GetWindowLongPtr(hwnd,GWL_HINSTANCE),
                                                                                                                                        //窗口实例句柄
                                                                                NULL);                                                //创建参数
                                }
                        }
                        return 0;
               
                case WM_SIZE:
                        cxBlock = LOWORD(lParam) / DIVISIONS;
                        cyBlock = HIWORD(lParam) / DIVISIONS;
                        
                        for(x = 0;x < DIVISIONS;x++)
                        {
                                for(y = 0;y < DIVISIONS;y++)
                                {
                                        MoveWindow(hwndChild[x][y],x * cxBlock,y * cyBlock,
                                                                cxBlock,cyBlock,TRUE);                //修改窗口尺寸及位置
                                }
                        }
                        return 0 ;    //添加这行
                case WM_LBUTTONDOWN:
                        MessageBeep(0);
                        return 0;
               
        case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
        
        }
        return DefWindowProc (hwnd, message, wParam, lParam);    //添加这行
}

LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        HDC hdc;
        PAINTSTRUCT ps;
        RECT rect;
        
        switch(message)
        {
                case WM_CREATE:
                        SetWindowLongPtr(hwnd,0,0);
                        return 0;
               
                case WM_LBUTTONDOWN:
                         SetWindowLongPtr(hwnd,0,1 ^ GetWindowLongPtr(hwnd,0));
                         InvalidateRect(hwnd,NULL,FALSE);
                         return 0;
               
                case WM_PAINT:
                        hdc = BeginPaint(hwnd,&ps);
                        
                        GetClientRect(hwnd,&rect);
                        Rectangle(hdc,0,0,rect.right,rect.bottom);    //hwnd改为hdc
                        
                        if(GetWindowLongPtr(hwnd,0))
                        {
                                MoveToEx(hdc,0,0,NULL);
                                LineTo(hdc,rect.right,rect.bottom);
                                MoveToEx(hdc,0,rect.bottom,NULL);
                                LineTo(hdc,rect.right,0);
                        }
                        
                        EndPaint(hwnd,&ps);
                        return 0;
        }
        
        return DefWindowProc(hwnd,message,wParam,lParam);
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-7 12:43:05 | 显示全部楼层
superbe 发表于 2019-9-3 23:28
有几个地方打错了,见下面红色行

#include

感谢大佬,是我太菜了(悲)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-16 09:47:50 | 显示全部楼层
楼主,问题解决了,请结帖!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-14 18:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表