![]() |
Platforms, Frameworks & Libraries »
WTL »
Beginners
Beginner
Ten WTL Tips and TricksBy Ed GadziemskiTen quick and easy tips and tricks for use when developing WTL applications |
VC6, VC7Win2K, WinXP, WTL, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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:
|
|
The following tips will work with either SDI or MDI applications.
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)
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();
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; }
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);
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);
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.

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); }
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);
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.
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);
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);
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);
The following tip works with controls.
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);
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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 11 Jun 2002 Editor: Chris Maunder |
Copyright 2002 by Ed Gadziemski Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |