11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13. Adding Special Features to Application<br />

Member style specifies window styles, by setting different bits of this member we can create different<br />

types of windows. There are many styles that can be combined together, two most often used styles are<br />

CS_HREDRAW and CS_VREDRAW, which will cause the client area to be updated if user resizes the window in<br />

either horizontal or vertical direction. Member lpfnWndProc points to a callback function that will be used<br />

to process incoming messages. When a window is created, it should contain several default objects: 1) icon,<br />

which will be used to draw the application when it is minimized; 2) cursor, which will be used to customize<br />

the mouse cursor when it is located within the client window of the application; 3) default mainframe<br />

menu; 4) brush, which will be used to erase the client area (This brush specifies the background pattern of<br />

the window). The above objects are described by following members of structure WNDCLASS respectively:<br />

hIcon, hCursor, hbrBackground and lpszMenuName.<br />

Another very important member is lpszClassName, which describes the type of the window we will<br />

create. Every window under Windows has a class name. Before creating a new type of window, we must<br />

register its class name to the system. After that we can use this class name to implement an instance of<br />

window. A class name is simply a string, which can be specified by the programmer.<br />

If we write windows program in C, we must go through the following procedure in order to create a<br />

new window: register the window class name, implement a message handling routine, use the registered<br />

window class name to implement a new window instance. In <strong>MFC</strong>, this procedure is hidden behind the<br />

classes, when we use a class derived from CWnd to declare a variable, the class registration is completed<br />

sometime before the window is created. Also, we do not need to provide message handling routines<br />

because there exist default message handlers in <strong>MFC</strong>. If we want to trap certain messages, we can add<br />

member functions and use message mapping macros to associate the messages with functions.<br />

It is relatively easy to implement one-instance application by programming in C: before registering a<br />

window class, we can first find out if there exists any instance implemented by the same type of window<br />

class (This class has nothing to do with class in C++) in the system. If so, we simply exit and do not go on<br />

to create a new window. If not, we will implement the new window.<br />

However, in <strong>MFC</strong>, we do not see how window class is registered, so it is difficult to manipulate it.<br />

Also, in <strong>MFC</strong>, all the window class names are predefined, so we actually can not modify them. In order to<br />

create one-instance application, we need to discard the default registered window class name, and use our<br />

own class name to create new instance. By doing so, we are able to check if there already exists an instance<br />

of this window type before creating a new one.<br />

Function CWnd::PreCreateWindow(…)<br />

The styles of a window (including the class name) can be modified just before it is created. In <strong>MFC</strong>,<br />

function CWnd::PreCreateWindow(…) can be overridden for this purpose.<br />

The input parameter of CWnd::PreCreateWindow(…) is a CREATESTRUCT type variable, which is passed<br />

to the function by reference:<br />

virtual BOOL CWnd::PreCreateWindow(CREATESTRUCT& cs);<br />

Structure CREATESTRUCT contains a variety of window styles:<br />

typedef struct tagCREATESTRUCT {<br />

LPVOID lpCreateParams;<br />

HANDLE hInstance;<br />

HMENU hMenu;<br />

HWND hwndParent;<br />

int cy;<br />

int cx;<br />

int y;<br />

int x;<br />

LONG style;<br />

LPCSTR lpszName;<br />

LPCSTR lpszClass;<br />

DWORD dwExStyle;<br />

} CREATESTRUCT;<br />

We can specify a new menu and use it as the mainframe menu. We can set the initial size and position<br />

of the window. We can also specify window name, and customize many other styles. Within the structure,<br />

392

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!