WTL (Windows Template Library [from ATL])

Note: How to make WTL available

Local

External

Misc (External)


Adding a Dialog Box



Menu Info

Adding a Menu to a window

  1. Goto resource view and create the menu.
  2. Load the menu like this:
    CMenu menu;
    menu.Attach( LoadMenu( _Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MENU1)));
  3. Include the menu in the windows create
    TestWin.Create( NULL, rcPos, _T("Name Goes Here"), 0, 0, (HMENU)menu);
    
  4. Add handlers to the Message map:
    BEGIN_MSG_MAP(CATLWindow)
    	MESSAGE_HANDLER( WM_CLOSE, OnClose)
    	// to link a method per item:
    	COMMAND_ID_HANDLER( ID_MENU_ITEM_1, OnMenuItem1)
    	COMMAND_ID_HANDLER( ID_MENU_ITEM_2, OnMenuItem1)
    	// to make a handler for all messages at once:
    	MESSAGE_HANDLER( WM_COMMAND, OnMenu)
    END_MSG_MAP()
  5. Here is what a COMMAND_ID_HANDLER func looks like:
    LRESULT OnMenuItem1( WORD code, WORD id, HWND hwnd, BOOL& bHandled)
    {
    	return 0;
    }
    
  6. Here is what a MESSAGE_HANDLER func looks like:
    LRESULT OnMenu( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
    {
    	PostQuitMessage(0);
    	return 0;
    }
    
  7. That's it

Gotchas


Optimizations


// If Default buttons are removed, remember to remove (and the functions):
	COMMAND_HANDLER(IDOK, BN_CLICKED, OnClickedOK)
	COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)

NOTE: This no longer appears to be valid


// If ActiveX controls don't need to be hosted,
// This:
class CTestDialog : 
	public CAxDialogImpl<ctestdialog>
// Can be replaced with this:
class CTestDialog : 
	public CDialogImpl<ctestdialog>

Message Map Macros

Note: Set bHandled to false if you would like to tell the message handling system to continue passing the message on to other handlers (or leave it true to signal that you have processed the message, and no other handlers need to see it).

Message Map


BEGIN_MSG_MAP(CMainCtrl)
	MESSAGE_HANDLER( WM_PAINT, OnPaint)
	MESSAGE_HANDLER( WM_SETFOCUS, OnSetFocus)
	MESSAGE_HANDLER( WM_KILLFOCUS, OnKillFocus)
	MESSAGE_HANDLER( WM_CREATE, OnCreate)
	// Routes other messages to the chained MSG_MAP:
	CHAIN_MSG_MAP(CMoreMainCtrl)
ALT_MSG_MAP(1)
	MESSAGE_HANDLER( WM_LBUTTONDBLCLK, OnLButtonDblClick)
ALT_MSG_MAP( {AltMsgMapIdNumber} )
	...
END_MSG_MAP

Handlers

MacroParameters
MESSAGE_HANDLERmsg, func
MESSAGE_RANGE_HANDLERmsgFirst, msgLast, func
COMMAND_HANDLERid, code, func
COMMAND_ID_HANDLERid, func
COMMAND_CODE_HANDLERcode, func
COMMAND_RANGE_HANDLERidFirst, idLast, func
NOTIFY_HANDLERid, code, func
NOTIFY_CODE_HANDLERcode, func
NOTIFY_RANGE_HANDLERidFirst, idLast, func

Handler Parameters

ParameterDescription
msgThe message to handle.
funcThe handler function.
msgFirst,
msgLast
A range of messages to handle. (?inclusive?)
idThe identifier for the menu item, control, or accelerator.
codeThe notification code.
idFirst,
idLast
A range of identifiers of message sources whose messages will be handled

Function Signatures

Handler TypeFunction Signature
MESSAGELRESULT MessageHandler( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
COMMANDLRESULT CommandHandler( WORD wNotifyCode, WORD wId, HWND hWndCtl, BOOL& bHandled)
NOTIFYLRESULT NotifyHandler( int idCtrl, LPNMHDR pnmh, BOOL& bHandled)

Chaining Message Maps

MacroParametersDescription
CHAIN_MSG_MAPtheChainClassRoutes the message to the default message map in a base class.
CHAIN_MSG_MAP_ALTtheChainClass,
msgMapID
Routes the message to an alternate message map in a base class.
CHAIN_MSG_MAP_MEMBERtheChainMemberRoutes the message to the default meassge map in a data member.
CHAIN_MSG_MAP_ALT_MEMBERtheChainMemberRoutes the message to an alternate meassge map in a data member.
CHAIN_MSG_MAP_DYNAMICdynaChainIdRoutes the message to the default message map of a member that is determined at runtime

Chaining Message Maps - Parameters

ParameterDescription
theChainClassBase class containing the message map.
msgMapIdAlternate message map identifier.
theChainMemberA data member that has a message map.
dynaChainIdIdentifies an object with a message map.

To use CHAIN_MSG_MAP_DYNAMIC the class defining the MSG_MAP must derive from CDynamicChain. It should use
BOOL SetChainEntry( DWORD dwChainId, CMessageMap* pObject, DWORD dwMsgMapId = 0)
to set where chainging for the registered id needs to go (the destination can be set [?and changed?] dynamically at run time)

Reflection

How is it used?? Somehow sends the original message plus OCM_BASE and sends it back to the child control that sent it.

MacroDescription
DEFAULT_REFLECTION_HANDLERA default handler for reflected messages that you can place in the message map of the child control.
REFLECT_NOTIFICATIONSWhen encountered in the message map of the parent, this macro will cause a message to be reflected back to the child conrol whose identifier is specified in the message.

Example


// DO NOT USE #include <windows.h>

// This part can go into stdafx.h:
#include <atlbase.h>
extern CComModule _Module;
#include <atlwin.h>
#include <atlcom.h>
//old way:		#include <atlcontrols.h>
//old way:		using namespace ATLControls;
#include <atlapp.h>
#include <atlctrls.h>
using namespace WTL;

// This should go into a .cpp file:

CComModule _Module;

class CATLWindow :
	public CWindowImpl<CATLWindow, CWindow,
		CWinTraits<WS_SIZEBOX | WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0>
	>
{
public:
	CATLWindow()
	{
	}
	
	~CATLWindow()
	{
		if (::IsWindow(m_hWnd) ) {
			DestroyWindow();
		}
	}
	
	BEGIN_MSG_MAP(CATLWindow)
		MESSAGE_HANDLER( WM_CLOSE, OnClose)
	END_MSG_MAP()
	
	LRESULT OnClose( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		PostQuitMessage(0);
		return 0;
	}
};

int APIENTRY WinMain( HINSTANCE hInst, HINSTANCE /*Prev*/, LPSTR lpCmdLine, int nCmdShow)
{
	_Module.Init( NULL, hInst);
	
	CATLWindow TestWin;
	RECT rcPos = { 0, 0, 300, 100 };
	TestWin.Create( NULL, rcPos, _T("Name Goes Here"));
	TestWin.ShowWindow(SW_SHOW);
	
	MSG msg;
	while( GetMessage(&msg, 0, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return (int) msg.wParam;
}


How to make WTL available

Get the header file atlcontrols.h and put it somewhere reachable, ...\Vc7\include


Wrapper Classes, list of

Main Wrappers

Table 1: Wrapper classes
Class Win32 class name Description
CStatic STATIC Static label
CButton BUTTON Command button
CListBox LISTBOX List box
CDragListBox LISTBOX List box with support for dragging items
CComboBox COMBOBOX Combo box
CEdit EDIT Text box
CScrollBar SCROLLBAR Scroll bar
CRichEditCtrl RICHEDIT Rich edit control
CListViewCtrl SysListView32 List view, like the right-hand pane of Windows Explorer
CTreeViewCtrl SysTreeView32 Tree view, like the left-hand pane of Windows Explorer
CTreeViewCtrlEx SysTreeView32 Same as CTreeViewCtrl but uses the wrapper class CTreeItem rather than the raw HTREEITEM
CHeaderCtrl SysHeader32 Headers with resizable columns
CToolBarCtrl ToolbarWindow32 Tool bar positioned below a menu
CStatusBarCtrl msctls_statusbar32 Status bar positioned at the bottom of a window
CTabCtrl SysTabControl32 Tabs, like those used in a tabbed dialog
CToolTipCtrl tooltips_class32 Tool tip
CTrackBarCtrl msctls_trackbar32 A slider control used to set a value
CUpDownCtrl msctls_updown32 A control used to increment and decrement values
CProgressBarCtrl msctls_progress32 Visual progress indication
CHotKeyCtrl msctls_hotkey32 Used to specify that a key combination will perform some action
CAnimateCtrl SysAnimate32 Control used to play an AVI
CReBarCtrl ReBarWindow32 The fancy toolbars used by IE4
CComboBoxEx ComboBoxEx32 A combo box that can have images for the items
CDateTimePickerCtrl SysDateTimePick32 An edit box-like control through which you can pick a date or time
CMonthCalendarCtrl SysMonthCal32 Control that shows a month’s dates
CIPAddressCtrl SysIPAddress32 An edit box-like control with four fields separated by dots
CPagerCtrl SysPager A control that allows you to ”scroll” another window

Auxilliary Wrappers

Table 2: Utility classes
Class Description
CImageList Wraps an HIMAGELIST for use with list views and tree views
CTreeItem Wraps a HTREEITEM, used with CTreeViewCtrlEx
CToolInfo Wraps a TOOLINFO, used with CToolTipCtrl
CDragListNotifyImpl Class used to handle notifications from a drag list box
CFlatScrollBar Changes a window to use flat control bars
CCustomDraw Class used to add custom draw support