没有合适的资源?快使用搜索试试~ 我知道了~
首页MFC框架原理以及消息运行机制
MFC框架原理以及消息运行机制
5星 · 超过95%的资源 需积分: 9 19 下载量 115 浏览量
更新于2023-07-10
评论 1
收藏 135KB DOC 举报
很好很强大,将MFC框架原理以及消息运行机制讲解的十分透彻,让你有一种顿悟的感觉,千万别犹豫!!!
资源详情
资源评论
资源推荐
MFC 框架原理以及消息运行机制 收藏
(1)Windows 程序内部运行机制
1,windows 程序设计是种事件驱动方式的程序设计,主要基于消息的。当用户需要完成某
种功能时,需要调用 OS 某种支持,然后 OS 将用户的需要包装成消息,并投入到消息队列
中,最后应用程序从消息队列中取走消息并进行响应。
2,消息结构:
typedef struct tagMSG { // msg
HWND hwnd; //接收消息的窗口句柄。和哪个窗口相关联。
UINT message; //消息标识。消息本身是什么。
WPARAM wParam; //消息的附加信息。具体取决于消息本身。
LPARAM lParam;
DWORD time; //消息投递时间。
POINT pt; //消息投递时,光标在屏幕上的位置。
} MSG;
3,消息队列:
每个应用程序 OS 都为它建立一个消息队列,消息队列是个先进先出的缓冲区,其中每个
元素都是一个消息,OS 将生成的每个消息按先后顺序放进消息队列中,应用程序总是取走
当前消息队列中的第一条消息,应用程序取走消息后便知道用户的操作和程序的状态,然
后对其处理即消息响应,消息响应通过编码实现。
4,使用 VC 编程除了良好的 C 基础外还需要掌握两方面:
一,消息本身。不同消息所代表的用户操作和应用程序的状态。
二,对于某个特定的消息来说,要让 OS 执行某个特定的功能去响应消息。
5,Window 程序入口:
int WINAPI WinMain(
HINSTANCE hInstance, // 当前事例句柄。
HINSTANCE hPrevInstance, // 先前事例句柄。
LPSTR lpCmdLine, // 命令行指针
int nCmdShow // (窗口)显示的状态
);
说明:WinMain 函数是 Windows 程序入口点函数,由 OS 调用,当 OS 启动应用程序的时候,
winmain 函数的参数由 OS 传递的。
6,创建一个完整的窗口需要经过下面四个操作步骤:
一,设计一个窗口类;如:WNDCLASS wndcls;
二,注册窗口类; 如:RegisterClass(&wndcls);
三,创建窗口; 如:CreateWindow(),CreateWindowEX();
四,显示及更新窗口。如:ShowWindow(),UpdateWindow();
说明:创建窗口的时候一定要基于已经注册的窗口类.
7,Windows 提供的窗口类:
typedef struct _WNDCLASS {
UINT style; //窗口的类型
WNDPROC lpfnWndProc; //窗口过程函数指针(回调函数)
int cbClsExtra; //窗口类附加字节,为该类窗口所共享。通常 0。
int cbWndExtra; //窗口附加字节。通常设为 0。
HANDLE hInstance; //当前应用程序事例句柄。
HICON hIcon; //图标句柄 LoadIcon();
HCURSOR hCursor; //光标句柄 LoadCursor();
HBRUSH hbrBackground; //画刷句柄 (HBRUSH)GetStockObject();
LPCTSTR lpszMenuName; //菜单名字
LPCTSTR lpszClassName; //类的名字
} WNDCLASS;
8,窗口类注册:
ATOM RegisterClass(
CONST WNDCLASS *lpWndClass // address of structure with class
// data
);
9,创建窗口:
HWND CreateWindow(
LPCTSTR lpClassName, // pointer to registered class name
LPCTSTR lpWindowName, // pointer to 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, // handle to menu or child-window identifier
HANDLE hInstance, // handle to application instance
LPVOID lpParam // pointer to window-creation data
);
10,显示和更新窗口窗口:
BOOL ShowWindow(
HWND hWnd, // handle to window
int nCmdShow // show state of window
);
BOOL UpdateWindow(
HWND hWnd // handle of window
);
11,消息循环:
MSG msg;
while(GetMessage(&msg,...)) //从消息队列中取出一条消息
{
TranslateMessage(&msg); //进行消息(如键盘消息)转换
DispatchMessage(&msg); //分派消息到窗口的回调函数处理,(OS 调用窗口回调函数进行处
理)。
}
其中:
//**The GetMessage function retrieves a message from the calling thread's message queue and
places it in the specified structure.
//**If the function retrieves a message other than WM_QUIT, the return value is nonzero.If the
function retrieves the WM_QUIT message, the return value is zero. If there is an error, the return
value is -1.
BOOL GetMessage(
LPMSG lpMsg, // address of structure with message
HWND hWnd, // handle of window
UINT wMsgFilterMin, // first message
UINT wMsgFilterMax // last message
);
//The TranslateMessage function translates virtual-key messages into character messages. The
character messages are posted to the calling thread's message queue, to be read the next time the
thread calls the GetMessage or PeekMessage function.
BOOL TranslateMessage(
CONST MSG *lpMsg // address of structure with message
);
//The DispatchMessage function dispatches a message to a window procedure.
LONG DispatchMessage(
CONST MSG *lpmsg // pointer to structure with message
);
12,窗口过程函数(回调函数)原型:
The WindowProc function is an application-defined function that processes messages sent to a
window. The WNDPROC type defines a pointer to this callback function. WindowProc is a
placeholder(占位符) for the application-defined function name.
LRESULT CALLBACK WindowProc( //这里 WindowProc 是个代号名字。
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
说明:两种函数调用约定(__stdcall 和 __cdecl):
#define CALLBACK __stdcall
//__stdcall 标准调用预定,是 PASCAL 调用约定,象 DELPHI 使用的就是标准调用约定
#define WINAPIV __cdecl
// __cdecl 是 C 语言形式的调用约定。
主要区别:函数参数传递顺序和对堆栈的清除上。
问题:除了那些可变参数的函数调用外,其余的一般都是__stdcall 约定。但 C/C++编译默
然的是__cdecl 约定。所以如果在 VC 等环境中调用__stdcall 约定的函数,必须要在函数声
明的时加上 __stdcall 修饰符 , 以便对这个函数 的 调 用是使用 __stdcall 约定(如使用
DELPHI 编写的 DLL 时候)。
(VC 中可通过这途径修改:project|settings..|c/c++|...)
在窗口过程函数中通过一组 switch 语句来对消息进行处理:
如:
LRESULT CALLBACK WindowProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(uMsg)
{
case WM_PAINT:
...
break;
case ...
break;
case WM_CLOSE:
//DestroyWindow(hwnd);
//销毁窗口,并发送 WM_DESTROY 消息。
break;
case WM_DESTROY:
//PostQuitMessage(0);
//发送 WM_QUIT 消息到消息队列中,请求终止。
//GetMessage()取到 WM_QUIT 消息后,返回 0,退出消息循 // 环,从而终止
应用程序。
break;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
//用缺省的窗口过程处理我们不感兴趣的消息(其它消息)。
//这是必须的。
}//switch
return 0;
}//WindowProc
13,DestroyWindow()函数和 PostQuitMessage()函数原型:
//**The DestroyWindow function destroys the specified window. The function sends
WM_DESTROY and WM_NCDESTROY messages。
BOOL DestroyWindow(
HWND hWnd // handle to window to destroy
);
//**The PostQuitMessage function indicates to the system that a thread has made a request to
terminate (quit). It is typically used in response to a WM_DESTROY message.
//**The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and
returns immediately; the function simply indicates(预示,通知) to the system that the thread is
requesting to quit at some time in the future.
When the thread retrieves the WM_QUIT message from its message queue, it should exit its
message loop and return control to the system.
VOID PostQuitMessage(
int nExitCode // exit code
);
14,关于 DC 句柄获取:
a)使用 BeginPaint(),EndPaint()对。注意只能在响应 WM_PAINT 消息时使用。
b)使用 GetDc(),ReleaseDC()对。注意他们不能在响应 WM_PAINT 中使用。
(2)C++
1,c 语言中,结构体 struct 中不能包括函数的,而在 C++中 struct 中可以包括函数。
2,C++中结构体和类可以通用,区别主要表现在访问控制方面:struct 中默认是 public,而
class 中默认的是 private。
3,构造函数最重要的作用是创建对象的本身,C++中每个类可以拥有多个构造函数,但必
须至少有一个构造函数,当一个类中没有显式提供任何构造函数,C++编辑器自动提供一
个默认的不带参数的构造函数,这个默认的构造函数只负责构造对象,不做任何初始化工
作。但在一个类中只要自己定义一个构造函数,不管带参不带参,编辑器不再提供默认的
不带参的构造函数了。构造函数没有返回值。
4,析构函数当一个对象生命周期结束时候被调用来回收对象占用的内存空间。一个类只需
有一个析构函数。析构函数没有返回值也不的带参数。
剩余22页未读,继续阅读
helinbo111
- 粉丝: 2
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论3