Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] How to Choose Between SendMessage and PostMessage

Sunday, 6 September 2015
|
Read: Completed in minutes

[Tips] How to Choose Between SendMessage and PostMessage


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)





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 steps

1. Open Notepad
2. Open your Microsoft Spy ++ (Built-in Microsoft Visual Studio 2012 ++)


How to Choose Between SendMessage and PostMessage

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

How to Choose Between SendMessage and PostMessage

4. Drag this cross circle to notepad on this edit area

How to Choose Between SendMessage and PostMessage

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

How to Choose Between SendMessage and PostMessage

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

How to Choose Between SendMessage and PostMessage

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 ✋✋✋✋

🙇🏼🙇🏼 We Appreciate Your Comments and Suggestions - Webzone, all things Tech Tips web development
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com