Removing Focus from a Control When Mouse Released OutsideLast reviewed: November 2, 1995Article ID: Q66947 |
The information in this article applies to:
SUMMARYUnder normal circumstances, when you move the mouse cursor into the client area of a child-window control, click it, and then release the mouse button, the child window sends a WM_COMMAND message to its parent and retains the focus. If you move the mouse into the client area of the child-window control, press the mouse button, move the mouse cursor out of the client area of the control, and then release the mouse button, the control does not send a WM_COMMAND message. However, the control retains the focus. If you do not want the control to retain the focus, you can remove it by performing the following steps:
MORE INFORMATIONWhen the mouse cursor is in the client area of a control and you press the mouse button, the parent window will receive a WM_PARENTNOTIFY message and a WM_MOUSEACTIVATE message. A Boolean (BOOL) flag should be set when the message is processed to indicate that this occurred. The parent window will receive other messages, including a number of WM_CTLCOLOR messages, when the mouse is moved around with the mouse button down. When the mouse button is released, the parent window receives only one of two messages:
In response to either message, the following steps must take place:
In a dialog box, SetFocus(NULL) MUST be used. SetFocus(hDlg) does not remove the focus from the button. The following code sample is taken from the dialog box procedure of a dialog that has a single OK button. If the mouse button is pressed while the mouse cursor is over the button, the mouse is moved outside the button, and then the mouse button is released, the focus is removed from the OK button. BOOL FAR PASCAL AboutProc(HWND hDlg, unsigned iMessage, WORD wParam, LONG lParam) { static BOOL fMousePress; switch (iMessage) { case WM_INITDIALOG: fMousePress = FALSE; return TRUE; case WM_PARENTNOTIFY: // or WM_MOUSEACTIVATE fMousePress = TRUE; break; case WM_MOUSEMOVE: if (fMousePress) SetFocus(NULL); fMousePress = FALSE; break; // Only command is the OK button. case WM_COMMAND: if (wParam == IDOK) EndDialog(hDlg, TRUE); break; } return FALSE; } |
Additional reference words: 3.00 3.10 3.50 3.51 4.00 95
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |