在 C# 中在 TabPage 上画一条线

发布于 2024-12-09 05:20:09 字数 503 浏览 3 评论 0原文

我在 TabPage 上画线时遇到问题。

我实际上在 TabControl 中有一个 TabControl。我画了一些标签,用作盒子。我想画线将它们连接在一起。

我已经尝试过:

Pen P = new Pen(System.Drawing.Color.Black, 10);
tabname.CreateGraphics().DrawLine(P, 10, 10, 100, 100);

并且

Pen P = new Pen(System.Drawing.Color.Black, 10);            
tabcontrolname.TabPages[0].CreateGraphics().DrawLine(P, 10, 10, 100, 100);

都没有显示该行。我假设这条线被放置在某个地方,因为没有错误。

有什么想法可以让它显示在正确的 TabPage 上吗?

谢谢你!

I am having trouble drawing a line on a TabPage.

I actually have a TabControl inside a TabControl. I have drawn a number of labels which I am using as boxes. I want to draw lines to join them together.

I have tried:

Pen P = new Pen(System.Drawing.Color.Black, 10);
tabname.CreateGraphics().DrawLine(P, 10, 10, 100, 100);

and

Pen P = new Pen(System.Drawing.Color.Black, 10);            
tabcontrolname.TabPages[0].CreateGraphics().DrawLine(P, 10, 10, 100, 100);

Neither are displaying the line. I assume the line is being placed somewhere as there are no errors.

Any ideas how I can get it to display on the correct TabPage?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏雪无痕 2024-12-16 05:20:09

您可能需要重写 OnPaint 方法(或处理 Paint 事件)才能使其正常工作。如果你不这样做,你的控件最终只会覆盖你的线条。

以下是相关文档的链接。

You probably need to override the OnPaint method (or handle the Paint event) to get this to work properly. If you don't your controls will just end up drawing over your lines.

Here's a link to the relevant docs.

晚雾 2024-12-16 05:20:09

您在哪里尝试这些代码,在哪个函数中?如果您在初始化或构造时执行一次,它们将不会按您的预期显示。每当需要重新绘制控件时,您也需要再次绘制这条线。重写控件的 OnPaint 方法或注册 Paint 事件并在那里进行线条绘制。

Where do you try these codes, in which function? If you are doing it once in the initialization or construction, they will not be displayed as you expect. Whenever the control needs to be redrawn, you need to draw this line too, again. Either override the OnPaint method of the control or register for the Paint event and do the line drawing there.

心房敞 2024-12-16 05:20:09

我能够使用以下代码显示箭头:

TabPage.Paint += new PaintEventHandler(TabPage_Paint);

但是

        protected void TabPage_Paint(object sender, PaintEventArgs e)
    {
        base.OnPaint(e);
        Pen arrow = new Pen(Brushes.Black, 4);
        arrow.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

        e.Graphics.DrawLine(arrow, 10, 10, 100, 100);
        arrow.Dispose();
    }

,当启动滚动时,画图会变得混乱:(

I was able to get the arrow to show up using the following code:

TabPage.Paint += new PaintEventHandler(TabPage_Paint);

and

        protected void TabPage_Paint(object sender, PaintEventArgs e)
    {
        base.OnPaint(e);
        Pen arrow = new Pen(Brushes.Black, 4);
        arrow.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;

        e.Graphics.DrawLine(arrow, 10, 10, 100, 100);
        arrow.Dispose();
    }

However, when scrolling is initiated the Paint messes up :(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文