请问各位大虾,下面的代码:
VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
在XP样式下运行没有错误,但是若把"显示 属性"->"外观"的"窗口和按钮"设置为"Windows经典样式"就会出错:“当前没有活动的视觉样式,因此与视觉样式相关的操作导致错误。”
我的程序要在Windows server 2003上运行,但是出现相同的错误。
我想在自定义的控件中使用树形控件的+、-号来表示展开和折叠,这个问题应该怎么解决啊?
VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
在XP样式下运行没有错误,但是若把"显示 属性"->"外观"的"窗口和按钮"设置为"Windows经典样式"就会出错:“当前没有活动的视觉样式,因此与视觉样式相关的操作导致错误。”
我的程序要在Windows server 2003上运行,但是出现相同的错误。
我想在自定义的控件中使用树形控件的+、-号来表示展开和折叠,这个问题应该怎么解决啊?
然后
treeView1.Nodes[0].ImageIndex = 0;//可以是+号和其它图片
treeView1.SelectedNode.SelectedImageIndex = 1;//选中节点的图片设置节点的 ImageIndex 和 SelectedImageIndex 属性。ImageIndex 属性确定正常和展开状态下的节点显示的图像, SelectedImageIndex 属性确定选定状态下的节点显示的图像。 这样不管是XP,Vista,还是Win2003都一致了
http://blogs.msdn.com/rideout/archive/2006/01/08/510700.aspxone of the comments contains the fix you need (someone else realized it requires styles, and wrote a check in case they're not available). In case that blog ever disappears, here is the relevant code:
in "TreeGridView.cs" delete the followin' two lines: ------8<------
internal VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
internal VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed);
------8<------ in "TreeGridCell.cs" find the line "if (node.HasChildren || node._grid.VirtualNodes)" and change it so that it looks like that: ------8<------
if (node.HasChildren || node._grid.VirtualNodes)
{
// Ensure that visual styles are supported.
if (Application.RenderWithVisualStyles)
{
VisualStyleRenderer rOpen = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Opened);
VisualStyleRenderer rClosed = new VisualStyleRenderer(VisualStyleElement.TreeView.Glyph.Closed); // Paint node glyphs
if (node.IsExpanded)
//node._grid.rOpen.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
rOpen.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
else
//node._grid.rClosed.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
rClosed.DrawBackground(graphics, new Rectangle(glyphRect.X, glyphRect.Y + (glyphRect.Height / 2) - 4, 10, 10));
}
else
{
int h = 8;
int w = 8;
int x = glyphRect.X;
int y = glyphRect.Y + (glyphRect.Height / 2) - 4;
//MessageBox.Show("x = " + x.ToString() + ", y= " + y.ToString()); graphics.DrawRectangle(new Pen(SystemBrushes.ControlDark), x, y, w, h);
graphics.FillRectangle(new SolidBrush(Color.White), x + 1, y + 1, w - 1, h - 1);
graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x + 2, y + 4, x + w - 2, y + 4); if (!node.IsExpanded)
graphics.DrawLine(new Pen(new SolidBrush(Color.Black)), x + 4, y + 2, x + 4, y + h - 2);
}
}
------8<------