How do I define my own WM (like WM_CLOSE, etc) message that can be handled by the message pump in C++?
If that is even possible.
The "message pump" is a core part of any Windows program that is responsible for dispatching windowing messages to the various parts of the application. This is the core of Win32 UI programming.
The message loop is an obligatory section of code in every program that uses a graphical user interface under Microsoft Windows. Windows programs that have a GUI are event-driven. Windows maintains an individual message queue for each thread that has created a window. Usually only the first thread creates windows.
The operating system communicates with your application window by passing messages to it. A message is simply a numeric code that designates a particular event. For example, if the user presses the left mouse button, the window receives a message that has the following message code.
After removing a message from its queue, an application can use the DispatchMessage function to direct the system to send the message to a window procedure for processing.
It depends on what you are using the message for. This link shows a breakdown of the "address space" for Win32 messages.
WM_USER
is not the correct solution in the general case. WM_USER
messages "can be defined and used by an application to send messages within a private window class. These values cannot be used to define messages that are meaningful throughout an application, because some predefined window classes already define values in this range."
You are better off assigning a message ID that is in the WM_APP
range.
RegisterWindowMessage
is useful if you want to have the system assign you a message ID at runtime. "All applications that register the same string can use the associated message number for exchanging messages," so you can use RegisterWindowMessage
when you need to use a custom message for simple inter-process communication.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With