Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 2 Variables and Identifiers Visual C++ and MFC Fundamentals


controls, you can get a handle to a (CWnd-derived) control with a call to m_hWnd. For
example, if a button on a dialog box initiates the message box, you can start this function
as follows:

::MessageBox(m_hWnd, ...);

We also saw that you can get a pointer to the main window by calling the MFC’s global
AfxGetMainWnd() function. This function only points you to the application. To get a
handle to the application, you can call the same m_hWnd member variable. In this case,
the message box can be started with:

::MessageBox(AfxGetMainWnd()->m_hWnd, ...);

If you are creating a message but do not want a particular window to own it, pass this
hWnd argument as NULL.

Practical Learning: Using a Message Box



  1. Create a new Win32 Project name it MsgBox

  2. Create it as a Windows Application Empty Project

  3. Specify that you want to Use MFC in a Shared DLL

  4. Create a C++ file and name it Exercise

  5. In the empty file, initialize the application as follows:
    #include <afxwin.h>


class CExerciseApp : public CWinApp
{
public:
BOOL InitInstance();
};

CExerciseApp theApp;

BOOL CExerciseApp::InitInstance()
{
return TRUE;
}


  1. Save All


2.3.4 The Box’ Message..................................................................................


For all these functions, the Text argument a null-terminated string. It specifies the text
that would be displayed to the user. This argument is required for all these functions.
Here is an example that use the CWnd::MessageBox() function:

void Whatever()
{
// TODO: Add your control notification handler code here
MessageBox("The name you entered is not in our records");
}
Free download pdf