如何在Visual Studio C#2008中的Windows窗体应用程序上画线. [英] How to draw a line in visual studio C# 2008 on windows form application .

查看:520
本文介绍了如何在Visual Studio C#2008中的Windows窗体应用程序上画线.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Windows窗体应用程序的Visual Studio C#2008中画线!

我想用工具箱画一条线!
帮助我找到有关它的.dll,以添加我的工具箱,然后绘制!

How to draw a line in Visual Studio C# 2008 on Windows Form application !

I want draw a line with toolbox !
Help me to find .dll about it to add my toolbox then draw !

推荐答案

您不需要其他.dll. .NET框架免费提供画线功能.这是完成的方式:

在一条控件上画一条特殊线.

版本1
将方法附加到基础控件的Paint事件.
You don''t need an additional .dll. The ability to draw a line comes with .NET framework for free. Here is how it''s done:

Draw your special line onto one of your controls.

Version 1
Attach a method to the underlying control''s Paint event.
BaseControl.Paint += new PaintEventHandler(BaseControl_Paint);

该方法现在称为绘画事件处理程序.它具有类型为PaintEventArgs的参数,通常称为"e".
在您的绘画事件处理程序中,调用

The method now is called a paint event handler. It will have an argument of type PaintEventArgs, often called "e".
In your paint event handler, call

private void BaseControl_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLine(Pens.Orange, firstPoint, secondPoint);
}

,将从第一个点到第二个点画一条橙色线.点位于基础控件的坐标中,控件的左上角为(0,0).

版本2
子类化基础控件并覆盖OnPaint:

An orange line will be drawn from the first point to the second. Points are in co-ordinates of the underlying control with (0,0) in the control''s upper left corner.

Version 2
Subclass the underlying control and override OnPaint:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    e.Graphics.DrawLine(Pens.Orange, firstPoint, secondPoint);
}

重要的是,您必须先调用DrawLine() base.OnPaint(),然后才能在基础控件上进行绘制.否则,您的行将被绘制而无法看到.

It''s important that you call your DrawLine() after base.OnPaint() in order to draw on top of the underlying control. Otherwise your line would get painted over and never been seen.


个人而言,我将处理表单的Paint事件,并在此处绘制.
但是,如果必须在工具箱中绘制它:请查看 LineShape [ ^ ]控件从 Visual Basic Powerpacks [
Personally, I would handle the Paint event for the form, and draw it there.
But, if you must draw it in the toolbox: look at the LineShape[^] control from the Visual Basic Powerpacks[^]


您可以使用 Graphics.DrawLine [
You may draw a line, programmatically, using the Graphics.DrawLine[^] method.


这篇关于如何在Visual Studio C#2008中的Windows窗体应用程序上画线.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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