Jump to content

  • Log In with Google      Sign In   
  • Create Account

What windows messages should a well behaved app support?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 Norman Barrows   Crossbones+   -  Reputation: 2158

Like
1Likes
Like

Posted 07 April 2013 - 10:26 AM

What windows messages should a well behaved app support?

 

 


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15824

Like
5Likes
Like

Posted 07 April 2013 - 11:19 AM

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



Ask a vague question, get a vague answer :-P



#3 AllEightUp   Moderators   -  Reputation: 4217

Like
5Likes
Like

Posted 07 April 2013 - 11:35 AM

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



Ask a vague question, get a vague answer :-P

 

Agree'd, but I think pointing out a couple of the non-obvious ones you want to consider would be good:

 

WM_SYSKEYDOWN, specifically VK_MENU.  In some fullscreen windows if the user hits alt it can hang the program for no apparent reason.  The user just needs to hit alt again or esc but this is just bad to allow it to happen.

 

WM_SYSCOMMAND, specifically SC_SCREENSAVE.  Letting the screen saver kick in is also bad form in a fullscreen app.

 

WM_DISPLAYCHANGE, often overlooked but in multimonitor setups you need to notice this if your app were on say the second monitor and the user hit Windows-P and changed to mirroring, if they have 2 different resolution monitors your full screen app could be forced into a different resolution.  DX would trigger a device lost or reset I believe but a GL game needs to catch this and readjust view areas and all that.

 

The various CPU frequency change messages, power notifications (sleep/hibernate/etc) and related.  You may not care about the CPU change but your app should respect sleep/hibernate and say disconnect from servers and/or go to a menu and wait for wakeup etc.

 

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.


Edited by AllEightUp, 07 April 2013 - 11:39 AM.


#4 mhagain   Crossbones+   -  Reputation: 8005

Like
3Likes
Like

Posted 07 April 2013 - 03:31 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#5 AllEightUp   Moderators   -  Reputation: 4217

Like
0Likes
Like

Posted 07 April 2013 - 06:16 PM

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. :)



#6 wack   Members   -  Reputation: 1311

Like
6Likes
Like

Posted 07 April 2013 - 06:56 PM

According to the Windows 7 logo certification guidelines, which define what Microsoft thinks is a well behaved app, all applications must handle WM_QUERYENDSESSION and WM_ENDSESSION.

More info here:
http://download.microsoft.com/download/1/E/9/1E9580D9-2B2B-499C-918A-C9BA5EAC4A32/Windows%207%20Client%20Software%20Logo.pdf

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


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

#7 Endurion   Crossbones+   -  Reputation: 3586

Like
1Likes
Like

Posted 07 April 2013 - 11:12 PM

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.


Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

#8 Khatharr   Crossbones+   -  Reputation: 3007

Like
0Likes
Like

Posted 07 April 2013 - 11:15 PM

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.


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#9 Amr0   Members   -  Reputation: 1115

Like
1Likes
Like

Posted 08 April 2013 - 02:20 AM

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.



#10 Norman Barrows   Crossbones+   -  Reputation: 2158

Like
0Likes
Like

Posted 11 April 2013 - 07:14 PM

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.


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#11 Norman Barrows   Crossbones+   -  Reputation: 2158

Like
0Likes
Like

Posted 11 April 2013 - 07:41 PM

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.


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#12 zfvesoljc   Members   -  Reputation: 440

Like
0Likes
Like

Posted 12 April 2013 - 08:24 AM

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



#13 shuma-gorath   Members   -  Reputation: 887

Like
0Likes
Like

Posted 12 April 2013 - 09:15 AM

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, 12 April 2013 - 09:33 AM.


#14 Norman Barrows   Crossbones+   -  Reputation: 2158

Like
0Likes
Like

Posted 13 April 2013 - 07:37 PM

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?


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 


#15 zfvesoljc   Members   -  Reputation: 440

Like
0Likes
Like

Posted 15 April 2013 - 02:15 AM

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.



#16 Norman Barrows   Crossbones+   -  Reputation: 2158

Like
0Likes
Like

Posted 15 April 2013 - 01:08 PM

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.


Norm Barrows

Rockland Software Productions

"Building PC games since 1988"

 

rocklandsoftware.net

 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS