Partager via


Window Objects

MFC supplies class CWnd to encapsulate the HWND handle of a window. The CWnd object is a C++ window object, distinct from the HWND that represents a Windows window but containing it. Use CWnd to derive your own child window classes, or use one of the many MFC classes derived from CWnd. Class CWnd is the base class for all windows, including frame windows, dialog boxes, child windows, controls, and control bars such as toolbars. A good understanding of the relationship between a C++ window object and an HWND is crucial for effective programming with MFC.

MFC provides some default functionality and management of windows, but you can derive your own class from CWnd and use its member functions to customize the provided functionality. You can create child windows by constructing a CWnd object and calling its Create member function, then customize the child windows using CWnd member functions. You can embed objects derived from CView, such as form views or tree views, in a frame window. And you can support multiple views of your documents via splitter panes, supplied by class CSplitterWnd.

Each object derived from class CWnd contains a message map, through which you can map Windows messages or command IDs to your own handlers.

The general literature on programming for Windows is a good resource for learning how to use the CWnd member functions, which encapsulate the HWND APIs.

Functions for Operating On a CWnd

CWnd and its derived window classes provide constructors, destructors, and member functions to initialize the object, create the underlying Windows structures, and access the encapsulated HWND. CWnd also provides member functions that encapsulate Windows APIs for sending messages, accessing the window's state, converting coordinates, updating, scrolling, accessing the Clipboard, and many other tasks. Most Windows window-management APIs that take an HWND argument are encapsulated as member functions of CWnd. The names of the functions and their parameters are preserved in the CWnd member function. For details about the Windows APIs encapsulated by CWnd, see class CWnd.

CWnd and Windows Messages

One of the primary purposes of CWnd is to provide an interface for handling Windows messages, such as WM_PAINT or WM_MOUSEMOVE. Many of the member functions of CWnd are handlers for standard messages — those beginning with the identifier afx_msg and the prefix "On," such as OnPaint and OnMouseMove. Message Handling and Mapping covers messages and message handling in detail. The information there applies equally to the framework's windows and those that you create yourself for special purposes.

What do you want to know more about?

See Also

Concepts

Windows