We usually use SetFocus function to set the focus to a control. But in a dialog box, this may cause some small issues like sometimes the focus rectangle may be on one button while another button is highlighted, or sometimes having no default button at all. So we need to use another method for that.
If we use SetFocus function, the highlighting behavior may not be consistent. The highlight may depend on many factors like the previous input etc.
This behavior is because of the concept of "default button". The default button is typically drawn with a distinctive look, and will be the button that gets pushed when we hit Enter.
We can get the default button handle by sending the DM_GETDEFID message; similarly, we can change the default button by sending the DM_SETDEFID message. The default button can be set in the resource file itself, by setting the property “ Default Button”.
When the dialog box moves focus to a pushbutton, that pushbutton becomes the new default button. But when the dialog box moves focus to some other controls, the old default button will again be the default button. The dialog manager remembers which control was the default button when the dialog was initially created, and when it moves focus to something that isn't a button, it restores that original button as the default button.
The SetFocus function is a window manager function. If we use this, we are going directly to the window manager, bypassing the dialog manager. This can cause some issues like having focus rectangle on a button and the highlighting on another.
To avoid this problem, don't use SetFocus to change focus on a dialog. Instead, use the WM_NEXTDLGCTL message.
SendMessage(hdlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, TRUE);
The DefDlgProc function handles the WM_NEXTDLGCTL message by updating the internal dialog manager bookkeeping, deciding which button should be default, etc.
Using SetFocus we can move the Focus rectangle to a dialog control. But for push buttons, the blue highlight seen on them can’t be moved to another Pushbutton using SetFocus. So better use this message to set the focus in a dialog – always.
No comments:
Post a Comment