Ten tips for WTL programming

xiaoxiao2021-03-06  68

Introduction

This article will introduce ten skills to develop WTL applications. These tricks involve how to control and place the application main window to how to display strings and integers in the control. You can download sample programs that use this ten tricks to http://www.codeproject.com/wtl/tips/tipsproject.zip. Ten tricks include:

· Set the size of the main window

· When startup, the main window is displayed in the center of the screen.

· Set the smallest / maximum size of the main window

· Dynamic load main window title

· Set the toolbar to a flat-stand

· Set the color of the dialog text and background

· Exchange dialog button

· Set a flat-style ListView header

· Display integers in the control

· Display resource strings in the control

Main window skills

The following techniques can be used in the SDI and MDI applications simultaneously:

1. Set the size when the window is generated

Using the following technique in the RUN () function of the program .CPP file can control the size size when the window is generated. Set the value of the RECT with the window size you want, then pass this value as the second function to the createEx () function, as shown below:

Rect rc = {0, 0, 380, 265};

IF (WNDMAIN.CREATEX (NULL, RC) == NULL)

2. Display the main window in the center of the desktop

To make the main window display in the center of the desktop, add a line of code below before the application's .cpp file Run () function is added before the showWindow () command:

Wndmain.centerwindow ();

3. Set the minimum / maximum size

If you want to control the maximum minimum size of your main window, you have to add the following message processing procedure in the CMAINFRAME.H of the headfinder.

Message_handler (WM_GETMINMAXINFO, ONGETMINMAXINFO)

Fully implement the function, you also need to add processing functions in the file:

Lresult ONGETMAXINFO (UINT, WPARAM, LPARAM LPARAM, BOOL &)

{// lParam transmits a pointer to the MinMaxInfo structure

LPMINMAXINFO LPMMI = (LPMINMAXINFO) LPARAM;

/ / Change the corresponding value in the Size structure for the window size value we want

LPMMI-> PTMINTRACKSIZE.X = 200; // Minimum width

LPMMI-> PTMINTRACKSIZE.Y = 150; // Minimum height

LPMMI-> PTMaxTracksize.x = 600; // Maximum Width

LPMMI-> ptmaxtracksize.y = 450; // maximum height

Return 0;}

4. Dynamic setting title

It is possible to generate a CString object by loading a string in the resource, and then dynamically set up a dynamic setting window title. The following code can be completed in the onCreate () function. In addition, you need to "atlmisc.h" in the project, this file defines the CSTRING class. You can load up to 255 characters with loadstring ().

CString Str;

Str.LoadString (IDS_EDITSTRING);

SetwindowText (STR);

5. Flat-style toolbar flat style toolbar

When using the WTL AppWizard to generate a program, if Rebar is selected, the generated toolbar is a standard stereo button. If you want to have a flat-style in the toolbar without Rebar, you can add the following code as long as you add the following code after the code of the creation toolbar of the main frame oncreate function:

Ctoolbarctrl Tool = M_HWNDTOOLBAR;

Tool.ModifyStyle (0, TBStyle_flat);

Dialog Tips dialog box skills

The following techniques can be used in a dialog or a dialog-based application. The figure below shows the About dialog of our sample function, which uses two techniques 6. Dialog text and background color dialog text and background color

This tip provides a way to change the text or background color of the dialog box. In this article sample program, we use setTextColor to set the text color for white. Beijing color is set to black using "stock brush". The first step is to add the following two lines of code in the dialog message mapping table:

Message_handler (WM_CTLCOLORDLG, ONCTRLCOLOR)

Message_handler (WM_CTLCOLORSTATIC, ONCTRLCOLOR)

The second step is to change the color of the text and the background in the onctrlcolor function. Set the background mode to transparent, which correctly displays static controls and Group Box control text. Next, set the text to our desired color, finally set the background brush.

Add atlmisc.h header files in the project because the ATLGETSTOCKBRUSH () function is defined in this header file. There are several stock brusks to choose White_brush, LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, and Black_Brush, if you use other colors, you need to generate new 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. Dynamic exchange button

The following code is from the OnInitDialog () function of the About dialog, used to exchange the OK and the CANCEL button. The key point is how to convert the screen position to the location relative to the client.

CButton Bok = Getdlgitem (IDOK));

CButton Bcancel = getdlgitem (iDCancel));

// get the button position

Rect RCOK, RCCANCEL;

Bok.getWindowRect (& RCOK);

ScreenToClient (& RCOK);

Bcancel.getWindowRect (& RCCancel);

ScreenToClient (& RCCANCEL);

// Exchange button position

Bok.SetWindowPos (NULL, & RCCANCEL, SWP_NOZORDER | SWP_NOSIZE);

Bcancel.SetWindowPos (NULL, & RCOK, SWP_NOZORDER | SWP_NOSIZE);

Control skills

Skills 8 Suitable for ListView controls for report types, tips 9 and 10 apply to any controls that accept text, such as Edit controls and RicheDit controls.

8. Flat-style ListView header

Change the header of the report type ListView into a flat appearance, as long as the head control object is obtained, and the type of it is modified

CheaderCtrl hdr = mylistview.getHeader ();

HDr.ModifyStyle (HDS_Buttons, 0);

9. Show an integer

Add an atlmisc.h file in the project, this file defines the CString class. Then use the following code to display an integer value in the control.

Int nvalue = 9999;

Cstring sinteger;

SINTEGER.FORMAT ("% I", nvalue); MyControl.SetWindowText (sinteger);

10. Display resource strings

Using the auxiliary function atlloadstring load length in the atlmisc.h header file can more than 255 characters, and then display this string into the control. The following code is used to display the string in the EDIT control in the sample program. When you enter a string in a resource string table, use / r / n to branch, only / n cannot be correctly branched.

Tchar Carray [1000];

AtlloadString (IDS_EDITSTRING, Carray, 1000 1);

MyControl.SetWindowText (Carray);

Additional skill

The following skills can be used in all controls

11. Default font

When a control is placed on the dialog, the control uses the default font of the dialog. However, when a control is used in a window or the split panel, the SYSTEM_FONT font will be used, which is not very beautiful. To change the font, just add the atlmisc.h file in the project, then call AtlgetStockFont to get the TrueType font DEFAUI_FAUI_Font, set the control to this font:

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

Terms of use

The sample program in this article is free, you can use anywhere.

This Software is Distributed As-IS, WITHOUT WARRANTIES OF ANY KIND.

转载请注明原文地址:https://www.9cbs.com/read-85719.html

New Post(0)