2010-03-09 34 views
4

我想添加一些標準的System.Windows.Forms.TreeView控件的元素更多的圖標。TreeView所有者繪製故障時選擇

我的計劃是隻改變treeview控件的標籤區域,但它顯示出一個奇怪的行爲。如果我點擊一個節點來選擇它,當鼠標按鈕被按下時,背景被用高亮顏色正確繪製。但是,在釋放鼠標按鈕之前,文本是錯誤的未選中的顏色。就好像e.State在鼠標按鈕被按下和釋放之間包含錯誤的狀態。

這是我在做什麼:我初始化爲this.DrawMode = TreeViewDrawMode.OwnerDrawText,然後註冊我的事件處理程序this.DrawNode += LayoutTreeView_DrawNode。這裏是處理:

void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e) 
{ 

    Color color = (e.State & TreeNodeStates.Selected) != 0 ? 
     SystemColors.HighlightText : SystemColors.WindowText; 

    TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine | 
     TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis; 

    TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, flags); 
} 

如果我設置處理其默認情況下...

void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e) 
{ 
    e.DefaultDraw = true; 
} 

...同樣的事情發生,這是因爲怪異的窗戶現在實際上是畫它。此行爲在Windows XP中使用.Net 3.5。

有什麼辦法解決這個奇怪的行爲?

+0

僅供參考,包含您正在處理的.Net版本總是有用的。我會試着看看你是否完全理解你所看到的內容。如果我想我會告訴你我在想什麼。 – 2010-03-10 21:07:45

+0

另外,我從來都不喜歡在控件的使用者中繪畫。我總是推動讓它在派生類中完成,這樣,如果在另一個表單或應用程序上需要相同的行爲,則更容易。 – 2010-03-10 21:08:37

+0

我無法使用繪圖默認值重現所描述的行爲,但可以使用自定義代碼。我正在運行Vista x64; VS 2008.Net 3.5。你可以添加你使用的操作系統,Visual Studio和.Net版本嗎? (我聽說有傳言說Vista和Windows 7在操作系統中與TreeViews的工作方式有些不同。) – 2010-03-10 21:13:37

回答

3

變化

Color color = (e.State & TreeNodeStates.Selected) != 0 ? 
    SystemColors.HighlightText : SystemColors.WindowText; 

Color color = (e.State & TreeNodeStates.Focused) != 0 ? 
    SystemColors.HighlightText : SystemColors.WindowText; 

這個工作在Vista x64和VS 2008與.net 3.5。請讓我知道這對你有沒有用。

我在觀察默認窗口行爲時觀察到的情況是,直到節點被選中並獲得焦點後,纔會繪製文本和高光。所以我檢查了聚焦狀態以改變文字顏色。然而,這並不完全模仿Widows行爲,在鼠標被釋放之前不會使用新顏色。它似乎是當它選擇繪製藍色突出顯示狀態改變時,在自畫像模式與繪製它的窗口......這一點顯然是混亂。

編輯 但是,當您創建自己的派生樹視圖時,您可以完全控制何時繪製所有內容。

public class MyTreeView : TreeView 
{ 
    bool isLeftMouseDown = false; 
    bool isRightMouseDown = false; 
    public MyTreeView() 
    { 
     DrawMode = TreeViewDrawMode.OwnerDrawText; 
    } 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     TrackMouseButtons(e); 
     base.OnMouseDown(e); 
    } 

    protected override void OnMouseUp(MouseEventArgs e) 
    { 
     TrackMouseButtons(e); 
     base.OnMouseUp(e); 
    } 
    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     TrackMouseButtons(e); 
     base.OnMouseMove(e); 
    } 

    private void TrackMouseButtons(MouseEventArgs e) 
    { 
     isLeftMouseDown = e.Button == MouseButtons.Left; 
     isRightMouseDown = e.Button == MouseButtons.Right; 
    } 

    protected override void OnDrawNode(DrawTreeNodeEventArgs e) 
    { 
     // don't call the base or it will goof up your display! 
     // capture the selected/focused states 
     bool isFocused = (e.State & TreeNodeStates.Focused) != 0; 
     bool isSelected = (e.State & TreeNodeStates.Selected) != 0; 
     // set up default colors. 
     Color color = SystemColors.WindowText; 
     Color backColor = BackColor; 

     if (isFocused && isRightMouseDown) 
     { 
      // right clicking on a 
      color = SystemColors.HighlightText; 
      backColor = SystemColors.Highlight; 
     } 
     else if (isSelected && !isRightMouseDown) 
     { 
      // if the node is selected and we're not right clicking on another node. 
      color = SystemColors.HighlightText; 
      backColor = SystemColors.Highlight; 
     } 

     using (Brush sb = new SolidBrush(backColor)) 
      e.Graphics.FillRectangle(sb,e.Bounds); 

     TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.SingleLine | 
      TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis; 

     TextRenderer.DrawText(e.Graphics, e.Node.Text, Font, e.Bounds, color, backColor, flags); 
    } 
} 
+0

此外,使用mouseup和down捕獲鼠標按鈕不起作用。這可能是特定於XP的,但控件已經在處理選擇和重畫之前,在控件中提升了事件。 – Coincoin 2010-03-11 16:42:10

+0

我嘗試了類似的東西,它可以正常工作....直到您滾動並拖放。我自res自己繪製整個項目,現在至少文本與背景顏色同步,即使行爲有點奇怪。我會接受你的答案,因爲它是最接近我們可以達到原始的XP行爲沒有小故障。 – Coincoin 2010-03-11 16:44:47

+0

OOf。我想我沒有打擾和滾動和拖放。我已經完成了自己的一切。這是很多代碼,但如果需要的話值得。 (我相信我的代碼是〜1000 LOC,因爲我需要繪製複選框,再加上減號和行......) – 2010-03-12 00:14:12