在Winform項目中,有時需要將UI變成適應Vista/Windows7/8/10的風格,通過"選用"以下代碼來使你的UI支持系統主題渲染. 註:該功能不支持XP,建議使用DS控制項庫中的WIN7風格控制項。 新建工程,添加相關控制項,複製粘貼以下代碼即可 ...
在Winform項目中,有時需要將UI變成適應Vista/Windows7/8/10的風格,通過"選用"以下代碼來使你的UI支持系統主題渲染.
註:該功能不支持XP,建議使用DS控制項庫中的WIN7風格控制項。
新建工程,添加相關控制項,複製粘貼以下代碼即可
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click DSAPI.Win7特性.任務欄特效.設置任務欄進度(Me.Handle, 50, 100) '*******縮略圖按鈕標識ID至少應200以上,以避免系統衝突 '------------------ Dim Itm1 As New DSAPI.Win7特性.任務欄縮略圖.縮略圖控制按鈕 With Itm1 .標記 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕狀態.啟用 .標識ID = 500 .標誌組合 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.提示 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.圖標 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.標記 .圖標 = My.Resources.D__32_.GetHicon .提示 = "上一首" End With '------------------ Dim Itm2 As New DSAPI.Win7特性.任務欄縮略圖.縮略圖控制按鈕 With Itm2 .標記 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕狀態.啟用 .標識ID = 501 .標誌組合 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.提示 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.圖標 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.標記 .圖標 = My.Resources.D__41_.GetHicon .提示 = "暫停" End With '------------------ Dim Itm3 As New DSAPI.Win7特性.任務欄縮略圖.縮略圖控制按鈕 With Itm3 .標記 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕狀態.啟用 .標識ID = 502 .標誌組合 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.提示 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.圖標 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.標記 .圖標 = My.Resources.D__31_.GetHicon .提示 = "播放" End With '------------------ Dim Itm4 As New DSAPI.Win7特性.任務欄縮略圖.縮略圖控制按鈕 With Itm4 .標記 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕狀態.啟用 .標識ID = 503 .標誌組合 = DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.提示 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.圖標 + DSAPI.Win7特性.任務欄縮略圖.縮略圖按鈕標誌.標記 .圖標 = Me.Icon.ToBitmap.GetHicon .提示 = "幫助" End With DSAPI.Win7特性.任務欄特效.添加縮略圖控制按鈕(Me.Handle, New DSAPI.Win7特性.任務欄縮略圖.縮略圖控制按鈕() {Itm1, Itm2, Itm3, Itm4}) AddHandler DSAPI.Win7特性.任務欄特效.縮略圖按鈕被點擊, AddressOf ItmClick End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load DSAPI.Win7特性.任務欄特效.初始化(Me) DSAPI.控制項相關.Form窗體.窗體支持滑鼠拖動(Me, True) Me.ForeColor = Color.FromArgb(255, 2, 2, 2) For Each S As String In [Enum].GetNames(ListView1.View.GetType) ComboBox1.Items.Add(S) Next ComboBox1.SelectedIndex = 0 For Each S As String In [Enum].GetNames(GetType(DSAPI.Win7特性.任務欄縮略圖.任務欄進度條狀態)) ComboBox2.Items.Add(S) Next ComboBox2.SelectedIndex = 0 End Sub Sub ItmClick(ID As Integer) Select Case ID Case 500 MsgBox("點擊了上一首") Case 501 MsgBox("點擊了暫停") Case 502 MsgBox("點擊了播放") Case 503 MsgBox("點擊了幫助") End Select End Sub Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown TreeView1.ExpandAll() End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged ListView1.View = ComboBox1.SelectedIndex End Sub Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked Try Process.Start("SystemPropertiesAdvanced.exe") Catch End Try End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If Form2.Visible = True Then Exit Sub Form2.Show() DSAPI.Win7特性.在指定窗體指定區域顯示源窗體縮略圖(Form2.Handle, New Rectangle(0, 0, Me.Width, Me.Height), Me.Handle, False, 255) End Sub Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click DSAPI.Win7特性.設置窗體為磨砂透明效果(Me) End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click DSAPI.Win7特性.使控制項應用Win7資源管理器風格(Me) End Sub Private Sub TrackBar1_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar1.ValueChanged DSAPI.Win7特性.任務欄特效.設置任務欄進度(Me.Handle, TrackBar1.Value, TrackBar1.Maximum) End Sub Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged DSAPI.Win7特性.任務欄特效.設置任務欄進度條狀態(Me.Handle, GetV(ComboBox2.SelectedItem.ToString)) End Sub Private Function GetV(S As String) As Integer Dim Ints() As Integer = [Enum].GetValues(GetType(DSAPI.Win7特性.任務欄縮略圖.任務欄進度條狀態)) For Each T As Integer In Ints If [Enum].GetName(GetType(DSAPI.Win7特性.任務欄縮略圖.任務欄進度條狀態), T) = S Then Return T End If Next Return 0 End Function End Class