Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 15: Fundamental Controls Visual C++ and MFC Fundamentals


This method gives focus to the control that called it. In the following example, an edit
box identified as IDC_EDIT1 will receive focus when the user clicks the button:

void CFormView1View::OnButton2()
{
// TODO: Add your control notification handler code here
CButton *btnFirst;

btnFirst = (CButton *)GetDlgItem(IDC_EDIT1);
btnFirst->SetFocus();
}

Once a control receives focus, it initiates a WM_SETFOCUS message, which fires an
OnSetFocus() event. The syntax of the CWnd::OnSetFocus() event is:

afx_msg void OnSetFocus( CWnd* pOldWnd );

You can use this event to take action when, or just before, the control receives focus. In
the following example, when an edit box receives focus, a message box is displayed:

void CFormView1View::OnSetFocusEdit3()
{
// TODO: Add your control notification handler code here
MessageBox("The Result edit box should not receive focus!!!");
}

At anytime, to find out what control has focus, call the CWnd::GetFocus() method. Its
syntax is:

static CWnd* PASCAL GetFocus();

This method returns a handle to the control that has focus at the time the method is called.
While the user is interracting with your application, the focus changes constantly. For this
reason, you should avoid using the return type of this method from various events or
member functions. In other words, do not globally declare a CWnd variable or pointer,
find out what control has focus in an event Event1 and use the returned value in another
event Event2 because, by the time you get to Event2, the control that had focus in Event1
may have lost focus. In fact, the dialog box that holds the control or the main application
may have lost focus. Therefore, use the GetFocus() method only locally.

14.2.9..The Window’s Visibility....................................................................


After a window or a control has been created, for the user to take advantage of it, it must
be made visible. As we will learn in other lessons, when it comes to their visibility, there
are two types of windows: those the user can see and interact with, and those invisible
conttrols that work only behind the scenes and cannot be displayed to the user.

During control design and when we reviewed their styles, we saw that a window can be
made displayed to the user by setting its Visible property to True or by adding it the
WS_VISIBLE style.

If you did not set the Visible property to True or did not add the WS_VISIBLE style, the
control would be hidden (but possibly available). Therefore, if at any time a window is
hidden, you can display it by calling the CWnd::ShowWindow() method. Its syntax is:
Free download pdf