WTL in MFC

zhaozj2021-02-16  83

http://www.codeproject.com/wtl/mix_wtl_mfc.asp?df=100&Forumid=14171&exp=0&select=666091# xx666091xx

What is it?

That Article Presents A Way To Use WTL Template Classes ON MFC Window Classes, That Is How To Transform The MFC CWnd Class To Its ATL / WTL Counterpart Cwindow, While Leaving The Class Usable from MFC Code.

Here You Can Find The Required Files, a Detailed Explanation In 10 Steps and A Working Demo.

How to do it, step by step

The Basic Steps To Obtain Hybrid MFC / WTL Windows. They Have Been Used ON MFC Dialog Based Projects, But Should Work with Any MFC Application.

Add WTL Support to an MFC Application

Take An Mfc Project You Want To Modify SO That It Supports ATL / WTL Functions and Classes. Open ITS Precompiled Header File (Usually "stdafx.h") Andd The Following Lines: // Add Support for ATL / WTL

#define _WTL_NO_AUTOMATIC_NAMESPACE This prevents the WTL headers to automatically merge the WTL namespace to the global namespace. This avoids conflicts with MFC classes with the same names, such as CRect, CDC and others. # include

#include

extern WTL :: CAppModule _Module; The ATL / WTL code may access the global _Module variable, so it must have external linkage # include We add the common WTL header here to exploit precompiled headers, but you may want to. INCLUDE It Only You Really Need It. Open the Project Main File, Where The CWINAPP-DERIVED CLASS IS IMPEMENTED, AND MODIFY IT AS FOLLOWS: /

// the one and only cmixedWindowApp Object

CmixedWindowapp theApp;

WTL :: CappModule _Module; // add this line the global _module variable must be defined Somewhere and this is a good place. Modify initInstance () as stock: /// cmixedWindowApp Initialization

Bool cmixedwindowapp :: initInstance ()

{

// Initialize ATL

_Module.init (null, afxgetinstancehandle ());

...

} If not present, add the the application Object, for example using classwiz: int cmixedwindowapp :: exitIndingwindowapp :: exitInstance ()

{

// Terminate ATL

_Module.Term ();

Return CWINAPP :: EXITINSTANCE ();

}

Make an Hybrid WINDOW CLASS

Assume we want to add scrolling capabilities to a static bitmap control. We may want to use the WTL template class CScrollImpl <...> together with the MFC class CStatic, as there is no such a window class in MFC.

Make a new class, derived from CStatic (or any other CWnd-derived class), for example using ClassWizard Open the class header file and include any WTL header you need:. // Add support for scrolling windows

#include Remember that if you did not choose to use the precompiled header at point 3 above, you need to add that line here, before any other WTL header: #include Include the necessary header To Turn A CWND Class Into a CWindow. It Contains a Template Class That Defines The Missing Members: // Make This Class Cwindow Compatible

#include "Wnd2Window.h" You may want to put the above line into your precompiled header instead, like all the other WTL headers, if you use them many times, to speed up recompilation. That's completely at your choice. Then modify the class declaration to make the conversion to CWindow and to use the WTL template class you have chosen: class CScrollPicture: public CWnd2Window , public WTL :: CScrollImpl Add any other function the WTL class may require, paying attention to Specify The WTL Namespace for ITS Arguments. in this Example We need to import dopaint () to work with scrolling: public:

// Inline Helper

Void Dopaint (wtl :: CDCHANDLE DC)

{

OnDraw (CDC :: fromHandle (DC));

}

protected:

//Mplement Painting with Scroll Support

Void OnDraw (CDC * PDC);

Notes on Implementation

It may be a good idea not to mix the code too much with MFC and WTL classes used anywhere, as it could become quite confusing. An efficient way could be defining inline helper functions that translates WTL arguments to their MFC counterpart, unless you want to .

Using helper functions could also make writing "bridge" classes easier For example, you could define a CScrollWnd class to reuse (through inheritance) whenever you need scrolling capabilities and custom drawing, declaring a virtual OnDraw () function like in CView and CScrollView.:

PUBLIC:

// Inline Helper

Void Dopaint (wtl :: CDCHANDLE DC)

{

OnDraw (CDC :: fromHandle (DC));

}

protected:

// Must Be Implement In Derived Class

Virtual Void OnDraw (CDC * PDC) = 0; i buy a pure Virtual Function, But CView Defines A base Implementation That Just Some Ideas, But Feel Free to Suggest New.

Sounds Good ... But how does it work?

Well, nothing special or mysterious. The ATL class CWindow, which is used by WTL, has only one member variable, m_hWnd, that is exactly the same as the one you can find in the CWnd class. So all the CWindow member functions need just That Member Variable, That Can Also Be found in a cwnd-deerived class.

What i Did Was To Copy All The CWindow Members, Except M_HWnd, To a include file and define a new template class.

Template Class Cwnd2window: Public TBASE

To use the template You need to pass both the derived class and the base class, as you can see in the example Above About a scrolling control.

THEN IIABLE WTL Message Maps. I Implement The MFC Function DefWindowProc (), That Is Called After WindowProc (), When No Entry Is Found In The MFC Message Map.

protected:

Virtual Lresult DefWindowProc (Uint NMSG, WPARAM WPARAM, LPARAM LPARAM)

{

T * pt = static_cast (this);

Atlassert (:: iswindow (pt-> m_hwnd);

LRESULT LRESULT;

IF (Pt-> ProcessWindowMessage (M_HWND, NMSG, WPARAM, LPARAM, LRESULT)

Return LRESULT;

Return TBASE :: DefWindowProc (NMSG, WPARAM, LPARAM);

}

Remember that WindowProc () is called first. If it is not overridden or if you pass the message to the base implementation in CWnd, the MFC message handler is called. If the corresponding entry is not found in the MFC message map or if you call the base implementation in your message handler or if you explicitly call Default (), the message finally arrives to DefWindowProc () that calls the ATL / WTL message map implementation.Note that you do not have to change the MFC message map macros, that Skip to the mfc base class ignoring the interfaceow. I don't define an mfc message map there, so this ispectly legal.

I Also Commented Out "Dangerous" Member Functions Already Defined by MFC, Such As Attach (), Detach () () () () () () () () () () () () () () () () () () () () () (), CreateWindow () and destroyWindow (), But there may be Others to Comment Out I'm Not aware of. As far as I know Attach / Detach deal with CWnd maps, while CreateWindow / DestroyWindow set up Windows hooks or call some virtual functions. So if you still want to use your class as a CWnd, you need to call the original functions, not those defined by CWindow .

There May Be Also Some Functions Identical To The MFC Ones, That Could Be Removed. Feel Free To Suggest Improvements.

So far, so good ... but what changes?

Well, you have to pay attention to a few strings:

You need to explicitly specify namespaces when accessing WTL objects, which is not a bad practice anyway. And you may find that the compiler complains about some member functions of your hybrid classes. That's because many CWnd and CWindow members have the same name and you must Resolve the Ambiguity to Compile.

That's all. I Just Used this Method a Couple of Times, SO I'm NOT Aware of Any Other Problems. But I Expect Comments ... license

I suppose you can freely use the code as long as you have the right to use ATL source code. There is no original code here, except for the CScrollPicture class, only a nice idea. All the source code by me is released to the public Domain, You May Do What You Want With It. As for the CWnd2Window Class, Almost All The Core Is Copyright of Microsoft Corporation.

The demo project

In the attached demo project you can find a very simple implementation of a scrolling picture control. Since I subclass a CStatic control, I need to override the default behaviour sometimes. A much cleaner implementation would have used a CWnd, but that way I can show That Message Routing Works As Expected, Even Mixing MFC Message Handlers with WindowProc ().

To test the control use the mouse on the scroll bars or move the focus on it and use the keyboard arrows, holding down the CTRL key to scroll by pages. There is no focus indicator, so you have to guess by exclusion. There is also A Button to Load External Bmp Files.

Please note that this control is not meant to be fully featured, but only an example of use for WTL templates in MFC projects. In particular, I expect bugs if the control is used in a resizable dialog, as to my experience the WTL classes that Implement Scrolling Are Not "Perfect" from That Point Of View. I'm Working on A Replacement Class, But no time to release it yet.

Any Idea to Improve this Article, please let me know!

转载请注明原文地址:https://www.9cbs.com/read-10483.html

New Post(0)