Introduction
This topic focuses on two things:
- What is the difference between
SendMessage
and PostMessage
- 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?
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 ++)
Next, Click to menu Spy -> FindWindows, using cross circle on the red frame.
Drag this cross circle to Notepad on this edit area:
Click choose Messages properties same as the below picture. Then click ok.
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.
My LPARAM
value found: 0x0001C001
and RPARAM
value: 0x0000000D
::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:
HWND hwnd = FindWindowA("Notepad", NULL );
HWND hWndChild = ::FindWindowEx(hwnd, NULL, L"Edit", NULL);
BOOL CReadInfomationGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();
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);
}
}
SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE);
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; }
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()
{
HWND hHandle = GetHandleOfWindow();
CString strCBText;
int nIndex = m_cbCommand.GetCurSel();
if (nIndex == -1) {
TCHAR strTemp[255];
memset(strTemp, 0x00, sizeof(strTemp));
GetDlgItemText(IDC_COMBO_COMMAND, strTemp, sizeof(strTemp));
PostChatMessage(hHandle, strTemp);
}
else {
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.