Sign in to follow this  
Norman Barrows

What windows messages should a well behaved app support?

Recommended Posts

mhagain    13430

A few others.

 

WM_MENUCHAR for reasons outlined at http://stackoverflow.com/questions/3662192/disable-messagebeep-on-invalid-syskeypress

 

WM_INPUT if you're using raw input (obviously).

 

WM_MOVE and WM_SIZE are good places to handle any internal state you may have that depends on the window size and position.  WM_SIZE is not a good place for lost device handling, nor is WM_ACTIVATE.

 

WM_CLOSE may be handy to prevent the player from accidentally closing the window when they may not intend to.

 

WM_ACTIVATE to detect when you're no longer the focus; this is useful because you can start Sleeping, skip display updates and otherwise behave like a civilized program on the player's computer.  Don't use it for lost device handling.

 

WM_DESTROY can be a good place to clean up resources.

Share this post


Link to post
Share on other sites
Hiwas    5807

A few others.

 

WM_MENUCHAR for reasons outlined at http://stackoverflow.com/questions/3662192/disable-messagebeep-on-invalid-syskeypress

 

WM_INPUT if you're using raw input (obviously).

 

WM_MOVE and WM_SIZE are good places to handle any internal state you may have that depends on the window size and position.  WM_SIZE is not a good place for lost device handling, nor is WM_ACTIVATE.

 

WM_CLOSE may be handy to prevent the player from accidentally closing the window when they may not intend to.

 

WM_ACTIVATE to detect when you're no longer the focus; this is useful because you can start Sleeping, skip display updates and otherwise behave like a civilized program on the player's computer.  Don't use it for lost device handling.

 

WM_DESTROY can be a good place to clean up resources.

Those are good messages to handle but only the menu one is the type of edge case stuff I was thinking of.  Not saying this is wrong, just that I'd be more interested in getting a good list of all those rarely used but oh so annoying messages a game should handle but usually doesn't.  As much for the original poster as for me, I used to have a list but I can't seem to find the damned thing. :)

Share this post


Link to post
Share on other sites
Endurion    5408

You can choose to handle all or none.

 

Most important is that you pass ALL of them on to DefWindowProc unless you explicitely handle it and the docs say you can return a specific value from your WndProc.

Share this post


Link to post
Share on other sites
Khatharr    8812

Precisely those messages needed to do its job - no more, no less.


Why even answer if that's all you have to say?

 

Because it's really correct. An open-ended question like that implies that the author is either not thinking about the question or else is not thinking about solutions.

Share this post


Link to post
Share on other sites
Amr0    2230

Precisely those messages needed to do its job - no more, no less.


Why even answer if that's all you have to say?

 

Actually his answer sums up a strategy which is important in order to not introduce unwanted quirks. The less messages that a window handles, the less likely it is to misbehave. DefWindowProc() does A LOT of things to implement standard behavior of windows, and it expects to receive certain messages with certain data in a certain order. If your window messes with the order (by calling certain functions in its message handler which cause certain events which will in turn send certain messages before they return, or by not passing certain messages to DefWindowProc()), then there is a chance your window will break one of the standard behaviors implemented by DefWindowProc().

 

So yes, only process the messages that you need to process.

Share this post


Link to post
Share on other sites
Norman Barrows    7179

Hmm, I remember there being a number more of these annoying edge cases you really should handle but can't think of any more off the top of my head.

 

thanks for the list. i think i've figured out the absolute minimum. those you mentioned will make for a more robust implementation.

Share this post


Link to post
Share on other sites
Norman Barrows    7179

looks like at a minimum, WM_DESTROY should be handled: close_myapp() PostQuitMessage() Exit()

 

i had it turned off, and the process would not terminate from the taskbar when its window was destroyed.

 

Turning it on and just calling PostQuitMessage() only unhooks the winproc, its doesnt exit. 

 

close_myapp() is the "right" way to do it, but may be overkill, as you're terminating anyway, and windows ought to release all resources associated with the process. but its still probably safer to not rely on windows to do your cleaning up for you.    in my book (my way of doing things) i'd probably have to call "not calling close_myapp()" a bad practice.

 

other than WM_DESTROY, i only trap mouse movement, buttons, and wheel. default winproc does all the rest.  i wonder what happens when the PC goes to sleep when the game is running? never tried it.   it handles lost device now, so it ought to come right back up when you wake up the PC, assuming it (windows) does a full save and restore of state on sleeping.

Share this post


Link to post
Share on other sites
shuma-gorath    1161

i wonder what happens when the PC goes to sleep when the game is running? never tried it.   it handles lost device now, so it ought to come right back up when you wake up the PC, assuming it (windows) does a full save and restore of state on sleeping.

You can easily test this.  First, create a timer that times out after a short time, using SetTimer().  When you receive WM_TIMER, call KillTimer(), then call SetSuspendState() to make the system sleep, passing FALSE for all three arguments.  According to MSDN, calling SetSuspendState() on you UI thread is a blocking call, which means you should be able to invoke your recovery code right after.

 

EDIT: An alternative method is to do sleep manually, then handle the WM_POWERBROADCAST notification when the system awakens.

Edited by shuma-gorath

Share this post


Link to post
Share on other sites
Norman Barrows    7179

Does anyone know which messages are sent when switch display shortcuts are used: shift + win + left/right arrow keys?

 

none specific to that action that i can tell. and i just dug through them all the other day. if you're talking about windows itself, they may trap the individual WM_key messages or check the keys asynchronously  if you're talking about 3rd party software it may be an application defined message.

 

i would think that it would send some type of message about change of window size or something. you're talking about switching between 2 monitors on a multi-monitor system, right?

Share this post


Link to post
Share on other sites
zfvesoljc    604

Does anyone know which messages are sent when switch display shortcuts are used: shift + win + left/right arrow keys?

 

none specific to that action that i can tell. and i just dug through them all the other day. if you're talking about windows itself, they may trap the individual WM_key messages or check the keys asynchronously  if you're talking about 3rd party software it may be an application defined message.

 

i would think that it would send some type of message about change of window size or something. you're talking about switching between 2 monitors on a multi-monitor system, right?

 

Yep, for some reason my win app (CreateWindow, etc...) doesn't switch to other monitor.

Share this post


Link to post
Share on other sites
Norman Barrows    7179

Yep, for some reason my win app (CreateWindow, etc...) doesn't switch to other monitor.

 

2 head system, right?      2 graphics cards, 2 monitors,     or 2 cards in 1 with 2 monitors.

 

i would think that each head would be a separate d3d device, and that you'd shut down one and startup the other on head switch.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this