Load XP styles for Windows Forms in. Net

Source: Internet
Author: User
Tags command line implement modify resource web services win32 root directory visual studio
window| load XP styles on Windows Forms loaded in. Net



Author: Heath Stewart

Introduced

When Windows XP was released with his own visual style or theme, many people were excited by the gorgeous interface he had. However, when the. NET1.0 official version was released, many people included myself disappointed that Windows Forms did not support the visual style of Windows XP. I've studied visual-style API functions and Microsoft's. NET Framework development documentation, and then realized that it's not hard to add XP styles to. NET applications.

The following will be a simple program to add XP styles to your programs and controls. Content applies to controls that have properties that implement XP styles that enable them to create XP styles, while notifying Windows systems to add themes to your applications and controls.



To achieve the required conditions

You need some conditions to implement this feature:

1. Microsoft. NET Framework SDK (required)

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

3. Windows XP operating System (optional), support for the topic of the official version of the operating system



Get to the point.

Open visual Studio. NET to build 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 have finished building the project, open the AssemblyInfo.cs file, fill in the value in Assemblytitle,assemblydescription and AssemblyVersion, and modify the AssemblyDelaySign value to true. You also have to modify the value of AssemblyKeyFile to @ ". \.. \keyfile.snk ", keyfile.snk file we then come back to create or copy one.

When you have finished with the above, your code should look like the following:

Using System.Reflection;

Using System.Runtime.CompilerServices;



[Assembly:assemblytitle ("Theme Test")]

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

[Assembly:assemblyconfiguration ("")]

[Assembly:assemblycompany ("")]

[Assembly:assemblyproduct ("")]

[Assembly:assemblycopyright ("")]

[Assembly:assemblytrademark ("")]

[Assembly:assemblyculture ("")]

[Assembly:assemblyversion ("1.0.0.0")]

[Assembly:assemblydelaysign (True)]

[Assembly:assemblykeyfile (@). \.. \keyfile.snk ")]

[Assembly:assemblykeyname ("")]



Now, open the Form1.cs form design interface and add some Windows Forms controls to the top, such as Listviews,buttons,groupboxes,progressbars or more, and here's a list of all the holes that 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 other specific controls are inherited from Buttonbase,groupbox, or labels, and you need to set their FlatStyle properties to system, and I'll make some brief notes.

When you compile this application, you will find that this Windows Forms does not look like an XP style, and to find out why, please look down.



How Windows XP styles are displayed

Simple description: Comctl32.dll, version 6. Comctl32.dll, or those normal controls are used for a long time. This dynamic connection library is used by the underlying control while USER32.DLL is provided to the user control. In version 6 of the Comctl32.dll, all the controls are placed inside, so they all support the theme. But the new version of Comctl32.dll is not like the previous version, and to use the Windows XP style, you must use an operating system that contains Comctl32.dll, such as Windows XP.

For those that are supported, their styles are related to special topic resources that are used to draw in the client interface. Controls that are derived from Buttonbase,groupbox and Lablel must have the FlatStyle property set to FlatStyle.System enumeration values so the system can draw them.

In order for the operating system to add theme styles to our controls, you need to notify the system to make version 6 Comctl32.dll the default. The system defaults to version 5 Comctl32.dll, as is the case for. NET programs, but we will let the program use version 6 Comctl32.dll when it is executed.

Add an XML file to your project, and the file named "Your application name].exe.manifest,[your application name" refers to the main EXE file generated by the project compilation. Sets the compilation type to "None".

If the code in your AssemblyInfo.cs is the one mentioned above, then the new [your application name].exe.manifest file should look like this:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>

<assembly

Xmlns= "Urn:schemas-microsoft-com:asm.v1"

Manifestversion= "1.0" >

<assemblyidentity

version= "1.0.0.0"

Processorarchitecture= "X86"

Name= "Theme Test"

Type= "Win32"/>

<description>testing Windows XP Visual styles.</description>

<dependency>

<dependentAssembly>

<assemblyidentity

Type= "Win32"

Name= "Microsoft.windows.common-controls"

version= "6.0.0.0"

Processorarchitecture= "X86"

Publickeytoken= "6595B64144CCF1DF"

Language= "*"/>

</dependentAssembly>

</dependency>

</assembly>



You should replace the values of the "version" and "name" attributes above, so that he corresponds to the assemblyversion and Assemblytitle attributes, as in your project. The "descryiption" element also conforms to your assemblydescription attribute.

Next, open the Command Prompt window to modify your project output path, should be "bin\debug" as the project path root directory. If you have added the path of the Sn.exe Strong Name tool to your system environment variable, enter the following on the command line:

Sn.exe–k.. \.. \keyfile.snk

The above steps generate a keyfile.snk public key file, which is necessary to make your assembly a strongly typed assembly. This is necessary in many cases.



Last step Add mnifest information

You are now going to be in the directory where the project file is exported. In my case it's a simple WindowsApplication1.exe file, and then I'll talk about him.

Now, back to Visul Studio.NET, click on the File menu-> Open, browse to your application such as WindowsApplication1.exe, click Open he we will see a resource file Management tree.

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

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

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

4. Save all the files let's go back to the previous resources to browse the tree. We can now see a rt_manifest node.

5. Click to select the new resource file below him, usually with a name of 101. Change his ID value to 1 in the Properties window and save your application once again.

6. Close the Resource Explorer window you opened back to the command prompt, enter the following command:

Sn–r WindowsApplication1.exe. \.. \keyfile.snk

After execution, some version information is output, and the final display is:

Assembly ' WindowsApplication1.exe ' successfully re-signed

To run your. NET application, you will see the Windows XP style.



Profile

You don't have to do a lot of work to add Windows XP visualization styles to your applications and controls. Using these new styles will give your form a new vision, which is often used in business applications. Many companies are spending their energy on developing this stuff, and now you can do it without doing a lot of work.

Remember to set the control with the FlatStyle attribute to FlatStyle.System, and add the manifest resource after compiling your program like the sample file above. When all this is done, the rest is to complete the signature of the assembly. If you want to test your application or you have permission to sign it, you can turn off validation of the assembly by using the following command:

SN.EXE–VR WindowsApplication1.exe

Act now and Add XP style to your application to make your product more characteristic. It won't take much time but the effect is obvious.



About Heath Stewart

Heath Stewart is a happily married software engineer originally-Nebraska and a graduate of Iowa state University in a Mes, Iowa. With nothing more than do in either state, Heath started programming early in the Gopher ruled and the Internet wasn ' t commercialized, and enjoys continuous 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 SES, and data analysis tools. He also enjoys photography.

Currently, Heath is the Director of Technology in 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 editor for CodeProject and are happy to help the development community as a Microsoft MVP (C #).


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.