Table of contents
1.
Introduction
2.
CWindowImpl
3.
Implementing a Window with CWindowImpl
3.1.
Uses of Windows Class
3.1.1.
Create a Window derived from a New Windows Class
3.1.2.
Superclassing an Existing Windows Class
3.1.3.
Subclassing an Existing Window
4.
Frequently Asked Questions
4.1.
What is the use of CWindowImpl Class?
4.2.
What does ATL stand for?
4.3.
What is a class data value?
4.4.
Why do we need ATL?
4.5.
List the difference between class and instance.
5.
Conclusion
Last Updated: Mar 27, 2024

Implementing a Window with CWindowImpl

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The ATL (Active Template Library) is designed to support COM(Component Object Model). It contains a range of classes for modeling windows. You can use these classes for COM objects with windows, such as ActiveX Controls and Windows applications that do not necessarily involve COM

Implementing a Window with CWindowImpl

Let's now briefly discuss one of the most important ATL windowing classes, CWindowImpl.

CWindowImpl

You can use CWindowImpl to create a window based on a new Windows class, superclass an existing class, or subclass an existing window. To allow you to process window messages in a CWindowImpl-derived class, ATL inherits from the abstract base class CMessageMap via CWindowImplRoot.

CWindowImpl

CWindowImpl inherits functionality from parent classes- CWindowImplBaseT and CWindowImplRoot. CWindowImpl itself implements only the Create function.

Let's now discuss some examples of CWindowImpl.

Example

Out of three parameters of class T, derived from CWindowImpl, only the first is required, and rest two are optional.

template <class T, class TBase =     CWindow, class TWinTraits = CControlWinTraits> class ATL_NO_VTABLE CWindowImpl :     public CWindowImplBaseT< TBase, TWinTraits >

Where-

is a derived class from CWindowImpl.

CWindow is generally used for TBase, but you can provide your own CWindow derivative.

TBase is the base class that contains the m_hWnd data member.

Implementing a Window with CWindowImpl

To implement a window that uses an entirely new window class (not a C++ class), first derive a class from CWindowImpl. After that, in your derived class, declare a message map and the message handler functions. 

The following code section shows how to declare a basic application frame window in ATL:

class CMainFrame : public CWindowImpl<CMainFrame, CWindow,     CFrameWinTraits>   { public:     CMainFrame();     virtual ~CMainFrame(); DECLARE_WND_CLASS(NULL);     BEGIN_MSG_MAP(CMainFrame) END_MSG_MAP() };

The resulting window will have the predefined ATL frame window traits when created. Hence, it will get the following styles and extended styles:

WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,     WS_EX_APPWINDOW | WS_EX_WINDOWEDGE

Uses of Windows Class

As discussed earlier that we can use our window class in three different ways:

Create a Window derived from a New Windows Class

The DECLARE_WND_CLASS macro in CWindowImpl can be used to declare Windows class information. The GetWndClassInfo function, which uses CWndClassInfo to specify the information of a new Windows class, is implemented by this macro. A new window is produced when CWindowImpl::Create is called.

Superclassing an Existing Windows Class

You can construct a window that superclasses an already-existing Windows class using the DECLARE WND SUPERCLASS macro. Include this macro in your CWindowImpl- derived class. Messages are managed using a message map, just like any other ATL window.

Subclassing an Existing Window

To subclass an existing window, derive a class from CWindowImpl and declare a message map, as in the two previous cases. Note, however, that you do not specify any Windows class information since you will subclass an already existing window.

Frequently Asked Questions

What is the use of CWindowImpl Class?

Class CWindowImpl helps you to implement a window and handle its messages.

What does ATL stand for?

ATL stands for the active template library.

What is a class data value?

Information that is shared by all the instances or aggregate information about the instances is kept in a class data value.

Why do we need ATL?

We need ATL to provide efficient and easy-to-implement COM objects.

List the difference between class and instance.

The class is similar to the blueprint. The Object is a real item that was created using the "blueprint" (like the house). An instance represents an item that is virtual but not a true copy.

Conclusion

In this blog, we have discussed the implementation of a Window class derived from CWindowImpl. We also discussed its uses with some examples.

Check out the following articles to learn more about ATL and its subclasses.

 

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Code Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass