C# WinForm TreeView改變選中節點顏色,失去焦點時選中節點仍突顯。

來源:互聯網
上載者:User
當使用者焦點離開TreeView時,TreeView選中節點仍然高亮,但是顏色太淺,幾乎看不出來。
這裡重寫一下DrawMode();可以控制選中節點顏色。
TreeView.HideSelection = False;可讓選中節點保持高亮。
參考:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.treeview.hideselection(VS.80).aspxpublic FrmMain()
{
    InitializeComponent();
    treeView1.HideSelection = False;
    //自已繪製
    this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawText;
    this.treeView1.DrawNode += new DrawTreeNodeEventHandler(treeView1_DrawNode);
}

//在繪製節時間點事件中,按自已想的繪製
private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    e.DrawDefault = true; //我這裡用預設顏色即可,只需要在TreeView失去焦點時選中節點仍然突顯
    return;

    if ((e.State & TreeNodeStates.Selected) != 0)
    {
        //示範為綠底白字
        e.Graphics.FillRectangle(Brushes.DarkBlue, e.Node.Bounds);

        Font nodeFont = e.Node.NodeFont;
        if (nodeFont == null) nodeFont = ((TreeView)sender).Font;
        e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,Rectangle.Inflate(e.Bounds, 2, 0));
    }
    else
    {
        e.DrawDefault = true;
    }

    if ((e.State & TreeNodeStates.Focused) != 0)
    {
        using (Pen focusPen = new Pen(Color.Black))
        {
            focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            Rectangle focusBounds = e.Node.Bounds;
            focusBounds.Size = new Size(focusBounds.Width - 1,
            focusBounds.Height - 1);
            e.Graphics.DrawRectangle(focusPen, focusBounds);
        }
    }

}

url:http://greatverve.cnblogs.com/archive/2012/03/21/treeview-HideSelection.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.