Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


CreateWindow() or CreateWindowEx() functions. Otherwise, pass this argument as
NULL.

If you are creating an MFC application and want the main window to display a menu,
design the menu as you see fit and create its resource. Then pass the menu name as the
lpszMenuName argument of the Create() method. If you want to use the identifier of the
menu, pass its ID to the MAKEINTRESOURCE macro. This can be done as follows:

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);

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

To make sure that the window was created, you can check its return value.

If the CreateWindow() or the CreateWindowEx() functions of a Win32 application
succeeds in creating the window, it returns a handle to the window that was created. You
can check this validity using an if conditional statement. Here is an example:

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndCls;

...


RegisterClass(&WndCls);

hWnd = CreateWindow(ClsName,
WndName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
Free download pdf