I recently realized that the Windows dialog manager apparently honors mnemonics to controls that aren't visible. For example, if I hide a button labeled "&Foo"
on a dialog via ShowWindow(SW_HIDE)
, pressing Alt+F triggers the button.
Why does Windows do this? (I'm guessing that long ago some developer somewhere perhaps used a hidden control as a poor-man's way to implement hidden keyboard shortcuts, and this unintuitive behavior has been preserved for the sake of backward compatibility.)
More importantly: before I go through our entire codebase to audit all ShowWindow
sites and to make them explicitly disable hidden controls, is there any decent way to adjust this behavior in the dialog manager? I'm somewhat surprised that I haven't been able to find much information/complaining about this; the best I could find is what looks like an old Microsoft KB article archived by a third-party that recommends to override each control's WM_GETTEXT
handler by subclassing. (That seems rather heavy-handed thing to do for every control that might be hidden.)
(Also note that that article discusses disabled controls, not hidden ones, so the dialog manager doesn't ignore disabled controls either. This can also be observed by creating a hidden, disabled checkbox that shares a mnemonic with a visible, enabled one. Pressing the mnemonic key moves focus to the visible checkbox but does not toggle it as it normally would, just as it would for a typical case of a mnemonic collision.)