当前位置: 首页 > news >正文

Page 251~254 Win32 GUI项目

win32_gui  源代码:

#if defined(UNICODE) && !defined(_UNICODE)#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)#define UNICODE
#endif#include <tchar.h>
#include <windows.h>/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
{HWND hwnd;               /* This is the handle for our window */MSG messages;            /* Here messages to the application are saved */WNDCLASSEX wincl;        /* Data structure for the windowclass *//* The Window structure */wincl.hInstance = hThisInstance;wincl.lpszClassName = szClassName;wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */wincl.cbSize = sizeof (WNDCLASSEX);/* Use default icon and mouse-pointer */wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);wincl.hCursor = LoadCursor (NULL, IDC_ARROW);wincl.lpszMenuName = NULL;                 /* No menu */wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */wincl.cbWndExtra = 0;                      /* structure or the window instance *//* Use Windows's default colour as the background of the window */wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;/* Register the window class, and if it fails quit the program */if (!RegisterClassEx (&wincl))return 0;/* The class is registered, let's create the program*/hwnd = CreateWindowEx (0,                   /* Extended possibilites for variation */szClassName,         /* Classname */_T("Code::Blocks Template Windows App"),       /* Title Text */WS_OVERLAPPEDWINDOW, /* default window */CW_USEDEFAULT,       /* Windows decides the position */CW_USEDEFAULT,       /* where the window ends up on the screen */544,                 /* The programs width */375,                 /* and height in pixels */HWND_DESKTOP,        /* The window is a child-window to desktop */NULL,                /* No menu */hThisInstance,       /* Program Instance handler */NULL                 /* No Window Creation data */);/* Make the window visible on the screen */ShowWindow (hwnd, nCmdShow);/* Run the message loop. It will run until GetMessage() returns 0 */while (GetMessage (&messages, NULL, 0, 0)){/* Translate virtual-key messages into character messages */TranslateMessage(&messages);/* Send message to WindowProcedure */DispatchMessage(&messages);}/* The program return-value is 0 - The value that PostQuitMessage() gave */return messages.wParam;
}/*  This function is called by the Windows function DispatchMessage()  */LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{switch (message)                  /* handle the messages */{case WM_DESTROY:PostQuitMessage (0);       /* send a WM_QUIT to the message queue */break;default:                      /* for messages that we don't deal with */return DefWindowProc (hwnd, message, wParam, lParam);}return 0;
}

11行,窗口过程(回调函数),实际上就是窗口用于处理消息的过程,返回值的类型是一个宏定义,即LRESULT

14行,使用全局变量定义一个类名字

21,声明窗口句柄

22,声明窗口消息

23,声明窗口类结构体,数据结构

26~40,填充窗口结构体的成员数据

43,向操作系统注册这个窗口类,其作用相当于告诉操作系统:“嗨哥们,将来我要是用“CodeBlocksWindowsApp“命名来创建一个窗口 ,你就把这个窗口的消息,都传给WindowProcedure这个函数。

47~60,创建一个窗口,使用变量hwnd保存,hwnd声明在21行,窗口创建成功之后,只是内存中的一堆数据,并不会立即显示在屏幕上,屏幕上的显示不过是表象。

63,将指定句柄的窗口显示出来

66~72,  消息循环。

66,将消息从消息队列中取出来,存放到messages中,

69,对消息做一些必要的转换

71,将消息发送到目标窗口,发送给窗口过程函数。

操作系统中有好多GUI程序(更专业的叫法是“进程process”)在运行,每个进程都需要接受消息。消息往往很多,所以进程需要一个“消息队列(message queue)”。GetMessage()从队列中取出一个消息,存放在message中,然后通过TranslateMessage(&messages)做一些必要的转换,最后通过DispatchMessage(&messages)将该消息发送到目标窗口

81~93,窗口过程函数,

85,如果收到的消息是WM_DESTROY, 调用PostQuitMessage (0),该操作将创建另一个“WM_QUIT”并将其丢进消息队列等着,直到GetMessage()将排在前面的消息取出并处理完后遇上它。

93,DefWindowProc()方法也是一个Windows API提供的“默认窗口过程”,可以处理窗口最大化,最小化,退出(前面提到的WM_QUIT)等消息。

WindowProcedure()函数的四个入参含义

表11-1 窗口过程函数的四个入参解释
参数含义
HWND  hwnd窗口句柄,代表接收到消息的窗口
UINT messageUINT是unsigned int,用无符号整数表示接收到消息的类型
WPARAM wParam

消息的附加消息之一

比如:对一个鼠标移动的消息,wParam用于表达在鼠标移动的同时,用户是否也按下了一些特殊的键

LPARAM lParam

消息的附加消息之二

比如:对一个鼠标移动的消息,lParam存储有鼠标当前的位置坐标

课堂作业:代码中CreateWindowEx()调用,并没有用到wincl这个数据,请思考wincl如何起作用?

        43行,wincl注册到系统里,47行,调用CreateWindowEx(), 从系统中取出wincl注册到系统中的数据,然后创建窗口

http://www.lryc.cn/news/278197.html

相关文章:

  • Kafka(七)可靠性
  • Spring Data JPA入门到放弃
  • MES系统数据采集的几种方式
  • 铭文 LaunchPad 平台 Solmash 推出早鸟激励计划
  • 【前端规范】
  • 12、JVM高频面试题
  • 【Docker】Docker安装入门教程及基本使用
  • 语义解析:如何基于SQL去实现自然语言与机器智能连接的桥梁
  • Java项目:117SpringBoot动漫论坛网站
  • Jenkins基础篇--添加节点
  • 【C++】手撕 list类(包含迭代器)
  • @Autowired 和 @Resource 的区别是什么?
  • 栈和排序.
  • springboot 多数据源怎么配置在控制台的sql打印日志
  • 【WinForms 窗体】常见的“陷阱”
  • Android readelf 工具查找函数符号
  • MySQL-索引回顾
  • 重新认识Elasticsearch-一体化矢量搜索引擎
  • 【附源码】基于SSM框架的房屋租赁系统的设计与实现
  • [SpringBoot]如何在一个普通类中获取一个Bean
  • [ERROR] 不再支持目标选项 5。请使用 7 或更高版本
  • EasyMR:为 AI 未来赋能,打造弹性大数据引擎的革命
  • C //练习 4-10 另一种方法是通过getline函数读入整个输入行,这种情况下可以不使用getch与ungetch函数。请运用这一方法修改计算器程序。
  • 竞赛保研 基于深度学习的行人重识别(person reid)
  • Ncast盈可视 高清智能录播系统 IPSetup.php信息泄露+RCE漏洞复现(CVE-2024-0305)
  • GO语言Context的作用
  • 金和OA C6 upload_json 任意文件上传漏洞
  • 大模型学习第四课
  • Code Runner使用外部控制台,运行结束后等待用户输入
  • IC设计的前端和后端是如何区分的?