在WinXP环境下显示XP风格的控件

通过标准的.NET Windows Application我们只能建立一个使用Comctl32.dll Version 5的应用程序。即便我们的程序运行在Windows XP系统下,外观仍旧沿用了Windows 2000。实际上通过简单的设置,就可以让我们的程序在Windows XP下面显示为新的程序外观。

首先我们建立自己的Windows Application,然后拖拽一个按钮(Button),一个进度条(ProgressBar),一个组合框(ComboBox),一个单选框(RadioButton)和一个复选框(CheckBox)。这些控件是在Windows XP下面有其特殊的显示效果。我们把其中的FlatStyle属性设置为System。这个操作表示我们这些控件的平面外观样式使用操作系统的默认值。

之后我们在我们应用程序的文件目录建立一个manifest文件。假如我们的应用程序AssemblyNamemyapp,那么这个文件名就是myapp.exe.manifest。编辑这个文件的内容。

<?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="Microsoft.Winweb.[AssemblyName]"

   type="win32"

/>

<description>.NET control deployment tool</description>

<dependency>

   <dependentAssembly>

     <assemblyIdentity

       type="win32"

       name="Microsoft.Windows.Common-Controls"

       version="6.0.0.0"

       processorArchitecture="X86"

       publicKeyToken="6595b64144ccf1df"

       language="*"

     />

   </dependentAssembly>

</dependency>

</assembly>

其中name="Microsoft.Winweb.[AssemblyName]"的部分写入自己程序的AssemblyName。比如这个程序我们就写成name="Microsoft.Winweb.myapp"。保持执行文件和这个manifest文件在同一目录下。这样,如果我们的程序在Windows XP系统下面运行,那么就会自动显示Windows XP的效果。

 

程序代码如下:

Public Class Form1

    Inherits System.Windows.Forms.Form

 

