如何在应用SetWindowTheme后更改winforms树视图的颜色

时间:2014-09-29 06:04:33

标签: c# winforms treeview

我有一个Windows窗体树视图。树视图的默认加减号图标不会使用Windows 7样式进行更新。

为了达到这个目的,我使用了UXTheme.dll的SetWindowTheme

SetWindowTheme(tvwRoot.Handle, "explorer", null);

应用此功能后,我无法更改树视图的selectednode颜色。 我尝试了以下内容,

private void tvwRoot_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {

        if (tvwRoot.GetNodeAt(e.Location) != null)
        {
            tvwRoot.SelectedNode = tvwRoot.GetNodeAt(e.Location);

            tvwRoot.SelectedNode.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
            tvwRoot.SelectedNode.ForeColor = System.Drawing.Color.White;

            if (e.Button == MouseButtons.Right)
            {
                nodRightClick = tvwRoot.SelectedNode;
                blnRightClick = true;
                pntLocation = e.Location;
            }
        }
        else
        {
            tvwRoot.SelectedNode = null;
            nodRightClick = null;
        }

    }

    private void tvwRoot_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (tvwRoot.SelectedNode != null)
        {
            if (tnPreviousSelected != null && !tnPreviousSelected.FullPath.Equals(tvwRoot.SelectedNode.FullPath))
                tnPreviousSelected = tvwRoot.SelectedNode;
            else if (tnPreviousSelected == null)
                tnPreviousSelected = tvwRoot.SelectedNode;


            tvwRoot.SelectedNode.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
            tvwRoot.SelectedNode.ForeColor = System.Drawing.Color.White;
        }

    }

    private void tvwRoot_AfterSelect(object sender, TreeViewEventArgs e)
    {
        if (tnPreviousSelected != null && !tnPreviousSelected.FullPath.Equals(tvwRoot.SelectedNode.FullPath))
        {
            tnPreviousSelected.BackColor = tvwRoot.BackColor;
            tnPreviousSelected.ForeColor = tvwRoot.ForeColor;
            tnPreviousSelected = null;
        }

        if (tvwRoot.SelectedNode != null)
        {
            if (tnPreviousSelected != null && !tnPreviousSelected.FullPath.Equals(tvwRoot.SelectedNode.FullPath))
                tnPreviousSelected = tvwRoot.SelectedNode;
            else if (tnPreviousSelected == null)
                tnPreviousSelected = tvwRoot.SelectedNode;

            tvwRoot.SelectedNode.BackColor = System.Drawing.Color.FromArgb(51, 153, 255);
            tvwRoot.SelectedNode.ForeColor = System.Drawing.Color.White;
        }
    }

但是,在树视图中移除焦点后,树视图上会显示颜色。 有没有办法在应用SetWindow主题后更改颜色?

0 个答案:

没有答案