当点击一个子窗口,比如是一个按钮的话,按道理,系统会发送一个WM_LBUTTONDOWN给这个按钮,按钮收到后,其默认处理就是按钮凹下去,但是我不解的是为什么父窗口也会收到一个参数为按钮ID的WM_COMMAND的消息,是系统自动发了两个消息?还是按钮收到鼠标按键消息后再发给父窗口的?

解决方案 »

  1.   

    一般来说,当选择一个按钮时将会发生:1 Windows 将键盘焦点赋给该按钮;2 该按钮向其父窗口发送一条消息以便将选择通知给它;3 父窗口向按钮发送一条消息来改变其状态;4 父窗口重绘该按钮来反映其新状态。
      

  2.   

    This in effect creates a "child window control." The child window processes mouse and keyboard messages and notifies the parent window when the child window's state has changed. In this way, the child window becomes a high-level input device for the parent window. It encapsulates a specific functionality with regard to its graphical appearance on the screen, its response to user input, and its method of notifying another window when an important input event has occurred. Although you can create your own child window controls, you can also take advantage of several predefined window classes (and window procedures) that your program can use to create standard child window controls that you've undoubtedly seen in other Windows programs. These controls take the form of buttons, check boxes, edit boxes, list boxes, combo boxes, text strings, and scroll bars. For instance, if you want to put a button labeled "Recalculate" in a corner of your spreadsheet program, you can create it with a single CreateWindow call. You don't have to worry about the mouse logic or button painting logic or making the button flash when it's clicked. That's all done in Windows. All you have to do is trap WM_COMMAND messages—that's how the button informs your window procedure when it has been triggered. Is it really that simple? Well, almost. 
      

  3.   

    我是楼主
    =======
    我能否这样理解:
    用户点击按钮后,系统发送消息给这个按钮,按钮收到后,发送消息给其父窗口,然后父窗口来做相应的处理。那我问一下:
    WM_COMMAND和BN_xxx消息有什么差别?
    比如点击按钮后,按钮发给父窗口的是WM_COMMAND还是BN_CLICKED还有WM_NOTIFY是什么?