鱼C论坛

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

我的窗口不见了?

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

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

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

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


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

  2. #define DIVISIONS 5

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

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

  6. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  7. {
  8.         static TCHAR szAppName[] = TEXT("My Windows");
  9.         HWND hwnd;
  10.         MSG msg;
  11.         WNDCLASS wndclass;
  12.        
  13.         //主窗口
  14.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  15.         wndclass.lpfnWndProc = WndProc;
  16.         wndclass.cbClsExtra = 0;
  17.         wndclass.cbWndExtra = 0;
  18.         wndclass.hInstance = hInstance;
  19.         wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
  20.         wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
  21.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  22.         wndclass.lpszMenuName = NULL;
  23.         wndclass.lpszClassName = szAppName;
  24.        
  25.         if (!RegisterClass(&wndclass))
  26.         {
  27.                         MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
  28.                         return 0;
  29.         }
  30.        
  31.         //子窗口
  32.         wndclass.lpfnWndProc = ChildWndProc;
  33.         wndclass.cbWndExtra = sizeof(long);                                //功能彩蛋
  34.         wndclass.hIcon = NULL;
  35.         wndclass.lpszClassName = szChildClass;
  36.        
  37.         RegisterClass(&wndclass);
  38.        
  39.         hwnd = CreateWindow(                                        //创建主窗口
  40.                         szAppName,
  41.                         TEXT("哈哈哈"),
  42.                         WS_OVERLAPPEDWINDOW,
  43.                         CW_USEDEFAULT,
  44.                         CW_USEDEFAULT,
  45.                         CW_USEDEFAULT,
  46.                         CW_USEDEFAULT,
  47.                         NULL,
  48.                         NULL,
  49.                         hInstance,
  50.                         NULL);
  51.        
  52.         ShowWindow(hwnd,iCmdShow);
  53.         UpdateWindow(hwnd);
  54.        
  55.         while (GetMessage(&msg, NULL, 0, 0))
  56.         {
  57.                 TranslateMessage(&msg);
  58.                 DispatchMessage(&msg);
  59.         }

  60.         return msg.wParam;
  61.        
  62. }

  63. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  64. {
  65.         static HWND hwndChild[DIVISIONS][DIVISIONS];
  66.         int cxBlock,cyBlock,x,y;
  67.         switch(message)
  68.         {
  69.                 case WM_CREATE:
  70.                         for(x = 0;x < DIVISIONS;x++)
  71.                         {
  72.                                 for(y = 0;y < DIVISIONS;y++)
  73.                                 {
  74.                                         hwndChild[x][y] = CreateWindow(
  75.                                                                                 szChildClass,                                //
  76.                                                                                 NULL,                                                //标题
  77.                                                                                 WS_CHILDWINDOW | WS_VISIBLE,//类型
  78.                                                                                 0,0,0,0,                                        //初始坐标、尺寸
  79.                                                                                 hwnd,                                                //父窗口句柄
  80.                                                                                 (HMENU)(y << 8 | x),
  81.                                                                                                                 //窗口菜单句柄(每个窗口唯一)
  82.                                                                                 (HINSTANCE)GetWindowLongPtr(hwnd,GWL_HINSTANCE),
  83.                                                                                                                                         //窗口实例句柄
  84.                                                                                 NULL);                                                //创建参数
  85.                                 }
  86.                         }
  87.                         return 0;
  88.                
  89.                 case WM_SIZE:
  90.                         cxBlock = LOWORD(lParam) / DIVISIONS;
  91.                         cyBlock = HIWORD(lParam) / DIVISIONS;
  92.                        
  93.                         for(x = 0;x < DIVISIONS;x++)
  94.                         {
  95.                                 for(y = 0;y < DIVISIONS;y++)
  96.                                 {
  97.                                         MoveWindow(hwndChild[x][y],x * cxBlock,y * cyBlock,
  98.                                                                 cxBlock,cyBlock,TRUE);                //修改窗口尺寸及位置
  99.                                 }
  100.                         }
  101.                
  102.                 case WM_LBUTTONDOWN:
  103.                         MessageBeep(0);
  104.                         return 0;
  105.                
  106.         case WM_DESTROY:
  107.                 PostQuitMessage(0);
  108.                 return 0;
  109.         
  110.         }
  111.        
  112. }

  113. LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  114. {
  115.         HDC hdc;
  116.         PAINTSTRUCT ps;
  117.         RECT rect;
  118.        
  119.         switch(message)
  120.         {
  121.                 case WM_CREATE:
  122.                         SetWindowLongPtr(hwnd,0,0);
  123.                         return 0;
  124.                
  125.                 case WM_LBUTTONDOWN:
  126.                          SetWindowLongPtr(hwnd,0,1 ^ GetWindowLongPtr(hwnd,0));
  127.                          InvalidateRect(hwnd,NULL,FALSE);
  128.                          return 0;
  129.                
  130.                 case WM_PAINT:
  131.                         hdc = BeginPaint(hwnd,&ps);
  132.                        
  133.                         GetClientRect(hwnd,&rect);
  134.                         Rectangle(hwnd,0,0,rect.right,rect.bottom);
  135.                        
  136.                         if(GetWindowLongPtr(hwnd,0))
  137.                         {
  138.                                 MoveToEx(hdc,0,0,NULL);
  139.                                 LineTo(hdc,rect.right,rect.bottom);
  140.                                 MoveToEx(hdc,0,rect.bottom,NULL);
  141.                                 LineTo(hdc,rect.right,0);
  142.                         }
  143.                        
  144.                         EndPaint(hwnd,&ps);
  145.                         return 0;
  146.         }
  147.        
  148.         return DefWindowProc(hwnd,message,wParam,lParam);
  149. }

复制代码
想知道小甲鱼最近在做啥?请访问 -> 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-5-3 07:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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