#Region " Windows Form Designer generated code "

 

    Public Sub New()

        MyBase.New()

 

        'This call is required by the Windows Form Designer.

        InitializeComponent()

 

        'Add any initialization after the InitializeComponent() call

 

    End Sub

 

    'Form overrides dispose to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        If disposing Then

            If Not (components Is Nothing) Then

                components.Dispose()

            End If

        End If

        MyBase.Dispose(disposing)

    End Sub

 

    'Required by the Windows Form Designer

    Private components As System.ComponentModel.IContainer

 

    'NOTE: The following procedure is required by the Windows Form Designer

    'It can be modified using the Windows Form Designer. 

    'Do not modify it using the code editor.

    Friend WithEvents Button1 As System.Windows.Forms.Button

    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox

    Friend WithEvents RadioButton1 As System.Windows.Forms.RadioButton

    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

    Friend WithEvents HScrollBar1 As System.Windows.Forms.HScrollBar

    Friend WithEvents VScrollBar1 As System.Windows.Forms.VScrollBar

    Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar

    Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton

    Friend WithEvents ToolBarButton2 As System.Windows.Forms.ToolBarButton

    Friend WithEvents ToolBarButton3 As System.Windows.Forms.ToolBarButton

    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar

    Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Me.Button1 = New System.Windows.Forms.Button

        Me.CheckBox1 = New System.Windows.Forms.CheckBox

        Me.RadioButton1 = New System.Windows.Forms.RadioButton

        Me.ComboBox1 = New System.Windows.Forms.ComboBox

        Me.HScrollBar1 = New System.Windows.Forms.HScrollBar

        Me.VScrollBar1 = New System.Windows.Forms.VScrollBar

        Me.ToolBar1 = New System.Windows.Forms.ToolBar

        Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton

        Me.ToolBarButton2 = New System.Windows.Forms.ToolBarButton

        Me.ToolBarButton3 = New System.Windows.Forms.ToolBarButton

        Me.StatusBar1 = New System.Windows.Forms.StatusBar

        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar

        Me.SuspendLayout()

        '

        'Button1

        '

        Me.Button1.FlatStyle = System.Windows.Forms.FlatStyle.System

        Me.Button1.Location = New System.Drawing.Point(32, 144)

        Me.Button1.Name = "Button1"

        Me.Button1.TabIndex = 0

        Me.Button1.Text = "Button1"

        '

        'CheckBox1

        '

        Me.CheckBox1.FlatStyle = System.Windows.Forms.FlatStyle.System

        Me.CheckBox1.Location = New System.Drawing.Point(40, 200)

        Me.CheckBox1.Name = "CheckBox1"

        Me.CheckBox1.TabIndex = 1

        Me.CheckBox1.Text = "CheckBox1"

        '

        'RadioButton1

        '

        Me.RadioButton1.FlatStyle = System.Windows.Forms.FlatStyle.System

        Me.RadioButton1.Location = New System.Drawing.Point(152, 200)

        Me.RadioButton1.Name = "RadioButton1"

        Me.RadioButton1.TabIndex = 2

        Me.RadioButton1.Text = "RadioButton1"

        '

        'ComboBox1

        '

        Me.ComboBox1.Location = New System.Drawing.Point(144, 152)

        Me.ComboBox1.Name = "ComboBox1"

        Me.ComboBox1.Size = New System.Drawing.Size(121, 20)

        Me.ComboBox1.TabIndex = 3

        Me.ComboBox1.Text = "ComboBox1"

        '

        'HScrollBar1

        '

        Me.HScrollBar1.Location = New System.Drawing.Point(160, 120)

        Me.HScrollBar1.Name = "HScrollBar1"

        Me.HScrollBar1.TabIndex = 4

        '

        'VScrollBar1

        '

        Me.VScrollBar1.Location = New System.Drawing.Point(256, 48)

        Me.VScrollBar1.Name = "VScrollBar1"

        Me.VScrollBar1.TabIndex = 5

        '

        'ToolBar1

        '

        Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.ToolBarButton1, Me.ToolBarButton2, Me.ToolBarButton3})

        Me.ToolBar1.DropDownArrows = True

        Me.ToolBar1.Location = New System.Drawing.Point(0, 0)

        Me.ToolBar1.Name = "ToolBar1"

        Me.ToolBar1.ShowToolTips = True

        Me.ToolBar1.Size = New System.Drawing.Size(292, 28)

        Me.ToolBar1.TabIndex = 6

        '

        'StatusBar1

        '

        Me.StatusBar1.Location = New System.Drawing.Point(0, 251)

        Me.StatusBar1.Name = "StatusBar1"

        Me.StatusBar1.Size = New System.Drawing.Size(292, 22)

        Me.StatusBar1.TabIndex = 7

        Me.StatusBar1.Text = "StatusBar1"

        '

        'ProgressBar1

        '

        Me.ProgressBar1.Location = New System.Drawing.Point(24, 64)

        Me.ProgressBar1.Name = "ProgressBar1"

        Me.ProgressBar1.TabIndex = 8

        '

        'Form1

        '

        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)

        Me.ClientSize = New System.Drawing.Size(292, 273)

        Me.Controls.Add(Me.ProgressBar1)

        Me.Controls.Add(Me.StatusBar1)

        Me.Controls.Add(Me.ToolBar1)

        Me.Controls.Add(Me.VScrollBar1)

        Me.Controls.Add(Me.HScrollBar1)

        Me.Controls.Add(Me.ComboBox1)

        Me.Controls.Add(Me.RadioButton1)

        Me.Controls.Add(Me.CheckBox1)

        Me.Controls.Add(Me.Button1)

        Me.Name = "Form1"

        Me.Text = "Form1"

        Me.ResumeLayout(False)

 

    End Sub

 

#End Region

 

End Class

 

Mainfest文件如下:

<?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="Microsoft.Winweb.WindowsApplication1"

   type="win32"

/>

<description>.NET control deployment tool</description>

<dependency>

   <dependentAssembly>

     <assemblyIdentity

       type="win32"

       name="Microsoft.Windows.Common-Controls"

       version="6.0.0.0"

       processorArchitecture="X86"

       publicKeyToken="6595b64144ccf1df"

       language="*"

     />

   </dependentAssembly>

</dependency>

</assembly>

posted @ 2005-04-14 15:52  妖居  阅读(2726)  评论(8编辑  收藏  举报