Visual C++ and MFC Fundamentals Chapter 15: Fundamental Controls
void CSecondDlg::OnDisableMemo()
{
// TODO: Add your control notification handler code here
Memo->EnableWindow(!Memo->IsWindowEnabled());
}
14.3 Access to a Controls Instance and Handle..............................................
14.3.1..The Instance of an Application.........................................................
When you create a Win32 application using the WinMain() function, or if you create an
MFC application using the CWinApp class, when the application comes up, it creates an
instance, which is usually the hInstance argument of the WinMain() function. In the
same way, when you create an MFC application, which is done using a class based on
CWinApp, and when the application comes up, it creates an instance. Sometimes you
will need to refer to the instance of your application. We have already mentioned that
you can do this by calling the CWinApp::m_hInstance member variable. Alternatively,
for an MFC application, you can call the AfxGetInstanceHandle() global function to get
a handle to the instance of your application. This could be accessed as follows:
BOOL CDialog1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
HINSTANCE InstanceOfThisApp = AfxGetInstanceHandle();
return TRUE; // return TRUE unless you set the focus to a control
}
14.3.2..The Handle to a Window...................................................................
We saw with Win32 applications that, when creating a parent window, if you want to
refer to that parent control, you should use the value returned by the CreateWindow() or
the CreateWindowEx() function, which is an HWND value. If you are creating an MFC
application, you usually call the Create() method of the window you are creating. As the
parent of all window classes of an MFC application, CWnd provides a member variable
called m_hWnd. It is defined as follows:
HWND m_hWnd;
This public variable is inherited by all classes that are based on CWnd, which includes
all MFC window objects. Consequently, m_hWnd gives you access to the handle to the
window you have created. For example, the CDialog class, which is based on CWnd but
is the most used host of Windows controls, can provide its m_hWnd variable as the
parent of its control. Here is an example: