Make the non-standard Win32 control or self-painted control also have the interface style of Windows XP

Source: Internet
Author: User
Tags drawtext

Here we will talk about two concepts: Theme and visual style. Theme first appeared in Microsoft plus! For Windows 95, it is a set of settings for windows such as wallpaper, cursors, fonts, sounds, and icons. Visual style is introduced in Windows XP. visual style specifies the appearance of contorls. It also includes a set of APIS for using these appearances. To use visual style, you must use comctl32.dll 6, but comctl32.dll 6 cannot be distributed to earlier versions of Windows. Therefore, you can only use visual style in Windows XP.

Delphi 7 (hereinafter referred to as 7) provides better support for visual style. First, encapsulate the menifest into VCL-txpmenifest, and add uxtheme. the PAS unit contains a set of APIS for visual style, their structures, constants, and reference declarations (about 47 API functions). More importantly, 7 also adds themes. the PAS Unit further simplifies and encapsulates this set of Apis. The Win32 control under 7 has greatly improved the support for visual style compared with Delphi 6, which is the credit of this unit.

GenerallyProgramIf you have a visual style in Windows XP, you only need to add VCL-txpmenifest to the main form. However, some non-standard or self-painted controls are still classic interfaces. Here we will talk about how to use the methods provided in themes. Pas to make these controls have visual styles.

Themes. PAS has only one class: tthemeservices. This class has an important attribute: themesenabled (boolean type), which is to determine whether the current program can use visual style, this property is true only when the program uses txpmenifest and runs in Windows XP and uses Windows XP themes (that is, desktop themes are not Windows classic, because the program runs in a previous version of Windows, your program must also provide the processing process when this attribute is false (generally the original processing process ). We also need to mention several important functions encapsulated in this class: drawedge is used to draw control boundaries, drawelement is used to draw the entire interface, and drawtext is used to write. This class also processes the wm_themechanged message, so that when we change the desktop topic, the program will automatically adjust the appearance. Let's talk about the most common function (multi-state function): getelementdetails. the return value of this function is used in the preceding drawxxx functions, the input value of this function is one of the 24 enumeration types. The 24 enumeration types are in themes. defined at the beginning of the PAS unit (starting from the second tthemedbutton until the tthemedwindow ). Finally, we do not directly use this class. There is a function in the themes. Pas unit:

Function themeservices: tthemeservices;
The returned value is this class, so we use this method directly. This is done in VCL 7.

Okay. Here is a simple example. The tpanel control of Delphi is not a standard control, so we can implement visual style on it. Create a new project in Delphi 7, put txpmenifest in the main form, and reference the themes unit in unit1.

Tform1 = Class (tform)
Add belowCode(The tcustompanel paint method is reloaded ):

Tvspanel = Class (tcustompanel)
Private
//
Protected
Procedure paint; override;
Public
//
End;
The implementation of the overload paint method is as follows:

procedure tvspanel. paint;
var
details: tthemedelementdetails;
begin
inherited;
If themeservices. themesenabled then
begin
details: = themeservices. getelementdetails (tbpushbuttonhot); {draw a button in the hot state.}
define merasebackground (self, canvas. handle); {background when the paint button is erased}
themeservices. drawelement (canvas. handle, details, clientrect);
themeservices. drawtext (canvas. handle, details, caption, clientrect,
dt_expandtabs or dt_vcenter or dt_center or dt_singleline, 0);
end;
tcustompanel has been modified, the tvspanel is instantiated in the create event of the main form. The Code is as follows:

Procedure tform1.formcreate (Sender: tobject );
VaR
Apanel: tvspanel;
Begin
Apanel: = tvspanel. Create (application );
Apanel. Left: = 100;
Apanel. Top := 100;
Apanel. Width := 200;
Apanel. Height: = 30;
Apanel. Caption: = 'panel with button style ';
Apanel. Parent: = self;
End;
Okay. Run the command to see if the visual style effect has come out. In my program, thintwindow is used as a floating window and visual style is added. The effect is good. If you are more careful, make the same interface as the floating entry of the Microsoft Pinyin Input Method in Windows XP.

In addition, there are several important things not mentioned, such as Part and state. You can check msdn in user interface design and development \ Windows Shell \ shell refrence \ visual styles refrence.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.