C# Windows窗体:图形。抽绳不起作用

C# Windows窗体:图形。抽绳不起作用,c#,winforms,C#,Winforms,下面是我表单类中的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { BstTree tree = new BstTree(); tr

下面是我表单类中的代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 private void Form1_Load(object sender, EventArgs e)
        {
            BstTree tree = new BstTree();
            tree.addRoot(50);
            for (int i = 1; i < 50; i++)
            {
                int b = rnd.Next(1, 100);
                listBox1.Items.Add(b);
                tree.AddNode(tree.root, b);
            }
            tree.treeOutput(tree.root, this);
        }
        public void draw(Point prevPT, Point currentPT)
        {
            Graphics p = CreateGraphics();
            Pen pen = new Pen(Color.Red, 5);
            p.DrawLine(pen, prevPT, currentPT);
        }
    }

但是它不画任何东西,也不知道如何解决这个问题……

位图中的绘图是静态的,您可以使用自己的
图形
对象来实现这一点;然而,WinForms中的绘图非常动态,因为绘图非常短暂。最小化表单、调整表单大小、将表单部分移出可见屏幕区域或在表单顶部打开另一个应用程序都会破坏绘制的内容。因此,Windows操作系统实现了一种巧妙的绘图机制,每当表单、控件或其部分必须重新绘制时,都会向应用程序发送消息。这将在应用程序中引发绘制事件,意味着操作系统将确定何时必须绘制(或重画)。因此,绘制例程必须发生在绘制事件处理程序中

如果要自己绘制树节点,请通过从
TreeView
派生树控件来创建自己的树控件,并相应地更改
DrawMode

public class MyTreeView : TreeView
{
    public MyTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawAll;
    }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        if (e.Node.IsVisible) {
            // Draw background of node.
            if (e.Node == e.Node.TreeView.SelectedNode) {
                e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds);
            } else {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }
            using (Pen p = new Pen(Color.Red, 5))
            {
                // TODO: Implement your drawing logic here
            }
            e.Graphics.DrawString(e.Node.Text, this.Font, Brushes.Black,
                                  e.Bounds.Left + delta, e.Bounds.Top + 1);
        }
    }
}

这只是一张草图。您可能需要考虑其他的细节,如“代码> E.No.IsPosisie和<代码> E.Stase./P>您在哪里调用<代码> TeeReutial方法?请显示相关代码(以及您的调试工作)请提供可测试代码,以再现问题。这里缺的东西太多了。什么是
pt
?什么是
节点
?我怀疑这些部分可以简单地省略,并且仍然可以重现这个问题。你在显示表单之前绘制它。请参阅(即
Load
event)。另一件事是这种绘图只会持续到下一个
Paint
事件,而不是
Form1
中的
draw
方法。参数应该成为属性,如果您将其更改为强制重画,请调用
Invalidate()
。CreateGraphics始终是一个否。在您的情况下,它实际上不起作用,因为表单在加载事件中还不可见。没什么可借鉴的。
public class MyTreeView : TreeView
{
    public MyTreeView()
    {
        DrawMode = TreeViewDrawMode.OwnerDrawAll;
    }

    protected override void OnDrawNode(DrawTreeNodeEventArgs e)
    {
        if (e.Node.IsVisible) {
            // Draw background of node.
            if (e.Node == e.Node.TreeView.SelectedNode) {
                e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds);
            } else {
                e.Graphics.FillRectangle(Brushes.White, e.Bounds);
            }
            using (Pen p = new Pen(Color.Red, 5))
            {
                // TODO: Implement your drawing logic here
            }
            e.Graphics.DrawString(e.Node.Text, this.Font, Brushes.Black,
                                  e.Bounds.Left + delta, e.Bounds.Top + 1);
        }
    }
}