Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Ten WTL Tips and Tricks

0.00/5 (No votes)
11 Jun 2002 1  
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