分享
 
 
 

Introduction to Messages as to vc++

王朝vc·作者佚名  2006-01-30
窄屏简体版  字體: |||超大  

The computer is a machine that only follows

instructions. It almost doesn't know anything. Because of this, the

computer cannot predict what a user wants to do with the computer. In

fact, a great deal of the responsibility is left to the programmer who

must decide what can and cannot or should not be done on an application. To help

the users with computer interaction, the operating system provides a

series of objects called Windows controls. The programmer decides what

objects are necessary for a given application.

Each computer application is equipped with Windows

controls that allow the user to interact with the computer. Because the

computer cannot and would not predict what the user wants to do when using

the computer, the operating system lets each object tell it when it needs

something from Windows. To do this, a control sends a message to

the operating system every time something is new. Because there can be so many

messages a control can send and because many controls can send various

messages, there is a formula each message or almost every one of them must

follow, just like there are rules the post office wants you to follow in

order to send a letter.

A message to Windows must provide four pieces of

information:

WHO sent the message? Every object you will need in your program,

just like everything in the computer, must have a name. The operating

system needs this name to identify every object, for any reason. An

object in Microsoft Windows is identified as a Handle. For Windows

controls, the handle is called HWNDWHAT message? The object that sends a message must let the operating

system know what message it is sending. As we will learn, there are

various types of messages for different circumstances. Nevertheless,

to make matters a little easier, each message is a constant positive

natural number (unsigned int) identified with a particular name.

Therefore, the message identifier is passed as UINTAccompanying items: Because there are so many types of messages, you

must provide two additional pieces of information to help process the

message. These two items depend on the type of message and could be

anything. The first accompanying item is a 32-bit type (unsigned int)

called WPARAM (stands for WORD Parameter; in other

words, it is a WORD (unsigned int) argument). The second accompanying item is a 32-bit type

of value (long) calle LPARAM (stands for LONG Parameter; in

other words, it is a LONG (long in C/C++) argument). Remember that these two can be different things

for different messages.

To manage the messages sent to Windows, they are

communicated through a function pointer called a Windows procedure. The

name of the function is not important but it must return a 32-bit integer,

in fact a C/C++ long or Win32 LONG.

Therefore, it is declared as LRESULT (LONG Result). Because this is a function pointer,

it must be declared and defined as CALLBACK. The messages can

be carried in a function defined as follows:

LRESULT CALLBACK MessageProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

To process the messages, and because there can be so

many of them, this function typically uses a switch control to list

all necessary messages and process each one in turn. After processing a

message, its case must return a value indicating that the message was

successfully processed or not.

No matter how many messages you processed, there will

still be messages that you did not deal with. It could be because they

were not sent even though they are part of the Windows controls used on an

application. If you didn't process some messages, you should/must let the

operating system know so it can take over. What happens is that the

operating system is aware of all messages and it has a default behavior or

processing for each one of them. Therefore, you should/must return a value

for this to happen. The value returned can be placed in the default

section of the switch condition and must simply be a DefWindowProc()

function. Its syntax is:

LRESULT DefWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

This function is returned to Windows, saying

"There are messages I couldn't process. Do what you want with

them". The operating system would simply apply a default processing

to them. The values returned by the DefWindowProc() function should be the

same passed to the procedure.

The most basic message you can process is to make sure

a user can close a window after using it. This can be done with a function

called PostQuitMessage(). Its syntax is:

VOID PostQuitMessage(int nExitCode)

This function takes one argument which is the value of

the LPARAM argument. To close a window, you can pass the argument as

WM_QUIT.

Based on this, a simple Windows procedure can be

defined as follows:

LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

{

switch(Msg)

{

case WM_DESTROY:

PostQuitMessage(WM_QUIT);

break;

default:

return DefWindowProc(hWnd, Msg, wParam, lParam);

}

return 0;

}

<img>

A basic program with one message can be written as

follows:

//---------------------------------------------------------------------------

#include <windows.h>

//---------------------------------------------------------------------------

HWND hWnd;

const char ClsName[] = "WndMsg";

const char WindowCaption[] = "Windows and Controls Messages";

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

//---------------------------------------------------------------------------

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow)

{

MSG Msg;

WNDCLASSEX WndClsEx;

WndClsEx.cbSize = sizeof(WNDCLASSEX);

WndClsEx.style = CS_HREDRAW | CS_VREDRAW;

WndClsEx.lpfnWndProc = WndProc;

WndClsEx.cbClsExtra = NULL;

WndClsEx.cbWndExtra = NULL;

WndClsEx.hInstance = hInstance;

WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);

WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);

WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

WndClsEx.lpszMenuName = NULL;

WndClsEx.lpszClassName = ClsName;

WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

RegisterClassEx(&WndClsEx);

hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,

ClsName,

WindowCaption,

WS_OVERLAPPEDWINDOW,

100,

120,

640,

480,

NULL,

NULL,

hInstance,

NULL);

ShowWindow(hWnd, nCmdShow);

UpdateWindow(hWnd);

while( GetMessage(&Msg, NULL, 0, 0) )

{

TranslateMessage(&Msg);

DispatchMessage(&Msg);

}

return Msg.wParam;

}

//---------------------------------------------------------------------------

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)

{

switch(Msg)

{

case WM_DESTROY:

PostQuitMessage(WM_QUIT);

break;

default:

return DefWindowProc(hWnd, Msg, wParam, lParam);

}

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有