Sunday, September 28, 2014

How to change the colors of the winforms treeview after SetWindowTheme has been applied

I have a windows forms treeview. The default plus minus icons of the treeview are not updated with the Windows 7 styles.

To achieve that I have used the SetWindowTheme from UXTheme.dll



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

After applying this, I am unable to change the selectednode color of the treeview. I tried the following,



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;
}
}



But with this the color appears on the treeview after the focus is removed from the treeview. Is there any way to change the color after SetWindowTheme has been applied?



No comments:

Post a Comment