TreeView所有者在选择时绘制故障
我正在尝试向标准 System.Windows.Forms.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 现在实际上正在绘制它。此行为出现在带有 .Net 3.5 的 Windows XP 中。
有什么办法可以解决这种奇怪的行为吗?
I'm trying to add a few more icons to elements of a standard System.Windows.Forms.TreeView control.
My plan was to only change the label area of the treeview control, but it shows a strange behaviour. If I click a node to select it, when the mouse button is depressed the background is draw correctly with the highlight color. However, the text is the wrong unselected color until I release the mouse button. It's as if e.State
contains the wrong state between when the mouse button is pressed and released.
Here is what I'm doing: I init with this.DrawMode = TreeViewDrawMode.OwnerDrawText
and then register my event handler with this.DrawNode += LayoutTreeView_DrawNode
. Here is the handler:
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);
}
If I set the handler to its default case...
void LayoutTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
e.DefaultDraw = true;
}
...the same thing happens, which is weird since windows is actually drawing it now. This behaviour is in Windows XP with .Net 3.5.
Is there any way to work around this strange behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改
为
这适用于 Vista x64 和带有 .Net 3.5 的 VS 2008。让我知道它是否适合您。
我在观察默认窗口行为时观察到的是,直到选择节点并获得焦点后才会绘制文本和突出显示。因此,我检查了聚焦条件以更改文本颜色。然而,这并不完全模仿 Widows 的行为,即在释放鼠标之前不会使用新颜色。当它选择在 Ownerdrawn 模式下绘制蓝色突出显示状态与窗口绘制它时的状态变化时,这似乎是一点......这无疑是令人困惑的。
编辑
但是,当您创建自己的派生树视图时,您可以完全控制何时绘制所有内容。
Change
to
This worked on Vista x64 and VS 2008 with .Net 3.5. Let me know if it works for you.
What I observed when watching the default windows behavior was that the text and highlight weren't drawn until the node was selected and had focus. So I checked for the focused condition in order to change the text color. However this doesn't precisely mimic the Widows behavior where the new colors aren't used until the mouse is released. It appears the point when it chooses to draw the blue highlight status changes when in ownerdrawn mode versus windows drawing it... Which admittedly is confusing.
EDIT
However, when you create your own derived treeview you have full control over when everything is drawn.