孙鑫VC++讲座笔记

来源:计算机等级考试    发布时间:2012-08-29    计算机等级考试视频    评论




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中使用。

上一页123下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答