Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


11.1 Controls Fundamentals...............................................................................


11.1.1..Introduction..........................................................................................


A Windows control, in this book called a control, is an object that displays or is part of an
application and allows the user to interact with the computer. There are various types of
controls as we will see in this book:

?? A text-based control is an object whose main function is to display to, or request
text from, the user

?? A list-based control displays a list of items
?? A progress-based control is used to show the progress of an action
?? a static control can be used to show colors, a picture or something that does not
regularly fit in the above categories

To make your application as efficient as possible, you will add controls as you judge
them necessary. Some and most controls are already available so you can simply
customize their behavior and appearance. Sometimes you will need a control for a
specific task or for a better look. If such a control is not available, you may have to create
your own.

There are two main ways a control is made part of your application. At design time,
which is referred to the time you are visually creating an application, you will select
controls and place them on a host. Another technique, referred to as run time, consists of
creating control programmatically. In this case you must write all the code that specifies
the control's appearance and behavior.

Practical Learning: Introducing Controls



  1. Start Visual Studio or Visual C++ and create a new Empty Win32 Project named
    Win32C

  2. Create a C++ Source file named Exercise and type the following in it:


#include <windows.h>
//---------------------------------------------------------------------------
char StrClassName[] = "WndCtrls";
char StrWndName[] = "Windows Controls";
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
MSG Msg;
HWND hWnd;
WNDCLASSEX WndClsEx;

WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
Free download pdf