2008-08-05 26 views
27

我有一個模仿ComboBox的控件。我想呈現控件,以便控件邊框看起來像標準的Windows組合框。具體來說,我遵循MSDN文檔,並且控件的所有呈現都是正確的,除了在禁用控件時呈現。如何使控件看起來像啓用了視覺樣式的組合框?

需要說明的是,這適用於啓用了視覺樣式的系統。此外,控件的所有部分都呈現正確,除了禁用的控件的邊框外,其與禁用的組合框邊框顏色不匹配。我正在使用VisualStyleRenderer類類。 MSDN建議使用VisualStyleElement.TextBox元件爲文本框部分組合框控制的,但一個標準禁用文本框和一個標準禁用組合框繪製略有不同(一個具有淺灰色的邊框,另淺藍色邊框) 。

如何在禁用狀態下正確呈現控件?

回答

1

是否有任何ControlPaint方法對此有用?這就是我通常用於自定義渲染控件的原因。

8

我不是100%確定如果這是您正在尋找的內容,但您應該查看System.Windows.Forms.VisualStyles命名空間中的VisualStyleRenderer

  1. VisualStyleRenderer class(MSDN)
  2. How to: Render a Visual Style Element(MSDN)
  3. VisualStyleElement.ComboBox.DropDownButton.Disabled(MSDN)

因爲如果用戶沒有啓用視覺樣式(VisualStyleRenderer將無法正常工作,他/她可能在Windows XP之前運行「經典模式」或操作系統),您應該始終擁有對ControlPaint類的回退功能。

// Create the renderer. 
if (VisualStyleInformation.IsSupportedByOS 
    && VisualStyleInformation.IsEnabledByUser) 
{ 
    renderer = new VisualStyleRenderer(
     VisualStyleElement.ComboBox.DropDownButton.Disabled); 
} 

,然後做這樣的繪畫時:

if(renderer != null) 
{ 
    // Use visual style renderer. 
} 
else 
{ 
    // Use ControlPaint renderer. 
} 

希望它能幫助!

相關問題