图中的WinForms线 [英] Drawing a line in Winforms

查看:148
本文介绍了图中的WinForms线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绘制在一个组中的线路在一个简单的Windows窗体。

I am having trouble drawing a line within a group box in a simple windows form.

这是我的code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();                        
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);            
            DrawLShapeLine(groupBox1.CreateGraphics(), 10, 10, 20, 40);
        }

        public void DrawLShapeLine(System.Drawing.Graphics g, int intMarginLeft, int intMarginTop, int intWidth, int intHeight)
        {
            Pen myPen = new Pen(Color.Black);
            myPen.Width = 2;
            // Create array of points that define lines to draw.
            int marginleft = intMarginLeft;
            int marginTop = intMarginTop;
            int width = intWidth;
            int height = intHeight;
            int arrowSize = 3;
            Point[] points =
             {
                new Point(marginleft, marginTop),
                new Point(marginleft, height + marginTop),
                new Point(marginleft + width, marginTop + height),
                // Arrow
                new Point(marginleft + width - arrowSize, marginTop + height - arrowSize),
                new Point(marginleft + width - arrowSize, marginTop + height + arrowSize),
                new Point(marginleft + width, marginTop + height)
             };

            g.DrawLines(myPen, points);
        }
    }

如果我附上DrawLShapeLine方法按钮单击事件,它绘制精细,但它并没有在形式的负载借鉴。

If I attach the DrawLShapeLine method to a button click event, it draws fine, but it does not draw on load of the form.

请咨询。

推荐答案

挂钩一个事件处理程序油漆事件的分组框并调用 DrawLShapeLine 从事件处理函数中。那么你应该使用在事件参数提供的图形目标:

Hook up an event handler for the Paint event of the GroupBox and call DrawLShapeLine from within that event handler instead. You should then use the Graphics object supplied by in event arguments:

private void groupBox1_Paint(object sender, PaintEventArgs e)
{
    DrawLShapeLine(e.Graphics, 10, 10, 20, 40);
}

随着code现在看起来它会尝试在画的分组框当表单需要画。该组框可以在其他任何场合,这将你画线消失,被涂。

As your code looks now it will attempt to paint in the GroupBox when the form requires painting. The group box may be painted at any other occasion, which will the line you paint disappear.

这篇关于图中的WinForms线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