Click here to Skip to main content
Click here to Skip to main content

Ten WTL Tips and Tricks

By , 11 Jun 2002
 

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

About the Author

Ed Gadziemski
President
United States United States
Member
Ed has over 35 years experience in computer technology and a bachelor's degree in Business Administration. He currently owns an online retail business and previously owned a software consulting firm. Ed is a veteran of the United States Army. He lives in Arizona in the United States.
 
This material is copyright 2011 by Ed Gadziemski. Unauthorized use is strictly prohibited. All rights reserved.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4 PinmemberCees Meijer17 Jan '12 - 2:38 
Generalrooky tips Pinmemberbrunnen7 Mar '08 - 2:04 
GeneralNeed advice regarding views in WTL PinmemberTarun Karela26 Aug '04 - 3:54 
QuestionListening to changes from Dynamic Controls? PinmemberRick Pingry8 Jul '04 - 12:07 
GeneralWTL version 7.1 released PinmemberArmen Hakobyan18 Mar '04 - 10:57 
QuestionCould you give me and advice? Pinmemberfreehawk28 Jan '04 - 17:53 
GeneralWTL_Book!!!!!!!!!!! PinmemberM.Khadem16 Nov '03 - 16:52 
Generalresize to fit PinmemberAnonymous28 Jun '02 - 2:05 
GeneralRe: resize to fit PinmemberEd Gadziemski28 Jun '02 - 5:19 
GeneralRe: resize to fit PinmemberAnonymous30 Jun '02 - 23:07 
GeneralRe: resize to fit PinmemberEd Gadziemski2 Jul '02 - 3:49 
GeneralRe: resize to fit PinmemberAnonymous3 Jul '02 - 23:27 
GeneralExcellent PinmemberRobert Edward Caldecott16 Jun '02 - 21:53 
GeneralRe: Excellent PinmemberEd Gadziemski17 Jun '02 - 3:55 
GeneralRe: Excellent PinmemberLuis Alonso Ramos4 Jan '04 - 15:09 
GeneralNice PinmemberMichael P Butler12 Jun '02 - 23:53 
GeneralRe: Nice PinmemberThomas Freudenberg13 Jun '02 - 0:47 
GeneralRe: Nice PinmemberMichael P Butler13 Jun '02 - 0:53 
GeneralRe: Nice PinmemberThomas Freudenberg13 Jun '02 - 1:01 
GeneralRe: Nice PinmemberPaul A. Howes13 Jun '02 - 2:00 
GeneralRe: Nice PinmemberThomas Freudenberg13 Jun '02 - 2:12 
GeneralRe: Nice PinmemberRobert Edward Caldecott16 Jun '02 - 21:52 
GeneralRe: Nice (Revised) PinmemberEd Gadziemski13 Jun '02 - 2:11 
GeneralRe: Nice PinmemberPaul A. Howes13 Jun '02 - 3:15 
GeneralRe: Nice PinmemberEd Gadziemski13 Jun '02 - 10:33 
GeneralRe: Nice PinmemberEd Gadziemski13 Jun '02 - 2:04 
GeneralRe: Nice PinmemberThomas Freudenberg13 Jun '02 - 2:15 
GeneralRe: Nice PinmemberEd Gadziemski13 Jun '02 - 2:25 
GeneralRe: Nice PinmemberRashid Thadha13 Jun '02 - 2:20 
GeneralRe: Nice PinmemberEd Gadziemski13 Jun '02 - 2:28 
GeneralRe: Nice PinmemberEd Gadziemski13 Jun '02 - 2:23 
GeneralRe: Nice PinmemberEd Gadziemski13 Jun '02 - 14:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.6.130523.1 | Last Updated 12 Jun 2002
Article Copyright 2002 by Ed Gadziemski
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid