Custom message in MFC

xiaoxiao2021-03-06  167

Message mapping, loop mechanisms are the basic ways of running the Windows program. There are many ready-made message handles in the VC MFC, which we need to complete other tasks, you need to customize the news, you have encountered some difficulties. Add user-defined messages are not allowed in the MFC ClassWizard, so we must add a corresponding code in the program so that you can process custom messages like processing other messages. The usual approach is to take the steps:

Step 1: Define the message.

Recommended User Custom Messages are at least WM_USER 100 because many new controls also use the WM_USER message.

#define WM_MY_MESSAGE (WM_USER 100)

Step 2: Implement the message processing function. This function uses WPRAM and LPARAM parameters and returns lpeSult.

LpeSult CMAINFRAME :: OnmyMessage (WPARAM WPARAM, LPARAM LPARAM) {// Todo: Handling User Custom Messages ... Return 0;}

Step 3: Describe the message processing function in the AFX_MSG block of the class header:

class CMainFrame: public CMDIFrameWnd {... // general message mapping function protected: // {{AFX_MSG (CMainFrame) afx_msg int OnCreate (LPCREATESTRUCT lpCreateStruct); afx_msg void OnTimer (UINT nIDEvent); afx_msg LRESULT OnMyMessage (WPARAM wParam, LPARAM lParam ); //}} AFX_MSG DECLARE_MESSAGE_MAP ()}

Step 4: In the user class message block, use the ON_MESSAGE macro to map the message to the message processing function.

BEGIN_MESSAGE_MAP (CMainFrame, CMDIFrameWnd) // {{AFX_MSG_MAP (CMainFrame) ON_WM_CREATE () ON_WM_TIMER () ON_MESSAGE (WM_MY_MESSAGE, OnMyMessage) //}} AFX_MSG_MAP END_MESSAGE_MAP ()

If the user needs a unique message to define the entire system, you can call the SDK function registerWindowMessage definition message:

Static uint WM_MY_MESSAGE = RegisterWindowMessage ("User");

And use the ON_REGISTERED_MESSAGE macro to replace the ON_MESSAGE macro, the rest of the steps.

When you need to use a custom message, you can call the function PostMessage or SendMessage to send a message PoseMessage (WM_MY_MESSAGE, O, O, O) in the function in the corresponding class; if you send a message to other processes, send messages by following:

DWord Result; SendMessageTimeout (WND-> M_HWND, // Target window WM_MY_MESSAGE, // message 0, // wparam0, // lparamsmto_abortifhung | smto_normal, timeout_interval, & result);

To avoid other processes, if the system is blocked, it causes the system to die.

However, if you need to send a message to other classes (such as primary frameworks, sub-windows, sisters, dialogs, status bars, toolbars, or other controls, etc.), the above method seems to be in powerless, and other classes are often taken during programming. A identification signal, the MFC framework has caused all kinds of restrictions, but can send messages to this class by getting a pointer to a certain class, and various actions of custom messages are defined in this class, which is free. Free message to other classes.

From: http://www.yesky.com/

转载请注明原文地址:https://www.9cbs.com/read-128064.html

New Post(0)