Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

在 .NET Windows Forms 應用程式中啟用 Windows XP 視覺樣式控件完整教學

編輯:XP 系統基礎知識

概要

Windows XP 引入全新視覺樣式:圓角按鈕、漸層進度條、立體核取方塊等。Visual Studio .NET (2002/2003) 雖內建相同控件,但預設仍呈現 Windows 98/2000 風格。本文詳細說明如何透過 FlatStyle.System 與 Manifest 檔,讓自訂表單套用 XP 原生外觀,並附完整 VB.NET / C# 實作範例。

核心原理:兩大關鍵

  1. 控件層級: 繼承自 ButtonBase 的控件 (Button, CheckBox, RadioButton, GroupBox) 擁有 FlatStyle 屬性,設為 System 即由作業系統繪製,自動套用當前視覺樣式。
  2. 應用程式層級: 必須綁定 Comctl32.dll 版本 6.0 (內含新版通用控件),透過 Manifest 檔 宣告相依性,才能在 XP 上載入新版 DLL;否則系統仍載入舊版 5.0,控件外觀不變。

受影響控件對照表

類別控件說明
自動套用 (無需 FlatStyle)ProgressBar進度條自動呈現 XP 樣式
TabControl索引標籤頁面
ToolBar / StatusBar工具列/狀態列
TreeView / ListView樹狀/清單檢視
ScrollBar (非客戶區)視窗框架捲軸由 OS 自動處理
其餘 Common Controls 6.0 內建控件同上
需設 FlatStyle.SystemButton圓角、按下動畫
CheckBox立體勾選框
RadioButton圓形選項鈕
GroupBox圓角群組框
外觀不變Label / LinkLabel純文字/連結標籤
DomainUpDown / NumericUpDown微調控件
CheckedListBox核取清單方塊
TextBox / RichTextBox文字方塊
ComboBox / ListBox下拉/清單方塊
DataGrid 等複合控件由子元件決定

Manifest 檔詳細規格

Manifest 是 XML 格式的應用程式資訊清單,告知載入器:「此程式需載入 Comctl32.dll v6.0」。兩種部署方式:

  • 外部檔: 與 .exe 同目錄,命名為 YourApp.exe.manifest。
  • 內嵌資源: 專案加入為資源 (RT_MANIFEST, ID=1),編譯進 .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="CompanyName.ProductName.YourApp"
    type="win32"
  />
  <description>Your Application Description</description>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        processorArchitecture="X86"
        publicKeyToken="6595b64144ccf1df"
        language="*"
      />
    </dependentAssembly>
  </dependency>
</assembly>
關鍵點: processorArchitecture="X86" 適用 32 位元應用;若編譯為 64 位元請改 amd64。publicKeyToken 必須完全一致。

完整實作步驟 (以 Visual Studio .NET 2003 為例)

步驟 1:建立專案並配置控件

  1. 新增「Windows 應用程式」專案 (VB.NET 或 C#),記住專案名稱與輸出路徑。
  2. 從工具箱拖曳以下控件至表單:
    Button、RadioButton、CheckBox、ProgressBar、Label (對照用)。
  3. 選取 Button、RadioButton、CheckBox (可 Ctrl+點擊多選) → 屬性視窗將 FlatStyle 設為 System。
  4. 雙擊 Button 建立 Click 事件,撰寫測試碼:
' VB.NET
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click
    ProgressBar1.Value = 50
End Sub

// C#
private void button1_Click(object sender, System.EventArgs e)
{
    progressBar1.Value = 50;
}

步驟 2:建立並加入 Manifest 檔

  1. 方案總管右鍵專案 → 「加入」→「加入新項目」→ 選「文字檔」。
  2. 命名規則:<專案名稱>.exe.manifest (例如 MyXPApp.exe.manifest)。
  3. 貼上上方 Manifest 範本,修改 assemblyIdentity name 與 description。
  4. 儲存後,將檔案複製到專案輸出目錄 (通常是 bin\Debug 或 bin\Release),與 .exe 同層。

步驟 3 (進階):內嵌 Manifest 為資源 (免外部檔)

  1. 專案右鍵 → 「屬性」→ 「資源」索引標籤 → 「加入現有檔案」選取 .manifest。
  2. 或使用 rc.exe / cvtres.exe 命令列手動嵌入 (ID=1, 類型=RT_MANIFEST)。
  3. 重新建置專案,輸出的 .exe 即內含 Manifest。

步驟 4:驗證成果

在 Windows XP 上執行編譯後的 .exe:

  • Button、CheckBox、RadioButton、GroupBox 呈現圓角、漸層、按下動畫。
  • ProgressBar 顯示 XP 風格分段漸層條 (而非實心藍條)。
  • Label 等非受影響控件保持原樣,作為對照。

XP 風格控件示意 預設 VS.NET 控件示意

常見問題排查

現象可能原因解決方向
控件仍為舊風格Manifest 未生效 (檔名錯誤、未置同目錄、未內嵌)用 mt.exe -inputresource:YourApp.exe;#1 驗證內嵌資源;或用 sxstrace 追蹤並存載入失敗
僅部分控件變樣FlatStyle 未設 System、或控件不支援 (如 TextBox)確認屬性;查閱上方對照表
在 Windows 2000/98 執行報錯Manifest 宣告 v6.0 但系統無此 DLLXP 專用技術;舊系統請移除 Manifest 或用條件編譯
應用程式無法啟動 (Side-by-side 錯誤)Manifest XML 格式錯誤、publicKeyToken 錯誤用 XML 驗證工具檢查;參考 Microsoft 官方範本

結語

啟用 XP 視覺樣式僅需兩步:控件設 FlatStyle.System + 專案加入 Manifest 綁定 Comctl32.dll v6.0。掌握此技巧,即可讓 .NET 1.x/2.0 應用程式在 XP 上擁有原生現代化外觀,提升使用者體驗,且無額外執行期負擔。

Copyright © Windows教程網 All Rights Reserved