Windows程序消息机制浅析
1、消息
消息是由MSG结构体来表示的。如下:
typedef struct tagMSG {
HWND hwnd;
UINT message;
WPARAM wParam;
LPARAM lParam;
DWORD time;
POINT pt;
} MSG
2、WinMain函数的定义
WinMain函数的原型声明如下:
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
);
3、窗口的创建:设计一个窗口类、注册窗口类、创建窗口、显示及更新窗口。
设计一个窗口:WNDCLASS结构体的定义如下:
typedef struct _WNDCLASS {
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCTSTR lpszMenuName;
LPCTSTR lpszClassName;
} WNDCLASS;
注册窗口类:注册函数的原型声明如下:
ATOM RegisterClass(
CONST WNDCLASS *lpWndClass // class data
);
创建窗口:CreateWindow函数的原型声明如下:
HWND CreateWindow(
LPCTSTR lpClassName, // registered class name
LPCTSTR lpWindowName, // window name
DWORD dwStyle, // window style
int x, // horizontal position of window
int y, // vertical position of window
int nWidth, // window width
int nHeight, // window height
HWND hWndParent, // handle to parent or owner window
HMENU hMenu, // menu handle or child identifier
HINSTANCE hInstance, // handle to application instance
LPVOID lpParam // window-creation data
);
更新及更新窗口:显示窗口ShowWindow函数的原型声明如下:
BOOL ShowWindow(
HWND hWnd, // handle to window
int nCmdShow // show state
);
更新及更新窗口:更新窗口UpdateWindow函数的原型声明如下:
BOOL UpdateWindow(HWND hWnd);
4、消息循环:不断从消息队列中取出消息,并进行响应。
BOOL GetMessage(
LPMSG lpMsg, // message information
HWND hWnd, // handle to window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax // last message
);
Windows应用程序消息处理机制如下图所示:


操作系统接收到应用程序的窗口消息,将消息投递到应用程序的消息队列中。
应用程序在消息循环中调用GetMessage函数从消息队列中取出一条一条的消息。取出消息后,应用程序可以对消息进行一些预处理,例如,放弃对某些消息的响应,或者调用TranslateMessage产生新的消息。
应用程序调用DispatchMessage,将消息回传给操作系统。消息是由MSG结构体对象来表示的,其中就包含了接收消息的窗口的句柄。因此,DispatchMessage函数总能进行正确的传递。
系统利用WNDCLASS结构体的lpfnWndProc成员保存的窗口过程函数的指针调用窗口过程,对消息进行处理(即“系统给应用程序发送了消息”)。
5、编写窗口过程函数:窗口过程函数的声明形式如下:
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
#include <windows.h>
#include <stdio.h> LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
); int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra=;
wndcls.cbWndExtra=;
wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WinSunProc;
wndcls.lpszClassName="bedrock32";
wndcls.lpszMenuName=NULL;
wndcls.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls); HWND hwnd;
hwnd=CreateWindow("bedrock32","http://www.cnblogs.com/bedrock32",WS_OVERLAPPEDWINDOW,
,,,,NULL,NULL,hInstance,NULL); ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd); MSG msg;
while(GetMessage(&msg,NULL,,))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} LRESULT CALLBACK WinSunProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CHAR:
char szChar[];
sprintf(szChar,"char code is %d",wParam);
MessageBox(hwnd,szChar,"char",);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"mouse clicked","message",);
HDC hdc;
hdc=GetDC(hwnd);
TextOut(hdc,,,"我是高手",strlen("我是高手"));
//ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hDC;
PAINTSTRUCT ps;
hDC=BeginPaint(hwnd,&ps);
TextOut(hDC,,,"http://www.cnblogs.com/bedrock32",strlen("http://www.cnblogs.com/bedrock32"));
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的结束?","message",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage();
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return ;
}

