How to use SendMessage to send custom messages correctly in the "Go" VC-good

Source: Internet
Author: User

Original URL: http://zhoumf1214.blog.163.com/blog/static/5241940200910265532959/

Recently in the development with VC2008, and later because of the use of the message sent, and need to customize the message, on the internet to find a lot of examples, according to what they say, although roughly the same, but basically no one can be completely done. To know VC programming there is a small place error, may be a cause you dizzy brain swelling things. After I have not only explored, after the improvement, a little error is not, now posted out for everyone's reference.

1. Start with a basic knowledge introduction

The basic structure of SendMessage is as follows:

SendMessage (

HWND hwnd,//Handle to the target window or thread of the message delivery.

UINT MSG,//message category (here can be some system messages, also can be their own definition, described below)

WPARAM WPARAM,//Parameter 1 (WPARAM is actually the same type as uint,

Right-click on the "Go to wparam definition" option in the VC compiler to view.

LPARAM LPARAM); Parameter 2

Some of these parameters are as follows:

typedef unsigned int UINT;

typedef UINT WPARAM;

typedef LONG LPARAM;

typedef LONG LRESULT;

2. SendMessage usage Examples

For example, you can use the following statement:

void Ctscrollwinview::onlbuttondblclk (UINT nflags, CPoint Point)
{
TODO: Add Message Handler code and/or call default values here

:: SendMessage (AfxGetMainWnd ()->M_HWND,WM_CHILDFRAMEDBCLK, 0,0);

CSCROLLVIEW::ONLBUTTONDBLCLK (nflags, point);
}

This is a piece of code in a multi-document tabbed project that I built under VC2008, to implement a message with no parameters after double-clicking the child window (whose ID is wm_user+1) to the main window, as part of the code, where wm_ CHILDFRAMEDBCLK is a custom message ID, AfxGetMainWnd ()->m_hwnd is getting the main window (this cannot be used here GetParent ()->m_hwnd or getparentframe ()->m _hwnd, because this is to get the parent window, but the parent window is not necessarily the main window, it is important to note that the message will be wrong to receive, I am here for a half-day, later in the QQ group in a warrior told me, here again thank the Warrior);

3. Define the form in which the message is received and the header file where the thread is located:

#define WM_CHILDFRAMEDBCLK wm_user+1//do something

4. Next, define a function to which the message needs to be mapped, as follows:

afx_msg LRESULT Onchlidframedbclick (WPARAM WPARAM, LPARAM LPARAM);

Note that the format must be: Two parameters are necessary, the return type must be lresult, many articles on the internet have neglected these two points, which is also a common error in the online article place.

5. Adding a message function map

On_message (Wm_childframedbclk,onchlidframedbclick)

Note that this must be on_message and cannot use On_command, which is intended primarily for user-defined messages, which

Wm_command commands, such as menus, toolbars, and so on.

6. Implement the Message function:

We define one such thing in the receiving form (the process),

LRESULT Cmainframe::onchlidframedbclick (WPARAM WPARAM, LPARAM LPARAM)
{
Cancelfullscreenwin (); Here called a full screen of the child window of the self-write function, I do not post it, and later on the topic will be mentioned

return 0;
}

7. The following is part of the code, in order to understand the custom message sent only look at this part of the code is enough

The message is defined in the MainFrm.h header file as follows:

#define WM_CHILDFRAMEDBCLK wm_user+1

The message functions that need to be mapped are then declared in MainFrm.cpp:

afx_msg LRESULT Onchlidframedbclick (WPARAM WPARAM, LPARAM LPARAM);

Then add the mappings in MainFrm.cpp as follows:

Begin_message_map (CMainFrame, CMDIFrameWndEx)
On_wm_create ()
On_command (Id_fullscreen, Onviewfullscreen)
On_message (Wm_childframedbclk,onchlidframedbclick)//Map add here
End_message_map ()

Then implement the Onchlidframedbclick () method in MainFrm.cpp, as follows:

LRESULT Cmainframe::onchlidframedbclick (WPARAM WPARAM, LPARAM LPARAM)
{
Cancelfullscreenwin ();//define your own functions, you can do the business you need here

return true;
}

Finally, you can send a message to the main window, as follows:

