2009-12-17 60 views
6

我有一個組合框,我已經設置DrawMode = DrawMode.OwnerDrawFixed。然後我處理OnDrawItem事件,並且一切正常。但是,它看起來與標準的ComboBox有很大的不同,因爲我的VisualStyles似乎不能渲染。我是否需要做一些事情來爲我的所有者繪製控件專門啓用VisualStyle渲染?我在我的控件上嘗試過SetWindowTheme,但我不確定要發送什麼主題類。任何幫助將非常感激。謝謝!OwnerDraw與VisualStyles的組合框

回答

6

所有者繪製的缺點是當你打開它時,所有者(你)必須繪製所有東西。你幾乎完全靠自己。

如果你想要視覺樣式,那麼你必須直接調用VisualStyles API來做你想做的事情。如果你想顯示選定的,集中的,啓用/禁用狀態,那麼你必須編寫代碼來處理它們。

這不是您的組合框問題的直接答案,但作爲如何使用VisualStyles的示例,以下是我如何在所有者繪製的TreeView中使用VisualStyles繪製加號/減號圖標:

// Draw Expand (plus/minus) icon if required 
if (ShowPlusMinus && e.Node.Nodes.Count > 0) 
{ 
    // Use the VisualStyles renderer to use the proper OS-defined glyphs 
    Rectangle expandRect = new Rectangle(iconLeft-1, midY - 7, 16, 16); 

    VisualStyleElement element = (e.Node.IsExpanded) ? VisualStyleElement.TreeView.Glyph.Opened 
                : VisualStyleElement.TreeView.Glyph.Closed; 

    VisualStyleRenderer renderer = new VisualStyleRenderer(element); 
      renderer.DrawBackground(e.Graphics, expandRect); 
} 
+0

我試圖使用VisualStyleRenderer繪製的一部分,但我能找到的唯一VisualStyleElement是ComboBox.Button。我會更多地調查這些,或者放棄,讓它們都變得沒有風格。 :) – 2009-12-17 20:20:31