Windows程序消息机制浅析的更多相关文章
- windows程序消息机制(Winform界面更新有关)
windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...
- windows程序消息机制(Winform界面更新有关)--转
1. Windows程序消息机制 Windows GUI程序是基于消息机制的,有个主线程维护着消息泵.这个消息泵让windows程序生生不息. Windows程序有个消息队列,窗体上的所有消息是这个队 ...
- Windows 消息机制浅析
1. Windows 的历史 中国人喜欢以史为鉴,而事实也确实是,如果你能知道一件事情的来龙去脉,往往可以更容易地理解事物为什么会表现为当前这样的现状.所以,我的介绍性开场白通常会以一段历 ...
- GDI及Windows的消息机制
什么是GDI? GDI, Graphics Device Interface GDI在以下位置已经被微软列为Legacy Graphics,不建议使用来开发应用程序(http://msdn.micro ...
- 【笨嘴拙舌WINDOWS】消息机制
如果将WINDOWS比做一个人,那么他就是为你提供各种服务的巫师,他手上有各式各样,奇形怪状的奇葩物品.他脑子充满了智慧,能够为你解决你所不能解决的疑难杂症.但是他不认识你! 你从小立志要想考状元,去 ...
- C#中的WinForm的消息机制简述,及消息机制下Invoke,和BeginInvoke的使用和区别
在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate,至于委托的本质请参考我的另一随笔:对.net事件的看法. 一.为什么Control类提供了Invoke和Begin ...
- 基础篇-Windows消息机制
1在介绍Windows 消息运行机制之前,首先介绍一下消息的概念: 消息(Message)指的就是Windows 操作系统发给应用程序的一个通告[5],它告诉应用程序某个特定的事件发生了.比如,用户单 ...
- <Win32_1>深入浅出windows消息机制[转自crocodile_]
上学期学习了Java ,感觉Java写一个窗口真心简单,很易上手,也就难怪很多开发人员选择Java作为自己的开发编程语言.但是由于自身对windows的热爱,让我觉得c.c++语言才是我亲睐的编程语言 ...
- 深入Delphi -- Windows 消息机制
http://www.txsz.net/xs/delphi/3/Windows%20%E6%B6%88%E6%81%AF%E6%9C%BA%E5%88%B6.htm Windows 消息机制 by m ...
随机推荐
- iPhone开发 Swift - NSNotification 通知
Swift创建Notification通知 创建一个SingleView Application 打开AppDelegate.swift,在方法 application(application:UIA ...
- Git之忽略文件(ignore file)
1. 环境 Windows XP SP3 + TortoiseGit + msysGit 2. ignore files的三种方法 以下涉及的ignore文件均为如下格式: # 以'#'开 ...
- spring三大核心学习(一)---控制反转
记得当年大学时候,java的企业级框架还是ssh的天下(spring,struts和hibernate),但是现在,感觉spring已经完全把那两个框架甩在后边了.用spring的人越来越多,用str ...
- PHP利用GD库画曲线
效果: PHP代码 <?php Header('Content-type: image/png;Charset:utf-8'); //声明图片 $im = imagecreate(400,200 ...
- Part 17 Temporary tables in SQL Server
Temporary tables in SQL Server
- 别人网站生成的json
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sb.ToString()); WebResponse rep = req.GetResp ...
- SQL 存储过程 执行效率优化提升 (显示估计)
在sql server 查询区 通过执行 "显示估计的执行计划" ,可以给出 存储过程 缺少索引的提示,并自动生成建立相应 索引 的代码 显示估计的执行计划 执行结果提示: 哦,算 ...
- apache windowns 下wamp配置多站点的问题
1.多站点配置找到apache下面的 conf/httpd.conf # Virtual hostsInclude conf/extra/httpd-vhosts.conf //将这句前面的#号注释 ...
- 配置PostgreSQL Streaming Replication集群
运行环境: Primary: 192.168.0.11 Standby: 192.168.0.21, 192.168.0.22 OS: CentOS 6.2 PostgreSQL: 9.1.2 版本以 ...
- 采用Service实现本地推送通知
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...