gpt4 book ai didi

c# - TreeView.DrawNode 的问题 - OwnerDrawText

转载 作者:太空狗 更新时间:2023-10-29 17:42:43 26 4
gpt4 key购买 nike

我有一个连接到远程服务器并在需要时轮询数据的应用程序。它有一个 TreeView,其中节点代表可用的对象,文本的颜色表示数据是否已加载;灰色斜体表示未加载,黑色,常规文本已加载。

目前我已将 TreeView 设置为 OwnderDrawText 并让 TreeView.DrawNode 函数简单地绘制文本:

private void TreeViewDrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (!e.Node.IsVisible)
{
return;
}

bool bLoaded = false;

if (e.Bounds.Location.X >= 0 && e.Bounds.Location.Y >= 0)
{
if(e.Node.Tag != null)
{
//...
// code determining whether data has been loaded is done here
// setting bLoaded true or false
//...
}
else
{
e.DrawDefault = true;
return;
}

Font useFont = null;
Brush useBrush = null;

if (bLoaded)
{
useFont = e.Node.TreeView.Font;
useBrush = SystemBrushes.WindowText;
}
else
{
useFont = m_grayItallicFont;
useBrush = SystemBrushes.GrayText;
}
e.Graphics.DrawString(e.Node.Text, useFont, useBrush, e.Bounds.Location);
}
}

我认为这就足够了,但是,这导致了一些问题;

  1. 当一个节点被选中时,无论是否聚焦,它都不会包含所有文本,example (我希望 imgur 没事)。
  2. 当节点获得焦点时,虚线轮廓也不会显示。如果你将它与这个 example 进行比较.文中带有“log”的节点使用的是e.DefaultDraw = true

我尝试按照 this 中给出的示例进行操作问题。它看起来像这样:

private void TreeViewDrawNode(object sender, DrawTreeNodeEventArgs e)
{
if (!e.Node.IsVisible)
{
return;
}

bool bLoaded = false;

if (e.Bounds.Location.X >= 0 && e.Bounds.Location.Y >= 0)
{
if(e.Node.Tag != null)
{
//...
// code determining whether data has been loaded is done here
// setting bLoaded true or false
//...
}
else
{
e.DrawDefault = true;
return;
}

//Select the font and brush depending on whether the property has been loaded
Font useFont = null;
Brush useBrush = null;

if (bLoaded)
{
useFont = e.Node.TreeView.Font;
useBrush = SystemBrushes.WindowText;
}
else
{
//member variable defined elsewhere
useFont = m_grayItallicFont;
useBrush = SystemBrushes.GrayText;
}

//Begin drawing of the text

//Get the rectangle that will be used to draw
Rectangle itemRect = e.Bounds;
//Move the rectangle over by 1 so it isn't on top of the check box
itemRect.X += 1;

//Figure out the text position
Point textStartPos = new Point(itemRect.Left, itemRect.Top);
Point textPos = new Point(textStartPos.X, textStartPos.Y);

//generate the text rectangle
Rectangle textRect = new Rectangle(textPos.X, textPos.Y, itemRect.Right - textPos.X, itemRect.Bottom - textPos.Y);

int textHeight = (int)e.Graphics.MeasureString(e.Node.Text, useFont).Height;
int textWidth = (int)e.Graphics.MeasureString(e.Node.Text, useFont).Width;

textRect.Height = textHeight;

//Draw the highlighted box
if ((e.State & TreeNodeStates.Selected) != 0)
{
//e.Graphics.FillRectangle(SystemBrushes.Highlight, textRect);
//use pink to see the difference
e.Graphics.FillRectangle(Brushes.Pink, textRect);
}
//widen the rectangle by 3 pixels, otherwise all of the text won't fit
textRect.Width = textWidth + 3;

//actually draw the text
e.Graphics.DrawString(e.Node.Text, useFont, useBrush, e.Bounds.Location);

//Draw the box around the focused node
if ((e.State & TreeNodeStates.Focused) != 0)
{
textRect.Width = textWidth;
Pen focusPen = new Pen(Color.Black);
focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawRectangle(focusPen, textRect);
}
}
}

然而,结果是this . (注意,使用粉红色来区分颜色)。如您所见,突出显示的背景并没有一直延伸到聚焦虚线所在的位置。还有另一个盒子也被绘制出来了。

我对如何解决这个问题有点困惑。我想要的只是在加载某些内容时使用灰色斜体文本。第一种也是最简单的方法不太奏效,第二种方法感觉我做得太多了。

毕竟,有没有人对如何正确执行此操作有任何建议,因为必须有更简单的方法。

提前谢谢你。

最佳答案

您需要使用 TextRenderer.DrawText()。这就是 TreeView 使用的,它呈现的文本与 Graphics.DrawString() 略有不同。

关于c# - TreeView.DrawNode 的问题 - OwnerDrawText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2053056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com