Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


11.3.1..Introduction..........................................................................................


To create a control, you can use either the CreateWindow() or the CreateWindowEx()
Win32 functions. When doing this, you must specify the control’s name. To help with
this, the Win32 library provides already defined names of classes you can use to create a
control.

We already saw that, when creating a window, you need to declare its handle and
initialize it with the CreateWindow() or the CreateWindowEx() functions. If the
window is created successfully, the function returns a window handle. If the control
created was a parent, its handle can be used as the hWndParent argument. This would be
done as follows:

HWND hWndParent;

// Create the parent window
hWndParent = CreateWindowEx(0, ClassName, StrWndName,
WS_OVERLAPPEDWINDOW,
0, 100, 140, 320,
NULL, NULL, hInstance, NULL);

// Create a window
CreateWindowEx(0, WndClassName, CaptionOrText,
ChildStyle, Left, Top, Width, Height,
hWndParent, NULL, hInstance, NULL);

The reason you declare a handle to the parent window is to allow you to refer to it later
on. In the same way, if you need to refer to any control that is part of your application,
you should define its own handle:

HWND hWndParent, hWndChild;

// Create the parent window
hWndParent = CreateWindowEx(0, ClassName, StrWndName,
WS_OVERLAPPEDWINDOW,
0, 100, 140, 320,
NULL, NULL, hInstance, NULL);

// Create a window
hWndChild = CreateWindowEx(0, WndClassName, CaptionOrText,
ChildStyle, Left, Top, Width, Height,
hWndParent, NULL, hInstance, NULL);

11.3.2..Control Creation Options...................................................................


To allow the user to interact with the computer, you equip your application with the
necessary controls. The controls are positioned on the client area of the parent window.
The easiest technique used to add a control to an application consists of picking it up
from the Controls toolbox and positioning it on a parent window. To help with this,
Visual C++ provides a series or ready-made objects on the Controls toolbox:

Visual C++ 6 Controls Visual C++ 7 Controls
Free download pdf