Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 12: Dialog-Based Windows Visual C++ and MFC Fundamentals


NULL);

if( !hWnd ) // If the window was not created,
return 0; // stop the application
}

For an MFC application, a Create() method returns TRUE if the window was created.
Otherwise, it returns FALSE. Therefore, you can use an if conditional statement to find
out whether the window was successfully created or not. Here is an example:

CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;
const char *StrWndName = "Windows Fundamentals";

WndCls.style = CS_VREDRAW | CS_HREDRAW;
WndCls.lpfnWndProc = AfxWndProc;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = AfxGetInstanceHandle();
WndCls.hIcon = AfxGetApp()->LoadStandardIcon(IDI_WARNING);
WndCls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
WndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndCls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_CROSS);
WndCls.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

const char *StrClass = AfxRegisterWndClass(WndCls.style, WndCls.hCursor,
WndCls.hbrBackground, WndCls.hIcon);

if( !Create(StrClass, StrWndName, WS_OVERLAPPEDWINDOW, rectDefault,
NULL, MAKEINTRESOURCE(IDR_MENU1)) )
return;
}

Practical Learning: Finalizing Window Creation



  1. To finalize the creation of the window, change the WinMain() function as follows:


//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
HWND hWnd;
WNDCLASSEX WndClsEx;

WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProc;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hInstance = hInstance;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_CROSS);
WndClsEx.hbrBackground = (HBRUSH)( COLOR_BACKGROUND + 1);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = StrClassName;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
Free download pdf