Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
Like this article? We recommend

Tips for Using Messages Well

You should probably set up your own prefix label for each registered message. As an example, MYWM_ would be one possible prefix. This is used mostly for clarity in your code, since all the Windows operating system messages begin with WM_.

It's a good idea to comment your messages in the code. While most of our code is self-documenting (cough, cough), some coders just don't think like we do (cough, cough), and we have to provide just a little explanation of what the registered messages do. It only takes two slashes (//) and a line of comment to make sure someone else understands what you meant.

When using messages, don't forget the difference between SendMessage and PostMessage:

  • SendMessage sends the message and any data to your application and waits for the function to return.

  • PostMessage sends the message and data to your application and returns immediately.

If you're using PostMessage and have data associated with the LPARAM or WPARAM, I suggest putting that data into a circular buffer; that way, you can process it while other data is being put in. WaitForSingleObject is a great Windows function to use when using PostMessage and putting the data into a buffer. How will you know if there is data in the buffer so you can process it and finally delete it? Well, that's where threads and WaitForSingleObject become your new friends.

  • + Share This
  • 🔖 Save To Your Account