通过功能绘制自定义控件的行-不起作用? [英] Draw line of custom control by a function - not work?

查看:69
本文介绍了通过功能绘制自定义控件的行-不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,当我将该自定义控件拖到Form上时,我想在其客户端中画一条线.

我将其编码如下:

I have a custom control, i want to draw a line in its client when i drag that custom control to Form.

I code it as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        private float _LineWidth = 1.0F;
        public float LineWidth
        {
            get
            {
                return _LineWidth;
            }
            set
            {
                _LineWidth = value;
                PaintLine();
                Invalidate();
            }
        }
        private void PaintLine()
        {
            Graphics surface;
            surface = this.CreateGraphics();
            Pen pen1 = new Pen(Color.Blue, _LineWidth);
            surface.DrawLine(pen1, 10, 10, 100, 100);
        }
    }
}



但这不起作用,发生了什么事?希望每个人都能帮助我.谢谢.



But it doesnt work, what is happened? Hope everyone helps me. Thanks.

推荐答案

这里有几处错误.

首先,仅当您设置LineWidth属性时,才会执行LineWidth设置器-您似乎并没有这样做. _LineWidth的Settign值不会导致LineWidth设置器被调用.

其次,在Paint事件之外进行任何绘画是一个非常糟糕的主意-例如,如果调整控件的大小,它将不会被重画.

第三,如果您确实在Paint Event之外进行绘制,那么之后立即调用Invalidate更加愚蠢,因为这将导致Paint Event被触发,并清除控件中的绘制...
There are a couple of things wrong here.

Firstly, the LineWidth setter will only be actioned when you set the LineWidth Property - which you don''t seem to be doing. Settign teh value of _LineWidth will not cause the LineWidth setter to be called.

Second it is a very bad idea to do any painting outside the Paint Event - it will not get redrawn if the control is re-sized for example.

Thirdly, if you do paint outside the Paint Event, then calling Invalidate immediately afterwards is even sillier, as it will cause the Paint Event to be fired, and clear the drawing off your control...


有时我还会遇到类似的问题.
但是我已经解决了这个问题:

首先,将Paint事件处理程序添加到您的控件中(在构造函数中执行此操作):
Sometimes I get also problems like that.
But I''ve solved it to do this:

First, add a Paint event handler to your control (do this in the constructor):
this.Paint += UserControl1_Paint;


然后,添加UserControl1_Paint方法:


Then, add the UserControl1_Paint method:

void UserControl1_Paint(object sender, PaintEventArgs e)
{
    Pen pen1 = new Pen(Color.Blue, _LineWidth);
    e.Graphics.DrawLine(pen1, 10, 10, 100, 100);
}


如果这样做,则可以删除PaintLine方法.
另外,您不需要创建Graphics对象,只需使用e.Graphics.
如果更改了LineWidth属性,请将PaintLine();行更改为此:


If you do that, you can remove the PaintLine method.
Also, you don''t need to create a Graphics object, use e.Graphics.
If the LineWidth property is changed, change the PaintLine(); line into this:

this.Refresh();


然后将自动调用方法UserControl1_Paint.

希望这会有所帮助.


Then the method UserControl1_Paint will be invoked automatically.

Hope this helps.


这篇关于通过功能绘制自定义控件的行-不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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