Load XP style to the Windows Form Program under .NET [Translation]

zhaozj2021-02-16  39

Load XP style to Windows forms in .NET

Author: Heath Stewart Translation: Zheng Zuo

Introduction

When Windows XP is released with his unique visual style or the subject, many people are excited for his gorgeous interface. However, when .NET1.0 official version is released, many people include I am disappointed with the Visual style of Windows XP for Windows Forms. I have studied the API function and Microsoft's .NET framework development documentation in the visual style, and it is not very difficult to recognize that .NET application is not very difficult.

The following will be added to your programs and controls to your programs and controls through a simple program. The content is suitable for some controls that have properties that enable XP styles. Through these properties, they generate XP styles while notifying the Windows system to add topics for your applications and controls.

Realize the required conditions

You need some conditions to implement this feature:

1. Microsoft .NET Framework SDK (must)

2. Microsoft Visual Studio .NET, he helps you design Windows Forms (optional)

3. Windows XP operating system (optional), official version of the operating system

To the point

Open Visual Studio .NET to create a new Windows application. Here I will use C # as a programming language, so if you use Visual Basic.net, you need to convert the code. When you build a project, open the assemblyInfo.cs file, fill in the value in AssemblyTitle, AssemblyDescription, and AssemblyVersion, modify the value of AssemblyDelaySIGN to True. At the same time, you also want to modify the value of AssemblyKeyFile is @ "../../ keyfile.snk", KeyFile.snk files we will come back to create or copy one.

When you follow these finished, your code should look like the following:

Using system.reflection;

Using system.Runtime.compilerServices;

[assmbly: assemblytitle ("theme test")]]]

[Assembly: AssemblyDescription ("Testing Windows XP Visual Styles.")]

[Assembly: AssemblyConfiguration ("")]

[assmbly: askEMBLYCOMPANY ("")]

[assembly: askPRODUCT ("")]

[Assembly: assemblycopyright ("")]

[assmbly: assemblytrademark ("")]

[assmbly: assemblyculture ("")]

[assmbly: assemblyversion "

1.0.0.0 ")]

[assmbly: assemblydelaysign (true)]

[assmbly: assemblykeyKeyfile (@ "../../ keyfile.snk"]

[assmbly: assemblykeykeyname ("")]

Now open the Form1.cs Form Design interface plus some Windows Forms Controls to top, such as ListViews, Buttons, Groupboxes, Progressbars, or more, listed in all support XP styles: TextBox, RichtextBox, HScrollbar, vscrollbar , ProgressBar, Tabcontrol, Mainmenu, Contextmenu, Combobox, DataGrid, Listbox, Listview, Treeview, DateTimePicker, Monthcalendar, Splitter, TRACKBAR, STATUSBAR, TOOLBAR, TREEVIEW, LISTVIEW.

For these controls, some are supported by default, and some specific controls are inherited from ButtonBase, GroupBox, or Label, you need to set their FlatStyle properties for System, those I will do some short instructions.

When you compile this application, you will find that this Windows Forms looks not like the XP style, you want to find the reason, please see it.

Display principle of Windows XP style

Simple description: ComctL32.dll, version 6. ComctL32.dll, or those ordinary controls have been used for a long time. This dynamic connection library is used by the infrastructure while user32.dll is provided to the user control. In the comctl32.dll of version 6, all controls are placed inside, so they all support the subject. But the new version of ComctL32.dll is not like a version, you must use the Windows XP style, you must use an operating system containing ComctL32.dll, such as Windows XP.

For those supported controls, their style is related to some special subject resources, which is used to draw in the customer interface. For those who are derived from ButtonBase, GroupBox, and Lables must set the FLATSTYLE attribute to FlatStyle.System enumeration value, so the system can draw them.

In order to give our controls to our controls, you need to notify the system to let the comctl32.dll of version 6 as the default. The default use of the system is the comctl32.dll of version 5, which is the same for the .NET program, but we will let the program use version 6 COMCTL32.DLL when executed.

Add an XML file to your project, the file is [your application name] .exe.manifest, [your application name] refers to the primary EXE file generated by the project compilation. Set the compilation type "none".

If the code in your assemblyinfo.cs is mentioned above, then the new [your application name] .exe.manifest file should be the following:

XMLns = "URN: Schemas-Microsoft-COM: ASM.V1"

ManifestVersion = "1.0">

Version = "1.0.0.0"

Processrarchitecture = "x86"

Name = "theme test"

TYPE = "Win32" />

Testing Windows XP Visual Styles.

TYPE = "Win32"

Name = "Microsoft.Windows.common-Controls"

Version = "6.0.0.0"

Processrarchitecture = "x86"

PublickeyToken = "6595b64144ccf1df"

Language = "*" />

You have to replace the value of the "Version" and "Name" attributes above so that he will correspond to the AssemblyVersion and AssemblyTitle properties as in your project. That "Descryiption" element is also in line with your AssemblyDescription property.

Next, open the command prompt window to modify your project output path, should use "bin / debug" as the project path root directory. Suppose you have added the path to the Sn.exe Strong Name tool to your system environment variable, then enter the following:

sn.exe -k ../../keyfile.snk

The above steps generate a keyfile.snk public key file, in order to make your assembly a strong type assembly this public key is necessary. This must be required in many cases.

The last step adds Mnifest information

You are now in the directory where the project file is output. In my example is a simple WindowsApplication1.exe file, will talk about him.

Now, return to Visul Studio.net, click File Menu -> Open, browse to your app, such as WindowsApplication1.exe, click to open He we will see a resource file management tree.

1. Right-click on the root node Click to select "Add Resources".

2. Click the "Import" button to find WindowsApplication.exe.mnifest in your project path and open him.

3. In the Custom Resource Type dialog, we enter RT_MANIFEST and click OK.

4. Save all files Let us return to the previous resource to browse the tree. We can now see a node of RT_Manifest.

5. Click to select the newly added resource file below, the usual name is 101. Modify his ID value to 1 in the properties window, save your application again.

6. Close the resource browsing window you open Back to the command line prompt, enter the following command:

Sn -r windowsapplication1.exe ../../keyfile.snk

Some version information will be output after execution, and finally display:

AskEMBLY 'SUCCESSFULLY RE-SIGNED

You will see the Windows XP style when you run your .NET application.

summary

You don't need to do a lot of work, you can add Windows XP visual patterns to your applications and controls. Using these new styles will bring your form new visual, which is often used in business programs. Many companies have flowers to develop things in this area, and now you can do it without having to do a lot. Remember to set the controls with the flatstyle property to Flatstyle.System, and add the manifest resource after compiling your programs, add the template file above. When these are done, the rest is to complete the signature of the assembly. If you want to test your app or if you have permission to sign him, you can close the verification of the assembly by the following command:

sn.exe -vr windowsapplication1.exe

Action Now, add the XP style for your app to make your product more features. This will not spend a lot of time but the effect is obvious.

About Heath Stewart

Heath Stewart Is A Happily Married Software Engineer Originally from ORIGINALLY

Nebraska And A Graduate of iowa State University in

Ames,

Iowa. With nothing much to do in either state, Heath started programming early in life when gopher ruled and the Internet was not commercialized, and enjoys continuous research and development in new languages ​​and frameworks. Fluent in many different programming languages, he has developed many large software solutions for companies in different areas, such as Internet filtering, intrusion detection systems, production management systems, Web sites for various purposes, and data analysis tools. He also enjoys photography.

Currently, Heath is the Director of Technology at Proplanner, a Web-based Production Planning System that primarily uses .NET where he designed a multi-tiered environment and supports multiple front-ends (such as a legacy Java interface on PDM) and back- Ends (Such As Web Services, Rdbms's, File-Based IO, AND PDM).

Heath Is Now An Editor for CodeProject and is happy to help the development Community As a Microsoft MVP (C #).

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

New Post(0)