Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 3: Windows Resources Visual C++ and MFC Fundamentals


3.9 Other Techniques of Creating Windows.....................................................


3.9.1 Window Registration and Standard Resources..................................


The CFrameWnd::Create() method we have used so far provides a quick mechanism to
create a window. Unfortunately, it is not equipped to recognize custom resources. In
reality, this method is only used to create a window, not to receive resources. As we saw
in its syntax, its first argument, lpszClassName is used to get a window that has been
built, that is, a window whose resources have been specified. If you do not specify these
resources, that is, if you pass the lpszClassName argument as NULL, the compiler would
use internal default values for the window. These default are: a window that can be
redrawn when the user moves or resizes it, a white background for the main area of the
window, the arrow cursor, and the Windows "flat" icon, called IDI_APPLICATION.

To use your own resources, you must first create then register them. To register the
resources, you can call the AfxRegisterWndClass() global function. Its syntax is:

LPCTSTR AFXAPI AfxRegisterWndClass(UINT nClassStyle,
HCURSOR hCursor = 0,
HBRUSH hbrBackground = 0,
HICON hIcon = 0);

As you can see, this function is used to register, the window style, a cursor, a color for the
background, and an icon. After registering the window's style or the style and the
resources, this function returns a (constant) string that can be passed as the first argument
of the CFrameWnd::Create() method.

Practical Learning: Using Standard Resources



  1. To use the AfxRegisterWndClass() function with default values, change the
    Exercise.cpp file as follows:


class CResFrame : public CFrameWnd
{
public:
CResFrame()
{
const char *RWC = AfxRegisterWndClass(NULL, NULL,
(HBRUSH)::GetStockObject(WHITE_BRUSH),
NULL);
Create(RWC, "Resources Fundamentals", WS_OVERLAPPEDWINDOW,
CRect(200, 120, 640, 400), NULL);
}
};


  1. Test the application

  2. After viewing the window, close it and return to MSVC

  3. To use a standard cursor and a standard icon, change the Exercise.cpp file as follows:
    CResFrame()
    {

Free download pdf