Click here to Skip to main content
13,201,940 members (73,418 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 11 Jun 2002

Ten WTL Tips and Tricks

, 11 Jun 2002
Rate this:
Please Sign up or sign in to vote.
Ten quick and easy tips and tricks for use when developing WTL applications

WTL Splitters and Panes Image


This article presents ten (actually eleven; there's a bonus tip) quick and easy tips and tricks for use when developing WTL applications. The tips range from how to control the size and placement of the application's main window to displaying strings and integers in controls. A sample project that implements all of the tips is available for download from the link above. The ten tips are:

  • Set main window creation size
  • Center main window on startup
  • Limit main window min/max size
  • Dynamically load main window title
  • Set toolbar to flat-style
  • Set dialog text and background color
  • Swap dialog button positions
  • Set flat-style header for ListView
  • Display integer value in control
  • Display resource string in control

Main Window Tips

The following tips will work with either SDI or MDI applications.

1. Creation Size

Use the following technique in the application .cpp file Run() function to control the main window creation size. Start by initializing a rect with the size desired for your main window and then pass the rect as the second parameter to CreateEx() as illustrated below. The new code to add is shown in bold.

[__b__]RECT rc = {0, 0, 380, 265};
if(wndMain.CreateEx(NULL, rc) == NULL)

2. Center on Desktop

To center the main window in the desktop area, add the following line to the application .cpp file Run() function just before the ShowWindow() command.


3. Limit Min/Max Size

If you would like to control the minimum and maximum window size of your main window, add the following message handler to the CMainFrame message map in your mainframe.h header file.


To complete the implementation, add this handler routine to the file:

{ // load size structure with lParam values

  // change the values in the size structure to desired values
  lpMMI->ptMinTrackSize.x = 200; // min width
  lpMMI->ptMinTrackSize.y = 150; // min height
  lpMMI->ptMaxTrackSize.x = 600; // max width
  lpMMI->ptMaxTrackSize.y = 450; // max height

  return 0; }

4. Dynamically Set Title

It is possible to dynamically load a resource string to the titlebar by passing the resource ID to a CString and then setting the mainframe window text with the string. Adding the following code to the mainframe OnCreate() routine will accomplish that task. Add atlmisc.h to your project so that CString is defined. You can load a maximum of 255 characters with LoadString().

CString str;

5. Flat-style Toolbar

Toolbars created by the WTL application wizard have standard buttons unless the rebar option is also selected. If you want a flat-style toolbar without rebars, add the following code to the mainframe OnCreate() routine just after the toolbar is created.

CToolBarCtrl tool = m_hWndToolBar;
tool.ModifyStyle(0, TBSTYLE_FLAT);

Dialog Tips

The following tips will work with any dialog or dialog-based application. The image below shows the About dialog from our sample project, which implements both tips.

About Dialog

6. Dialog Text and Background Color

This tip provides a quick and easy way to change the text color and/or background color of your dialogs. In the About dialog contained in the sample project included with this article, we set the text color to white using SetTextColor. The background color is set to black using what is known as a "stock brush". The first step is to add these two message handlers to the dialog's message map:


The second step for changing text and background color is shown in the following OnCtrlColor() handler. The background mode is set to transparent so that static and group box control text displays properly. Next, the text is set to the desired RGB color and finally, the desired shade of background brush is returned for use by the painting routine.

Add atlmisc.h to your project so that AtlGetStockBrush() is available. Stock brush choices include WHITE_BRUSH, LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, and BLACK_BRUSH. For other colors, you would have to create a non-stock brush.

{ // set background mode and text color
  SetBkMode((HDC)wParam, TRANSPARENT); // transparent background
  SetTextColor((HDC)wParam, RGB(255, 255, 255)); // white text

  return (LRESULT)AtlGetStockBrush(BLACK_BRUSH); }

7. Dynamically Swap Button Positions

The following code from the OnInitDialog() method swaps the positions of Ok and Cancel buttons on the About dialog. A key point is the translation of absolute screen positions to values relative to the client.

CButton bOk = GetDlgItem(IDOK));
CButton bCancel = GetDlgItem(IDCANCEL));

// get the button rects
RECT rcOk, rcCancel;

// swap the button positions
bOk.SetWindowPos(NULL, &rcCancel, SWP_NOZORDER | SWP_NOSIZE);
bCancel.SetWindowPos(NULL, &rcOk, SWP_NOZORDER | SWP_NOSIZE);

Control Tips

Tip 8 pertains to report-style listview controls while tips 9 and 10 apply to any controls that accept text, such as edit and rich edit controls.

8. Flat-style ListView Header

In order to change the header control associated with a report-style listview control (LVS_REPORT style is set) to a neater appearing flat look, obtain the header control object and modify its style as shown below.

CHeaderCtrl hdr = MyListView.GetHeader();
hdr.ModifyStyle(HDS_BUTTONS, 0);

9. Display Integer

Add atlmisc.h to your project so that CString is defined and then use the following code to display an integer value in a control:

int nValue = 9999;
CString sInteger;
sInteger.Format("%i", nValue);

