Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 16: Text-Based Controls Visual C++ and MFC Fundamentals


There are other messages that you can handle through a button.

To close a dialog box, you can use the Win32 API's PostQuitMessage() function. Its
syntax is:

VOID PostQuitMessage(int nExitCode);

This function takes one argument, which is an integer. The argument could be set to
almost any integer value although it should be WM_QUIT. Here is an example:

void CDialog5Dlg::OnBtnClose()
{
// TODO: Add your control notification handler code here
PostQuitMessage(125);
}

Although the MFC provides enough messages associated with the various controls, in
some circumstances you will need use a message that is not necessarily associated with
the control. In such a case, you can call the CWnd::SendMessage() method. Its syntax
is:

LRESULT SendMessage(UINT message, WPARAM wParam = 0, LPARAM lParam = 0);

The first argument of this method can be a Win32 message or constant. Examples would
be WM_CLOSE or WM_ACTIVATE. The wParam and lParam arguments can be
additional (Win32) messages.

The WinExec() function is used to run an application. Its syntax is:

UINT WinExec(LPCSTR lpCmdLine, UINT uCmdShow);

The lpCmdLine argument specifies the name or path of the application you want to
display. The uCmdShow specifies how the application should be displayed. It uses the
same values as the CWnd::ShowWindow() method.

Practical Learning: Using Buttons



  1. Create a new Dialog Based application named AppLauncher

  2. On the dialog, delete the TODO line, the OK, and the Cancel buttons

  3. On the Controls window, click the Button control and click in the lower section
    of the dialog box

  4. Display the button's Properties window and change its IDentifier to
    IDC_BTN_CLOSE

  5. Right-click the button and click Add Variable

  6. Set the Variable Name to m_BtnClose and make sure the Variable Type is CButton

Free download pdf