void Ctscrollwinview::onlbuttondblclk (UINT nflags, CPoint Point)
{
TODO: Add Message Handler code and/or call default values here

:: SendMessage (AfxGetMainWnd ()->m_hwnd,wm_user+1,0,0);

CSCROLLVIEW::ONLBUTTONDBLCLK (nflags, point);
}

Summary: ① Note that the ID of the definition message can not be duplicated, to use wm_user+n;

② Note that the return value of the defined message function must be lresult, and that there are two parameters that have to be wparam and lparam respectively;

③ attention maps with on_message, not on_command;

④ Note that the message passed to the main window is in the Cxxxxview class with AfxGetMainWnd ()->m_hwnd to get the main window.

At this point is finished, I hope that we find no resources to help

MFC Exit Program

Original URL: http://blog.csdn.net/one___thousand/article/details/40212369

PostQuitMessage (0);

PostMessage (wm_quit,0,0);

:: SendMessage (AfxGetMainWnd ()->m_hwnd,wm_close,0,0);

::P ostmessage (AfxGetMainWnd ()->m_hwnd,wm_close,0,0);

ExitProcess (0);//Pay attention to releasing allocated memory before use to avoid memory leaks

Exit (0);//normal termination procedure; Exit (not 0) non-normal termination program

SendMessage (WM_CLOSE); Send a WM_CLOSE message to the main window

Afxgetmainwnd->sendmessage (WM_CLOSE); To close an application in another class

How MFC obtains a handle

Handle Get method (after getting the window handle, you can send a message to the window class class, handlers):
For example:::P ostmessage ("Handle", Wm_user_drawviewpic_eds, 0, 0);

0. Gets the handle of the class window:
This->m_hwnd

1. Handle to the main window:
Gets the main window handle, whether within the main window class or within a child window class:
AfxGetMainWnd ()->m_hwnd,
such as::: SendMessage (AfxGetMainWnd ()->m_hwnd, Wm_my_dosome, 0, (LPARAM) i);
In the window class
This->m_hwnd
or This->getsafehwnd ()

2. child window handle: With FindWindow, (as long as you know the name of the child window) such as:
:: FindWindow (Null,windowsname)
Returns the handle of the found window
Find out whether the current window is running the Windowname program, the name of the window, such as:
cwnd* Pwnd=findwindow (null,_t ("Qing Zhi-cheque machine");//

GetSafeHwnd
function function: The safest way to get a handle (HWND) for a Window object (a CWnd derived object) pointer is to use the

The GetSafeHwnd () function.
Consider the following example for the reason:
CWnd *pwnd = FindWindow ("Explorewclass", NULL); Want to find the resource Manager
HWND hwnd = pwnd->m_hwnd; Get It's HWND
This code will appear with a "General protection error" when the pwnd is empty and close the app

program, because it is generally not possible to access its members to a null pointer if the following code is used:
CWnd *pwnd = FindWindow ("Explorewclass", NULL); Want to find the resource Manager
HWND hwnd = Pwnd->getsafehwnd (); Get It's HWND
There is no problem because GetSafeHwnd can still be used if pwnd is null, just return null

Source Document
3. Gets the handle to the control in the window, using the GetDlgItem
The syntax is: GetDlgItem (IDC_COMBO3)->m_hwnd: The parameter is the ID of the control, the return value is: cwnd* type, but it is a CWnd class

function, so get a pointer to the window where the control is located, such as:
CString Str (_t ("DD"));
:: SendMessage (AfxGetMainWnd ()->getdlgitem (IDC_COMBO3)->m_hwnd, Wm_settext, 0, (LPARAM)

Str. GetBuffer (0));

The subordinate handle obtains the pointer, uses the FromHandle
Syntax: cwnd* pWnd = Cwnd::fromhandle (hWnd);
Get window handle (complete) VC&MFC
To get the window handle:
This->m_hwnd
HWND GetForegroundWindow (VOID);
GetSafeHwnd take the handle of the window class where your program is located
GetActiveWindow take the current active window handle
AfxGetMainWnd take the main window handle
GetForegroundWindow take the foreground window handle
FindWindow
Enumwindow

How to use SendMessage to send custom messages correctly in the "Go" VC-good

Related Article

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.