Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

SendMessage and PostMessage?

3.47/5 (23 votes)
17 Oct 2015CPOL2 min read 94.4K   2.9K  
This topic focuses on what is the difference between SendMessage and PostMessage and how to use them?

Introduction

This topic focuses on two things:

  1. What is the difference between SendMessage and PostMessage
  2. How to use SendMessage, PostMessage with WM_KEYDOWN. The way to find LParam and RParam parameters

To understand what is the difference between SendMessage and PostMessage, please look at the below table.

What is the Difference between SendMessage, PostMessage

SendMessage PostMessage
Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message (from MSDN) Places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message (from MSDN)
Sequentially Not sequentially
synchronous asynchronous

How to Use SendMessage, PostMessage?

C++
//
// ::PostMessage (HWND, WM_KEYDOWN, WPARAM, LPARAM);
//
C++
//
// ::SendMessage (HWND, WM_KEYDOWN, WPARAM, LPARAM);
//

Parameters?

  • HWND is a handle you want to seed message to this window
  • WPARAM, you can view here: The virtual-key code of the nonsystem key.
  • LPARAM, you can know through Notepad program and Microsoft spy++

How to Find LParam

  • First, you must download Microsoft spy++. If you install Microsoft Visual 2012, Microsoft spy ++ will be integrated
  • I will demo how to get LParam with VK_KEYDOWN, value through some basic steps
    (Same as, you can do it with VK_DOWN, VK_LEFT, .... key)
  • Now open Notepad by run -> notepad
  • Open your Microsoft Spy ++ (Built-in Microsoft Visual Studio 2012 ++)

http://learn-tech-tips.blogspot.com

Next, Click to menu Spy -> FindWindows, using cross circle on the red frame.

http://learn-tech-tips.blogspot.com

Drag this cross circle to Notepad on this edit area:

http://learn-tech-tips.blogspot.com

Click choose Messages properties same as the below picture. Then click ok.

http://learn-tech-tips.blogspot.com

Let's press RETURN key to Edit Area, you can easily see WM_KEYDOWN message on Microsoft Spy++ Dash Broad. Then, right click to that message and choose properties, you can easily know LPARAM and WPARAM value.

http://learn-tech-tips.blogspot.com

My LPARAM value found: 0x0001C001 and RPARAM value: 0x0000000D

C++
::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);

::SendMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);

Using the Source Code

Using FindWindowA and FindWindowEx

API for get window handle:

C++
HWND hwnd = FindWindowA("Notepad", NULL );
HWND hWndChild = ::FindWindowEx(hwnd, NULL, L"Edit", NULL);
C++
/* *******************************************************************************
 Developer: zidane (huuvi168@gmail.com)
 Last Modified: 2015-09-01
 * ******************************************************************************/
BOOL CReadInfomationGameDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    // when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);   // Set big icon
    SetIcon(m_hIcon, FALSE);  // Set small icon
    
    // TODO: Add extra initialization here
    CString strCommand[] =  
    {

        L"learn-tech-tips",
        L"http://learn-tech-tips.blogspot.com",
        L"huuvi168@gmail.com",
        L"zidane"
        L"http://www.codeproject.com/Tips/1025019/Get-Server-Side-Data-time-with-Cplusplus",
    };

    int length = sizeof(strCommand) / sizeof(strCommand[0]);
    for (int i=0; i<length; i++)
        m_cbCommand.AddString(strCommand[i]);
 
 
    return TRUE;  // return TRUE  unless you set the focus to a control
}

HWND CReadInfomationGameDlg::GetHandleOfWindow(void)
{ 
    HWND hwnd = FindWindowA("Windows abc Class", NULL ); 

    if (!hwnd) 
    {
        MessageBox(_T("Window abc not found"), _T("Message"), MB_OK 
                                                    | MB_ICONINFORMATION);  
        return NULL;
    } 
 
    return hwnd;
}

void CReadInfomationGameDlg::OnBnClickedButtonAddmessage()
{
    // TODO: Add your control notification handler code here
    HWND hHandle = GetHandleOfWindow();

    CString strCBText;
    int nIndex = m_cbCommand.GetCurSel();
 
    if (nIndex == -1) // cannot found text in comboBox, post NULL
    {
        TCHAR strTemp[255];  
        memset(strTemp, 0x00, sizeof(strTemp));
        GetDlgItemText(IDC_COMBO_COMMAND, strTemp, sizeof(strTemp));
        PostChatMessage(hHandle, strTemp);
    }
    else // found text in comboBox
    {
        m_cbCommand.GetLBText(nIndex, strCBText); 
        PostChatMessage(hHandle, strCBText);
    }
}

void CReadInfomationGameDlg::PostChatMessage(HWND hHandle, LPCTSTR szChatMsg)
{
     ::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
     ::PostMessage(hHandle, WM_KEYDOWN, VK_DOWN, 0x00500001);

    while (szChatMsg[0])
    {
        ::PostMessage(hHandle, WM_CHAR, LOBYTE(szChatMsg[0]),0);
        szChatMsg++;
    }

    ::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
}

Points of Interest

I like coding, I like researching new technology.

My tech blog is http://learn-tech-tips.blogspot.com.

My blog sharing experiences about technology tips and trick include: language programming: C+, C#, ....
Design skills: photoshop, Office: Excel, Outlook and .... other things!

If you have any feedback about this topic, please leave your comment, we can discuss about it. Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)