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 />

Chapter 13<br />

Adding Special Features to Application<br />

Normal applications created by Application Wizard cannot satisfy us all the time. For certain types<br />

of applications, we need to add special features to our programs. Since Application Wizard or<br />

Class Wizard does not directly support these features, we need to have in-depth knowledge on<br />

Windows programming in order to customize standard applications. In this chapter, we will<br />

discuss how to create applications with special features such as multiple documents, multiple views,<br />

irregular-shaped window, customized non-client area. Also, we will discuss how to implement hook in the<br />

applications.<br />

13.1 One Instance Application<br />

By default, a Windows application is allowed to have multiple instances working simultaneously.<br />

Most of the time this is the desired feature of an application. For example, a word processing program may<br />

have several instances working together, each editing a different file. But sometimes we may want an<br />

application to have only one instance working at any time, this is especially true for some communication<br />

programs. For example, for a file server application, if we allow two servers to work together at the same<br />

time, it may cause inconsistency on the data contained in the files.<br />

Window Creation<br />

To implement one instance application, we must understand how the applications are created under<br />

Windows. This can be easily understood if we have the experience of writing Win32 Windows<br />

applications. However, if we started everything from <strong>MFC</strong>, it is not very obvious how a window is created.<br />

This is because <strong>MFC</strong> hides everything from the programmer. Although it is relatively easy to create an<br />

application by deriving classes from <strong>MFC</strong> without thinking about the actual procedure of creating<br />

windows, if we rely too much on <strong>MFC</strong>, we will lose the power of customizing it.<br />

Every visual object that is created under Windows is a window. This includes the frame window,<br />

tool bar, menu, view, button and other controls. Actually, <strong>MFC</strong> is not the only tool that can be used to<br />

create windows. A window can be created by using any computer language such as C, Basic, Pascal so long<br />

as it abides by the rules of creating windows.<br />

Under Windows, a window can be described by structure WNDCLASS:<br />

typedef struct _WNDCLASS {<br />

UINT style;<br />

WNDPROC lpfnWndProc;<br />

int cbClsExtra;<br />

int cbWndExtra;<br />

HANDLE hInstance;<br />

HICON hIcon;<br />

HCURSOR hCursor;<br />

HBRUSH hbrBackground;<br />

LPCTSTR lpszMenuName;<br />

LPCTSTR lpszClassName;<br />

} WNDCLASS;<br />

391

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

Saved successfully!

Ooh no, something went wrong!