通过添加以下导入并修改 Form1 的构造函数(新子例程),我获得了在 Windows 10 和 Visual Studio 2015 中工作的 XP 样式:
Imports System.Windows.Forms.VisualStyles
Public Sub New()
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Application.VisualStyleState = VisualStyleState.NoneEnabled
End Sub
如果您想了解上述解决方案为何如此,那么您应该尝试查看下面的 MSDN 示例,我必须对其进行修改才能使其运行。首先在VS中新建一个Project,添加一个Form1,然后将下面的代码全部粘贴到Form1的代码中:
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles
Public Class Form1
Inherits Form
Private WithEvents button1 As New Button()
Private radioButton1 As New RadioButton()
Private radioButton2 As New RadioButton()
Private radioButton3 As New RadioButton()
Private radioButton4 As New RadioButton()
Public Sub New()
With button1
.AutoSize = True
.Location = New Point(10, 10)
.Text = "Update VisualStyleState"
End With
With radioButton1
.Location = New Point(10, 50)
.AutoSize = True
.Text = "Apply styles to client area only"
End With
With radioButton2
.Location = New Point(10, 70)
.AutoSize = True
.Text = "Apply styles to nonclient area only"
End With
With radioButton3
.Location = New Point(10, 90)
.AutoSize = True
.Text = "Apply styles to client and nonclient areas"
End With
With radioButton4
.Location = New Point(10, 110)
.AutoSize = True
.Text = "Disable styles in all areas"
End With
Me.Text = "VisualStyleState Test"
Me.Controls.AddRange(New Control() {button1, radioButton1,
radioButton2, radioButton3, radioButton4})
End Sub
<STAThread()>
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub
Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles button1.Click
If radioButton1.Checked Then
Application.VisualStyleState =
VisualStyleState.ClientAreaEnabled
ElseIf radioButton2.Checked Then
Application.VisualStyleState =
VisualStyleState.NonClientAreaEnabled
ElseIf radioButton3.Checked Then
Application.VisualStyleState =
VisualStyleState.ClientAndNonClientAreasEnabled
ElseIf radioButton4.Checked Then
Application.VisualStyleState =
VisualStyleState.NoneEnabled
End If
' Repaint the form and all child controls.
Me.Invalidate(True)
End Sub
在运行时,只需单击各种 RadioButton,然后单击 Button1,然后您将看到客户端(边框)和非客户端(内部控件)发生变化。