본문 바로가기

Windows/MFC

Windows Message Handling - Part 1

http://www.codeproject.com/KB/dialog/messagehandling.aspx

Sending Messages:
Besides receiving messages, you will often find your self sending messages. You might want to send messages to communicate between to windows in your program, or to communicate between different programs. In order to send a message you need a pointer to a c++ window class. This can be retrieved using various functions, including CWnd::FindWindow, GetDlgItem(), GetParent(), and more. The CWnd class has a SendMessage() member function which allows you to send messages to it's window. For example, Let’s say you have a CWnd pointer to the Calculator, and you want to close it. What you should do is send a WM_CLOSE message, which will notify the Calculator that it should close. You can use the following code. In order to get a pointer to Calculator, I use the static CWnd::FindWindow() function and pass the title of the window, which in our case is "Calculator".


CWnd* pCalc;

pCalc = CWnd::FindWindow(NULL, _T("Calculator"));

if (pCalc == NULL)
{
    
}
else
{
    pCalc->SendMessage(WM_CLOSE);
}