1

I'm asking as the title says. Is it possible?

Since a MSG already contains all the things I need for a self-made event handler, I figured maybe I could make one. I'm asking this mostly to get rid of interpreted casting so I can use internal functions and classes inside my window class for performance. I also want to know if its possible to just get the MSG alone and do whatever I want with it.

Basically is there another way to get the window message, and then process it not similar to the general loops found in this thread?

EDIT: Currently I'm using GetMessage() function to get the MSG struct and use that in my own event handler, however I am not getting all the messages that I want with this. Is there anything else I should do?

Thank you in advanced.

Community
  • 1
  • 1
kir
  • 581
  • 1
  • 6
  • 22
  • It sounds like you're looking for a more modular, possibly object-oriented way to program Windows. You can use .NET if you're willing to go with "managed" C++ ... if you don't want to deal with .NET there's [MFC](http://msdn.microsoft.com/en-us/library/d06h2x6e.aspx) and [wxWidgets](http://www.wxwidgets.org/), among others. – David Jan 07 '14 at 13:06
  • MFC and .NET still use the message loop at their core, its just abstracted away from you into an event interface so you dont see it. The message loop is pretty much THE way of processing window messages, though as i said, you can abstract it away into a prettier looking event design – bizzehdee Jan 07 '14 at 13:11
  • @David: That was actually my first idea at first, however using c++ and the lower level winapi gives me better performance, which is top priority. – kir Jan 07 '14 at 13:12
  • Not quite sure I understand. GetMessage() loads MSG data from the Windows message queue into your MSG struct. It's there - you have it, you can do with it what you wish, including breaking your app interaction with the OS:) – Martin James Jan 07 '14 at 13:13
  • @bizzehdee Yes. But as I interpreted the question, a higher level of abstraction is exactly what the OP is looking for. – David Jan 07 '14 at 13:13
  • You are not providing any functional requirements, other than *"because I want to"*. This is very little to go by. The 'feature' you describe can be implemented using *Message Crackers*, *WTL*, and *MFC*. The latter two do in fact replace the default window procedure to subclass pre-defined window classes. – IInspectable Jan 07 '14 at 13:15
  • @user2280704 Based on my experience, performance bottlenecks are rarely where you think they are (or where you think they're going to be). In a well-designed (modular) application, it should be relatively easy to optimize the code that really does need to be optimized. My suggestion would be to get your application working correctly first, and then identify & remedy any performance bottlenecks. – David Jan 07 '14 at 13:20
  • 1
    @IInspectable - yeah, I don't really understand the question either, especially the reference to 'performance' - the overhead of retrieving messages and calling windprocs/events is normally swamped by the effort of repainting etc. of GUI components. – Martin James Jan 07 '14 at 13:20
  • @MartinJames: This is actually what I'm doing now, and though I am getting the mouse messages I want, some messages, such as `WM_ACTIVATE` just get sent to the `WndProc` function instead. Functions such as EndDeferWindowPos throws messages and gets sent to the WndProc also. – kir Jan 07 '14 at 13:22
  • *"Is there anything else I should do?"* Yes: Call `TranslateMessage`. Otherwise you're not going to get `WM_CHAR` messages, for example. You also seem to be lacking fundamental understanding [About Messages and Message Queues](http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927.aspx). – IInspectable Jan 07 '14 at 13:37

1 Answers1

5

Currently I'm using GetMessage() function to get the MSG struct

Which is the problem, GetMessage() only retrieves messages that were posted to the message queue. It does not detect messages that were sent with SendMessage(). Which bypasses the message queue and calls the window procedure directly.

You therefore must use WndProc to see all the messages for a window.

The subset of posted messages that go into the queue and thus returned by GetMessage() is a small one. In a nutshell, the input notification messages for mouse and keyboard and the low-priority messages (WM_PAINT, WM_TIMER, WM_QUIT). WM_ACTIVATE is always sent.

Replacing the WndProc of a window is certainly a common technique, it is called "sub-classing the window". Any C++ class library wrapper uses it to map messages to C++ methods. Best to not re-invent that wheel.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536