Microsoft KB Archive/224928

= BUG: CFormView Loses the Control's Focus When Restored =

Article ID: 224928

Article Last Modified on 9/12/2003

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Professional Edition
 * Microsoft Visual C++ 4.1 Subscription
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q224928



SYMPTOMS
If you set the focus to one control of CFormView, minimize the frame window of CFormView, and then restore it, the focus isn't set back to the control that previously held the focus.



CAUSE
The handle of the control that had the focus when the frame window was minimized, isn't stored.



RESOLUTION
To save the handle of the control that has the focus when you minimize the view, do the following:

  Define a message called WM_SAVEFOCUS in the Resource.h file:   Add a message handler for this user-defined message. In the class definition of your CMyFormView class, add: afx_msg LRESULT OnSaveFocus(WPARAM wParam, LPARAM lParam); In the .cpp file of CMyFormView, add the message map macro: ON_MESSAGE(WM_SAVEFOCUS, OnSaveFocus) Add the handler function: LRESULT CMyFormView::OnSaveFocus(WPARAM wParam, LPARAM lParam) {   CFormView::SaveFocusControl;
 * 1) define WM_SAVEFOCUS         WM_APP+1

return (LRESULT)0; }                     In the class definition of CMainFrame, add:

afx_msg void OnSysCommand( UINT nID, LPARAM lParam ); In the .cpp file, add the OnSysCommand message map macro: ON_WM_SYSCOMMAND Override OnSysCommand of the frame window class that you are using for the formview, and send the WM_SAVEFOCUS message when it is about to minimize. The code should resemble the following: void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam) {       if ((nID & 0xfff0) == SC_MINIMIZE) GetActiveView->SendMessage(WM_SAVEFOCUS);

CFrameWnd::OnSysCommand(nID,lParam); }                       



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



Steps to Reproduce Behavior

 * 1) Use AppWizard to generate an MFC SDI application. In the last step, change the base class of the view to CFormView.
 * 2) Insert two edit box controls to the view from the resource editor.
 * 3) Compile and run the application.
 * 4) The focus is set to the first edit box by default. Change the focus to the second edit box by clicking it.
 * 5) Minimize the frame window by clicking Minimize on the System menu, or click the Minimize button in the title bar.
 * 6) Restore the frame window. The focus will go back to the first edit box, not the second edit box.

