What is a Message?
什么是消息?
A message is an integer value.
消息是一个整型数据.
They are used to communicate pretty much everything in windows at least on basic levels.
在窗口里,最起码在底层,他们被用在交流差不多所有的事件.
Each message have two parameters, wParam and lParam ,but they don't have to be used. In Win32 wParam and lParam are both 32 bit.
每个消息都有两个参数,wParam和lParam.但不一定都要用.在Win32里,wParam和lParam都是32位.
To send a message you can use PostMessage() or SendMessage().
你可以用PostMessage()或SendMessage()来发送消息.
PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
it puts the message into the Message Queue and returns immediatly. That means when it is done the message may or may not have been processed yet.
它把消息放到消息队列并马上返回.这是指,当它完成时,消息可能还没被执行.
SendMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam) it sends the message directly to the window and does not return untill the window has finished processing it.
它把消息直接发送到窗口并等到窗口完成了消息后再返回.
What is the Message Queue?
什么是消息队列?
Lets say you were busy handling the WM_PAINT message and suddenly the user types a bunch of stuff on the keyboard. Whatshould happen? Should you be interrupted in your drawing to handle the keys or should the keys just be discarded? Wrong! Obviously neither of these options is reasonable, so we have the message queue, when messages are posted they are added to the message queue and when you handle them they are removed. This ensure that you aren't going to miss messages.
假设你是窗口,正忙于操作WM_PAINT消息.突然用户输入一大串字符.什么事会发生呢?你会打断你的画图去处理键盘的输入,还是无视掉键盘的输入呢?错了!明显两种选择都是不合理的.因此我们有消息队列.当收到消息时,消息被加到消息队列里.当你处理了它们后,它们就移除.这样就确保你在处理一条消息时,不会丢失其他消息.
What is a Message Loop?
什么是消息循环?
while(GetMessage(&Msg,NULL,0,0)>0){
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
1.The message loop calls GetMessage(), which looks in your message queue. If the message queue is empty your program basically stops and waits for one(it Blocks).
1.消息循环调用查看消息队列的GetMessage().如果消息队列是空的,程序基本上停止并等待(它就堵塞).
2.When an event occures causing a message to be added to the queue (for example the system registers a mouse click). GetMessages() returns a positive value indicating there is a message to be processed, and that it has filled in the members of the MSG structure we passed it. It returns 0 if it hits WM_QUIT, and -1 if an error occured.
2.当事件发生造成了消息被添加到队列(例如登记系统点击了一下鼠标).GetMessages()返回一个大于0的价值,说明有一个消息需要被处理,并MSG结构中空缺的成员.如果它遇到WM_QUIT,它就返回0.如果发生了错误就是-1。
3.We take the message (in the Msg variable) and pass it to TranslateMessage(), this does a bit of additional processing, translating virtual key messages into character messages. This step is actually optional, but certain things won't work if it's not there.
3.我们提取消息(在MSG的变量里),并将其交给TranslateMessage(),这需要一点额外的处理,把虚拟的键消息翻译成实质的字符消息.这一步实际上是可选的,但是如果它不存在,某些事情将不会工作.
4.Once that's done we pass the message to DispatchMessage(). What DispatchMessage() does is take the message, checks which window it is for and then looks up the Window Procedure for the window. It then calls that procedure, sending as parameters the handle of the window, the message, and wParam and lParam.
4.上一步一完成,我们就把消息传给DispatchMessage().DispatchMessage()拿到消息,检查它属于哪一个窗口,然后查找该窗口的窗口过程.然后它以窗口句柄,消息,wParam,lParam作为参数调用的窗口过程.
5.In your window procedure you check the message and it's parameters, and do whatever you want with them! If you aren't handling the specific message, you almost always call DefWindowProc() which will perform the default actions for you (which often means it does nothing).
5.在你的窗口过程中,你检查消息,和它的参数,并你想让它做的事!如果你不处理具体的消息,你几乎总是调用DefWindowProc.为您执行默认的事件(这往往意味着什么都不做).
6.Once you have finished processing the message, your windows procedure returns, DispatchMessage() returns, and we go back to the beginning of the loop.
6.一旦你完成消息处理,你的窗口过程就会返回,DispatchMessage()也返回,我们也回到开始的循环.
-----------------------------------------------------------------------------------------------
Actually, I copy them from book, because I don't think I have such high level to write down this beautiful explanation. But I do add some personal viewpoint. Also, i translate it by myself.
whatever, i strongly suggest you to read this if you want to due with win32, or even MFC.
Thursday, October 30, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment