在windows窗体C#中画线的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18527026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The best way to draw a line in windows form C#
提问by RealTao
I just want to know the best way to draw a line in windows form without using GDI+,but use control from toolbox.
我只想知道在不使用 GDI+ 的情况下以 Windows 窗体绘制线条的最佳方法,而是使用工具箱中的控件。
回答by Anuraj
You can use Visual Basic Power Pack Line Shape class - http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.lineshape.aspx
您可以使用 Visual Basic Power Pack Line Shape 类 - http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.lineshape.aspx
Another solution using Label control
使用 Label 控件的另一种解决方案
- Add a Label control to your form.
- Set Label Text to empty.
- Set BorderStyle to Fixed3D.
- Set AutoSize to false.
- Set Height to 2 (most easily accomplished by typing 2 into the Size/Height field under Label Properties).
- 向窗体添加一个 Label 控件。
- 将标签文本设置为空。
- 将 BorderStyle 设置为 Fixed3D。
- 将 AutoSize 设置为 false。
- 将高度设置为 2(通过在标签属性下的大小/高度字段中键入 2 最容易实现)。
Source : Draw horizontal divider in winforms
回答by Anuraj
Might not be the best choice but should help
可能不是最好的选择,但应该有帮助
Label lbl = new Label();
lbl.Text = "_________________________________";
this.Controls.Add(lbl);
回答by Amen Ayach
You can use my control:
您可以使用我的控件:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class AmenLine : Control
{
bool SzChInternaly;
public enum EDIR : int
{
Horizontal = 0,
Vertical = 1,
Slash = 2,
BKSlash = 3
}
public enum eStyle : int
{
rect = 0,
circ = 1
}
private eStyle _DrawStyle;
public eStyle DrawStyle {
get { return _DrawStyle; }
set {
_DrawStyle = value;
BuildMe(_Direc);
}
}
private EDIR _Direc = EDIR.Horizontal;
public EDIR Direction {
get { return _Direc; }
set {
if ((value == EDIR.Horizontal && _Direc == EDIR.Vertical) | (value == EDIR.Vertical && _Direc == EDIR.Horizontal)) {
SzChInternaly = true;
this.Size = new Size(this.Height, this.Width);
}
_Direc = value;
BuildMe(value);
}
}
private void BuildMe(EDIR BodyDir)
{
try {
SzChInternaly = true;
Drawing2D.GraphicsPath gr = new Drawing2D.GraphicsPath();
switch (BodyDir) {
case EDIR.Horizontal:
this.Size = new Size(this.Width, _BorderWidth + 10);
gr.AddRectangle(new Rectangle(0, (-_BorderWidth + this.Height) / 2, this.Width, _BorderWidth));
break;
case EDIR.Vertical:
this.Size = new Size(_BorderWidth + 10, this.Height);
gr.AddRectangle(new Rectangle((-_BorderWidth + this.Width) / 2, 0, _BorderWidth, this.Height));
break;
case EDIR.Slash:
try {
// CType(_BorderWidth * Height / Width, Integer)
for (i = 0; i <= Convert.ToInt32(Math.Pow(Math.Pow(Width, 2) + Math.Pow(Height, 2), 0.5)); i += _BorderWidth) {
switch (_DrawStyle) {
case eStyle.circ:
gr.AddPie(i, Convert.ToInt32(Height * i / Width), _BorderWidth, _BorderWidth, 0, 360);
break;
case eStyle.rect:
gr.AddRectangle(new Rectangle(i, Convert.ToInt32(Height * i / Width), _BorderWidth, _BorderWidth));
break;
}
}
} catch {
}
break;
case EDIR.BKSlash:
try {
// CType(_BorderWidth * Height / Width, Integer)
for (i = 0; i <= Convert.ToInt32(Math.Pow(Math.Pow(this.Height, 2) + Math.Pow(this.Width, 2), 0.5)); i += _BorderWidth) {
switch (_DrawStyle) {
case eStyle.circ:
gr.AddPie(Width - 1 - i, Convert.ToInt32(Height * i / Width), _BorderWidth, _BorderWidth, 0, 360);
break;
case eStyle.rect:
gr.AddRectangle(new Rectangle(Width - 1 - i, Convert.ToInt32(Height * i / Width), _BorderWidth, _BorderWidth));
break;
}
}
} catch {
}
break;
}
this.Region = new Region(gr);
SzChInternaly = false;
} catch {
}
}
private int _BorderWidth = 1;
public int BorderWidth {
get { return _BorderWidth; }
set {
_BorderWidth = value;
BuildMe(_Direc);
}
}
public AmenLine()
{
this.BackColor = Color.Black;
BuildMe(_Direc);
}
protected override void OnSizeChanged(System.EventArgs e)
{
base.OnSizeChanged(e);
if (SzChInternaly == false)
BuildMe(_Direc);
}
}
回答by Amen Ayach
You can draw line runtime by below code:
您可以通过以下代码绘制线条运行时:
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
e.Graphics.DrawLine(pen, 20, 10, 300, 100);
回答by iPaat
From: http://www.csharp-examples.net/separator-line/
来自:http: //www.csharp-examples.net/separator-line/
// separator bevel line
Label labelSeperator = new Label();
labelSeperator.AutoSize = false;
labelSeperator.Height = 2;
labelSeperator.BorderStyle = BorderStyle.Fixed3D;
this.Controls.Add(labelSeperator);