SendMessage vs PostMessage
What different between them?
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. | 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. |
Same TCP | Same UDP |
Sequentially | Not sequentially |
Gửi cho windows khác và nó sẽ nhận cái message này, xử lý cho xong mới tới cái khác, cho nên có một số cái bị not responding, do ko đáp ứng được | Gửi cho window khác không quan tâm là nhận được hay không |
Tạo thread khác để xử lý không viết chung thread xử lý giao diện (vẽ lai windows) |
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:How to use post message with LPRAM and RPARAM
This topic will be demo how to use PostMessage
::PostMessage(HWND, WM_KEYDOWN, WPARAM, LPARAM);
::SendMessage(HWND, WM_KEYDOWN, WPARAM, LPARAM);
Parametes:
- 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. See
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx
- LPARAM, you can know through notepad program and microsoft spy++
Finding LPARAM parameter:
I will demo how to get LParam value through some basic steps1. Open Notepad
2. Open your Microsoft Spy ++ (Built-in Microsoft Visual Studio 2012 ++)
3. Click to menu Spy -> FindWindows, using cross circle on the red frame.
4. Drag this cross circle to notepad on this edit area
5. Click choose Messages properties same as below picture. Then click ok
6. Let's press RETURN key to Edit Area, you can easy to see WM_KEYDOWN message on Microsoft Spy++ Dash Broad. Then right click to that message and choose properties, you can easy to know LPARAM and WPARAM value
7. After you have LPARAM and WPARAM value, you can use this PostMessage / SendMessage API as very easy
::PostMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
::SendMessage(hHandle, WM_KEYDOWN, VK_RETURN, 0x001C0001);
Same as, you can do it with VK_DOWN, VK_LEFT, .... key.
Source code:
Demo how to use PostMessage with WM_KEYDOWN, WM_CHAR- We have one ComboBox with value on control, we name it is m_cbCommand (Control Type)
- We want to post text from this ComboBox to windows with Handle
/* *******************************************************************************
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"https://learn-tech-tips.blogspot.com",
L"huuvi168@gmail.com",
L"zidane"
L"https://www.codeproject.com/Tips/1005932/How-to-build-program-auto-commit-source-code-to-SV",
};
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);
}
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
How to Choose Between SendMessage and PostMessage ,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips Zidane, all things tech tips web development
- I am Zidane, See you next time soon ✋✋✋✋