当前位置: 首页 > 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注册到系统中的数据,然后创建窗口

相关文章:

  • 使用MATLAB连接USRP
  • 6、C语言:输入与输出
  • [学习笔记]刘知远团队大模型技术与交叉应用L1-NLPBig Model Basics
  • 常见设计模式--通俗易懂版
  • 使用Spring Boot集成中间件:Elasticsearch基础->提高篇
  • [力扣 Hot100]Day2 字母异位词分组
  • springCould中的Bus-从小白开始【11】
  • 数据库管理-第130期 JSON二元性(20240109)
  • 【Java SE语法篇】9.抽象类和接口
  • BC28 大小写转换
  • SQL DML
  • git 上传小知识
  • vulkan中的指令队列的大致原理
  • EasyExcel简单实例
  • Java基础-常量,变量,数据类型-笔记
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 【跃迁之路】【585天】程序员高效学习方法论探索系列(实验阶段342-2018.09.13)...
  • CAP 一致性协议及应用解析
  • ECS应用管理最佳实践
  • exports和module.exports
  • GraphQL学习过程应该是这样的
  • IE报vuex requires a Promise polyfill in this browser问题解决
  • Iterator 和 for...of 循环
  • JavaScript新鲜事·第5期
  • Java比较器对数组,集合排序
  • Java多态
  • niucms就是以城市为分割单位,在上面 小区/乡村/同城论坛+58+团购
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • 线上 python http server profile 实践
  • 字符串匹配基础上
  • 正则表达式-基础知识Review
  • ​2021半年盘点,不想你错过的重磅新书
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • ​linux启动进程的方式
  • #1014 : Trie树
  • #ifdef 的技巧用法
  • #多叉树深度遍历_结合深度学习的视频编码方法--帧内预测
  • (10)STL算法之搜索(二) 二分查找
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (Matlab)使用竞争神经网络实现数据聚类
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (附源码)springboot美食分享系统 毕业设计 612231
  • (附源码)计算机毕业设计SSM智能化管理的仓库管理
  • (附源码)计算机毕业设计高校学生选课系统
  • (十七)devops持续集成开发——使用jenkins流水线pipeline方式发布一个微服务项目
  • (转)Oracle存储过程编写经验和优化措施
  • (转载)虚幻引擎3--【UnrealScript教程】章节一:20.location和rotation
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .jks文件(JAVA KeyStore)
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .NET开发人员必知的八个网站