VC6 Add custom messages (the main window sends messages to child windows)

Source: Internet
Author: User
Tags message queue

To send a message to a child window from the main window, you can add a custom message to the child window, and then you need a place to call the message in the main window.

Call Method:

1. Add the child window to the member variable of the main window;

2. The main window calls the message, the member variable name. SendMessage (um_progress);

child window Add custom message steps as follows:

1, define the message.

In Windows, all messages are represented by a specific integer value, and in order to avoid a custom message that conflicts with other messages that already exist, you should take advantage of a constant provided by Windows: Wm_user, which is less than the system-reserved. That is, the user-defined message is at least wm_user+1, noting that the value of the last message is not more than 0x7fff. When developing WINDOWS95 applications, Microsoft recommends that user-defined messages be at least wm_user+100, because many new controls also use Wm_user messages.

#define Um_progress Wm_user + 100

Adds the previous sentence to the header file (. h) of the child window class.

2. Declare the message handler function in the afx_msg block of the child window class header file:

Class Cmainframe:public cframewnd{

Protected

{{afx_msg (CMainFrame)

afx_msg int OnCreate (lpcreatestruct lpcreatestruct);

afx_msg void OnTimer (UINT nidevent);

afx_msg lresult OnProgress (WPARAM WPARAM, LPARAM LPARAM);

}}afx_msg

Declare_message_map ()

3. In the implementation file (. cpp) of the child window class, use the ON_MESSAGE macro directive to map messages to the message processing table.

Begin_message_map (CMainFrame, CFrameWnd)

{{Afx_msg_map (CMainFrame)

On_wm_create ()

On_wm_timer ()

On_message (um_progress, onprogress)/Note there is no semicolon at the back of this statement

}}afx_msg_map

End_message_map ()

4, the implementation of message processing functions. The function uses the Wpram and lparam parameters and returns Lpesult.

Lresult cmainframe::onprogress (WPARAM wparam,lparam LPARAM) {

CRect rect;

M_wndstatusbar.getitemrect (2,&rect); Get the pane area

Create a progress bar, note that the third parameter is cwnd* pParentWnd, select the parent form as appropriate

M_progress. Create (ws_child| ws_visible| pbs_vertical,rect,this,123);

M_progress. SetPos (50);

return 0;

}

5, in the appropriate time to send custom messages, for message processing. You need to pay attention to using SendMessage or postmessage for processing: SendMessage to return after the message is processed, and postmessage to return the message immediately after it has been placed in the message queue.

SendMessage (um_progress);

PostMessage (um_progress);

If the user needs a unique message for the entire system, you can invoke the SDK function RegisterWindowMessage and use ON_REGISTER_MESSAGE macro directives instead of On_message macros, and the rest of the steps are the same.

Turn from: http://blog.sina.com.cn/s/blog_6bd15b910100nc00.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.