Click here to Skip to main content
15,878,959 members
Articles / Desktop Programming / WTL
Article

Ten WTL Tips and Tricks

Rate me:
Please Sign up or sign in to vote.
4.10/5 (34 votes)
11 Jun 20024 min read 204.4K   2.5K   52   32
Ten quick and easy tips and tricks for use when developing WTL applications

WTL Splitters and Panes Image

Introduction

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.

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.

wndMain.CenterWindow();

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.

MESSAGE_HANDLER(WM_GETMINMAXINFO, OnGetMinMaxInfo)

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

LRESULT OnGetMinMaxInfo(UINT, WPARAM, LPARAM lParam, BOOL&)
{ // load size structure with lParam values
  LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;

  // 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;
str.LoadString(IDS_EDITSTRING);
SetWindowText(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:

MESSAGE_HANDLER(WM_CTLCOLORDLG, OnCtrlColor)
MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtrlColor)

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.

LRESULT OnCtrlColor(UINT, WPARAM, LPARAM, BOOL&)
{ // 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;
bOk.GetWindowRect(&rcOk);
ScreenToClient(&rcOk);
bCancel.GetWindowRect(&rcCancel);
ScreenToClient(&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);
MyControl.SetWindowText(sInteger);

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);
MyControl.SetWindowText(cArray);

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 SOFTWARE IS DISTRIBUTED AS-IS, WITHOUT WARRANTIES OF ANY KIND.

License

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


Written By
Founder Choycer
United States United States
Ed has over 40 years experience in computer technology and a bachelor's degree in Business Administration. He's currently a marketing technology consultant. 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 2019 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Cees Meijer17-Jan-12 2:38
Cees Meijer17-Jan-12 2:38 
Generalrooky tips Pin
brunnen7-Mar-08 2:04
brunnen7-Mar-08 2:04 
GeneralNeed advice regarding views in WTL Pin
Tarun Karela26-Aug-04 3:54
Tarun Karela26-Aug-04 3:54 
QuestionListening to changes from Dynamic Controls? Pin
Rick Pingry8-Jul-04 12:07
Rick Pingry8-Jul-04 12:07 
GeneralWTL version 7.1 released Pin
Armen Hakobyan18-Mar-04 10:57
professionalArmen Hakobyan18-Mar-04 10:57 
QuestionCould you give me and advice? Pin
freehawk28-Jan-04 17:53
freehawk28-Jan-04 17:53 
GeneralWTL_Book!!!!!!!!!!! Pin
M.Khadem16-Nov-03 16:52
M.Khadem16-Nov-03 16:52 
Generalresize to fit Pin
28-Jun-02 2:05
suss28-Jun-02 2:05 
GeneralRe: resize to fit Pin
Ed Gadziemski28-Jun-02 5:19
professionalEd Gadziemski28-Jun-02 5:19 
GeneralRe: resize to fit Pin
30-Jun-02 23:07
suss30-Jun-02 23:07 
GeneralRe: resize to fit Pin
Ed Gadziemski2-Jul-02 3:49
professionalEd Gadziemski2-Jul-02 3:49 
GeneralRe: resize to fit Pin
3-Jul-02 23:27
suss3-Jul-02 23:27 
GeneralExcellent Pin
Rob Caldecott16-Jun-02 21:53
Rob Caldecott16-Jun-02 21:53 
GeneralRe: Excellent Pin
Ed Gadziemski17-Jun-02 3:55
professionalEd Gadziemski17-Jun-02 3:55 
GeneralRe: Excellent Pin
Luis Alonso Ramos4-Jan-04 15:09
Luis Alonso Ramos4-Jan-04 15:09 
GeneralNice Pin
Michael P Butler12-Jun-02 23:53
Michael P Butler12-Jun-02 23:53 
GeneralRe: Nice Pin
Thomas Freudenberg13-Jun-02 0:47
Thomas Freudenberg13-Jun-02 0:47 
GeneralRe: Nice Pin
Michael P Butler13-Jun-02 0:53
Michael P Butler13-Jun-02 0:53 
AtlLoadString was one of those things it took ages to find out about. My problem was I'd gotten so used to MFC doing a lot of work for me (with wizards and the like), I'd forgotten how to do the simple stuff. Big Grin | :-D



Michael Smile | :)


"Eureka" is Greek for "This bath is too hot"
GeneralRe: Nice Pin
Thomas Freudenberg13-Jun-02 1:01
Thomas Freudenberg13-Jun-02 1:01 
GeneralRe: Nice Pin
Paul A. Howes13-Jun-02 2:00
Paul A. Howes13-Jun-02 2:00 
GeneralRe: Nice Pin
Thomas Freudenberg13-Jun-02 2:12
Thomas Freudenberg13-Jun-02 2:12 
GeneralRe: Nice Pin
Rob Caldecott16-Jun-02 21:52
Rob Caldecott16-Jun-02 21:52 
GeneralRe: Nice (Revised) Pin
Ed Gadziemski13-Jun-02 2:11
professionalEd Gadziemski13-Jun-02 2:11 
GeneralRe: Nice Pin
Paul A. Howes13-Jun-02 3:15
Paul A. Howes13-Jun-02 3:15 
GeneralRe: Nice Pin
Ed Gadziemski13-Jun-02 10:33
professionalEd 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.