10. Display Resource String

Use the AtlLoadString helper function (available int atlmisc.h) to load resource strings greater than 255 characters to a control. The sample project uses the following code to load a string to an edit control. When typing the string into the string table, use \r\n to indicate line breaks, as \n by itself just displays asa vertical bar and does not break the line.

TCHAR cArray[1000];
AtlLoadString(IDS_EDITSTRING, cArray, 1000 + 1);

Bonus Tip

The following tip works with controls.

11. Default Font

When a control is placed on a dialog it automatically assumes the font used by the dialog. However, when a control is used in a window implementation such as a view or splitter pane, it uses SYSTEM_FONT which is not very attractive. Add atlmisc.h to your project so that AtlGetStockFont is available, then use this tip to select DEFAULT_GUI_FONT, a TrueType font object.

MyControl.SetFont(AtlGetStockFont(DEFAULT_GUI_FONT), TRUE);

Terms Of Use

The sample project available with this article is free. Use the code however you wish.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ed Gadziemski
Founder Choycer
United States United States
Ed has over 40 years experience in computer technology and a bachelor's degree in Business Administration. He currently owns a software development firm and an online retail business. During his career, he's led software development departments and created software still in use in the communications and healthcare industries. Ed is a veteran of the United States Army. He lives in Arizona in the United States.

Find Ed on Linkedin.

This material is copyright 2015 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Cees Meijer17-Jan-12 2:38
memberCees Meijer17-Jan-12 2:38 
Generalrooky tips Pin
brunnen7-Mar-08 2:04
memberbrunnen7-Mar-08 2:04 
GeneralNeed advice regarding views in WTL Pin
Tarun Karela26-Aug-04 3:54
memberTarun Karela26-Aug-04 3:54 
QuestionListening to changes from Dynamic Controls? Pin
Rick Pingry8-Jul-04 12:07
memberRick Pingry8-Jul-04 12:07 
GeneralWTL version 7.1 released Pin
Armen Hakobyan18-Mar-04 10:57
memberArmen Hakobyan18-Mar-04 10:57 
QuestionCould you give me and advice? Pin
freehawk28-Jan-04 17:53
memberfreehawk28-Jan-04 17:53 
GeneralWTL_Book!!!!!!!!!!! Pin
M.Khadem16-Nov-03 16:52
memberM.Khadem16-Nov-03 16:52 
Generalresize to fit Pin
Anonymous28-Jun-02 2:05
memberAnonymous28-Jun-02 2:05 
GeneralRe: resize to fit Pin
Ed Gadziemski28-Jun-02 5:19
memberEd Gadziemski28-Jun-02 5:19 
GeneralRe: resize to fit Pin
Anonymous30-Jun-02 23:07
memberAnonymous30-Jun-02 23:07 
GeneralRe: resize to fit Pin
Ed Gadziemski2-Jul-02 3:49
memberEd Gadziemski2-Jul-02 3:49 
GeneralRe: resize to fit Pin
Anonymous3-Jul-02 23:27
memberAnonymous3-Jul-02 23:27 
GeneralExcellent Pin
Robert Edward Caldecott16-Jun-02 21:53
memberRobert Edward Caldecott16-Jun-02 21:53 
GeneralRe: Excellent Pin
Ed Gadziemski17-Jun-02 3:55
memberEd Gadziemski17-Jun-02 3:55 
GeneralRe: Excellent Pin
Luis Alonso Ramos4-Jan-04 15:09
memberLuis Alonso Ramos4-Jan-04 15:09 
GeneralNice Pin
Michael P Butler12-Jun-02 23:53
memberMichael P Butler12-Jun-02 23:53 
GeneralRe: Nice Pin
Thomas Freudenberg13-Jun-02 0:47
memberThomas Freudenberg13-Jun-02 0:47 
GeneralRe: Nice Pin
Michael P Butler13-Jun-02 0:53
memberMichael P Butler13-Jun-02 0:53 
GeneralRe: Nice Pin
Thomas Freudenberg13-Jun-02 1:01
memberThomas Freudenberg13-Jun-02 1:01 
GeneralRe: Nice Pin
Paul A. Howes13-Jun-02 2:00
memberPaul A. Howes13-Jun-02 2:00 
GeneralRe: Nice Pin
Thomas Freudenberg13-Jun-02 2:12
memberThomas Freudenberg13-Jun-02 2:12 
GeneralRe: Nice Pin
Robert Edward Caldecott16-Jun-02 21:52
memberRobert Edward Caldecott16-Jun-02 21:52 
GeneralRe: Nice (Revised) Pin
Ed Gadziemski13-Jun-02 2:11
memberEd Gadziemski13-Jun-02 2:11 
GeneralRe: Nice Pin
Paul A. Howes13-Jun-02 3:15
memberPaul A. Howes13-Jun-02 3:15 
GeneralRe: Nice Pin
Ed Gadziemski13-Jun-02 10:33
memberEd Gadziemski13-Jun-02 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 12 Jun 2002
Article Copyright 2002 by Ed Gadziemski
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid