有一条“线”吗?在CSharp [英] Is there a "Line" in CSharp

查看:42
本文介绍了有一条“线”吗?在CSharp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB有一条线。控制,只是一个简单的行,让你分开

控件,而不会浪费Groupbox的空间。 CSharp是否放弃了这个?b / b
Dom

解决方案

4月14日,12:08 * pm,Dom< dolivas ... @ gmail.comwrote:


VB有一个行控制,只是一个简单的行,让你分开

控件,而不会浪费Groupbox的空间。 * CSharp是否下降

这个?


Dom



嗨Dom,


没有内置的C#线控制,但是,它是相当直接的 -

前锋:

只需进行用户控制即可覆盖OnPaint:


公共类Foo:UserControl

{

public Foo()

:base()

{


}


protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

使用(System.Drawing.Pen p = new

System.Drawing。笔(System.Drawing.Color.Red))

{

e.Graphics.DrawLine(p,0,0,200,200);

}

}

}

干杯,


格雷格


4月14日,12:08 * pm,Dom< dolivas ... @ gmail.comwrote:


VB有线控制,只是一个简单的行,让你分开

控件,而不会浪费Groupbox的空间。 * CSharp是否下降

这个?


Dom



这是一个更好的例子,如果你想要显示3DLine效果。


公共类Foo:UserControl

{


int mSpacing = 0;

Border3DStyle mBorderStyle =

(Border3DStyle)Border3DStyle.Etched;

private System.ComponentModel.IContainer components = null;

#region组件设计器生成的代码


///< summary>

/// Designer支持所需的方法 - 不要修改

///使用代码编辑器调用此方法的内容。

///< / summary>

private void InitializeComponent()

{

this.SuspendLayout();

//

//行

/ /

this.AutoScaleDimensions = new System.Drawing.SizeF(6F,

13F);

this.AutoScaleMode =

System.Windows.Forms.AutoScaleMode.Font;

this.Name =" Line " ;;

this.Size = new System.Drawing.Size(150,16);

this.ResumeLayout(false);


}


///< summary>

///清理正在使用的所有资源。

///< / summary>

///< param name =" disposing">如果托管资源应该处理
,则为true;否则,false。< / param>

protected override void Dispose(bool disposing)

{

if(disposing&&( components!= null))

{

components.Dispose();

}

base.Dispose(处理);

}


#endregion


public Foo()

{$ / $

InitializeComponent();


SetStyle(ControlStyles.DoubleBuffer,true);

SetStyle(ControlStyles。 SupportsTransparentBackColo r,

true);

SetStyle(ControlStyles.AllPaintingInWmPaint,true);

SetStyle(ControlStyles.ResizeRedraw,true);


Name =" 3DLine";

this.Size = new System.Drawing.Size(150,16);

UpdateStyles ();

}


公共覆盖字符串文本

{

get {return base。文字;}

set {base.Text = value; }

}


公共字符串HeaderText

{

get {return Text; }

set {Text = value;无效(); }

}


公共Border3DStyle LineStyle

{

get {return mBorderStyle; }

set

{

if(value!= mBorderStyle){mBorderStyle = value;

Invalidate() ; }

}

}


public int Spacing

{

得到{return mSpacing; }

set

{

if(value!= mSpacing){mSpacing = value;

Invalidate() ; }

}

}


protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);


使用(刷子b =新的SolidBrush(ForeColor))

{

StringFormat sf = StringFormat.GenericTypographic;

SizeF s = e.Graphics.MeasureString(Text,Font,Width);

e.Graphics.DrawString(Text,字体,b,0,(高度/ 2) -

(s.Height / 2),sf);

if(s.Width + Spacing< Width)

{

点p =新点(Convert.ToInt32(s.Width +

间距),Convert.ToInt32(s.Height / 2));

ControlPaint.DrawBorder3D(e.Graphics,pX,

(高度/ 2),(宽度 - pX),5,mBorderStyle,Border3DSide.Top) ;

}

}


}

}


4月14日,5:37 * pm,Greg< gcad ... @ gmail.comwrote:


4月14日,12日: 08 * pm,Dom< dolivas ... @ gmail.comwrote:

VB有一个行控制,只是一个简单的行,让你分开

控件,而不会浪费Groupbox的空间。 * CSharp是否下降了

这个?


Dom



如果你想展示3DLine效果,这是一个更好的例子。


* *公共类Foo:UserControl

* * {


* * * * int mSpacing = 0;

* * * * Border3DStyle mBorderStyle =

(Border3DStyle)Border3DStyle.Etched;

* * * * private System.ComponentModel.IContainer components = null;


* * * * #region组件设计器生成的代码


* * * * ///< summary>

* * * * ///支持Designer的必要方法 - 不要使用代码编辑器修改

* * * * ///此方法的内容。 />
* * * * ///< / summary>

* * * * private void InitializeComponent()

* * * * {

* * * * * * this.SuspendLayout();

* * * * * * //

* * * * * * //线

* * * * * * //

* * * * * * this.AutoScaleDimensions = new System.Drawing.SizeF(6F,

13F);

* * * * * * this.AutoScaleMode =

System.Windows。 Forms.AutoScaleMode.Font;

* * * * * * this.Name =" Line";

* * * * * * this.Size = new System。 Drawing.Size(150,16);

* * * * * * this.ResumeLayout(false);


* * * *}


* * * * ///< summary>

* * * * ///清理正在使用的所有资源。

* * * * ///< / summary>

* * * * ///< param name =" disposing">如果托管资源应为

被处置;否则,false。< / param>

* * * * protected override void Dispose(bool disposing)

* * * * {

* * * * * * if(处理&&(components!= null))

* * * * * * {

* * * * * * * * components.Dispose();

* * * * * *}

* * * * * * base.Dispose(disposing);

* * * *}


* * * * #endregion


* * * * public Foo()

* * * * {


* * * * * * InitializeComponent();


* * * * * * SetStyle(ControlStyles .DoubleBuffer,true);

* * * * * * SetStyle(ControlStyles.SupportsTransparentBackColo r,

true);

* * * * * * SetStyle(ControlStyles.AllPaintingInWmPaint,true);

* * * * * * SetStyle(ControlStyles.ResizeRedraw,true);


* * * * * * Name =" 3DLine";

* * * * * * this.Size = new System.Drawing.Size(150,16);

* * * * * * UpdateStyles();

* * * *}


* * * * public override string Text

* * * * {

* * * * * * get {return base.Text;}

* * * * * * set {base.Text = value; }

* * * *}

* * * * public string HeaderText

* * * * {

* * * * * * get {return Text; }

* * * * * * set {Text = value;无效(); }

* * * *}

* * * * public Border3DStyle LineStyle

* * * * {

* * * * * * get {return mBorderStyle; }

* * * * * *设定

* * * * * * $

* * * * * * * * if(value != mBorderStyle){mBorderStyle = value;

Invalidate(); }

* * * * * *}

* * * *}


* * * * public int Spacing

* * * * {

* * * * * * get {return mSpacing; }

* * * * * *设定

* * * * * * $

* * * * * * * * if(value != mSpacing){mSpacing = value;

Invalidate(); }

* * * * * *}

* * * *}


* * * * protected override void OnPaint( PaintEventArgs e)

* * * * {

* * * * * * base.OnPaint(e);


* * * * * *使用(画笔b =新的SolidBrush(ForeColor))

* * * * * * {

* * * * * * * * StringFormat sf = StringFormat.GenericTypographic;

* * * * * * * * SizeF s = e.Graphics.MeasureString(Text,Font,Width);

* * * * * * * * e.Graphics.DrawString(文字,字体,b,0,(高度/ 2) -

(s.Height / 2),sf);

* * * * * * * * if(s.Width + Spacing< Width)

* * * * * * * * {

* * * * * * * * * * Point p = new Point(Convert.ToInt32(s.Width +

Spacing),Convert.ToInt32(s.Height / 2));

* * * * * * * * * * ControlPaint.DrawBorder3D(e.Graphics,pX,

(高度/ 2),(宽度 - pX),5,mBorderStyle,Border3DSide.Top);

* * * * * * * *}

* * * * * *}


* * * *}

* *}



哇。这是很多工作。这就是我最终做的事情。我使用了一个

GroupBox没有标题,高度为5.


Dom


VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox. Did CSharp drop
this?

Dom

解决方案

On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:

VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox. *Did CSharp drop
this?

Dom

Hi Dom,

There is no built-in C# line control, however, it''s fairly straight-
forward:
Just make a usercontrol and override the OnPaint:

public class Foo : UserControl
{
public Foo()
: base()
{

}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (System.Drawing.Pen p = new
System.Drawing.Pen(System.Drawing.Color.Red))
{
e.Graphics.DrawLine(p, 0, 0, 200, 200);
}
}
}
Cheers,

Greg


On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:

VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox. *Did CSharp drop
this?

Dom

Here is a better example if you want to show a 3DLine effect.

public class Foo : UserControl
{

int mSpacing = 0;
Border3DStyle mBorderStyle =
(Border3DStyle)Border3DStyle.Etched;
private System.ComponentModel.IContainer components = null;
#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Line
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.Name = "Line";
this.Size = new System.Drawing.Size(150, 16);
this.ResumeLayout(false);

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

public Foo()
{

InitializeComponent();

SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColo r,
true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);

Name = "3DLine";
this.Size = new System.Drawing.Size(150, 16);
UpdateStyles();
}

public override string Text
{
get{ return base.Text;}
set { base.Text = value; }
}

public string HeaderText
{
get { return Text; }
set { Text = value; Invalidate(); }
}

public Border3DStyle LineStyle
{
get { return mBorderStyle; }
set
{
if (value != mBorderStyle) { mBorderStyle = value;
Invalidate(); }
}
}

public int Spacing
{
get { return mSpacing; }
set
{
if (value != mSpacing) { mSpacing = value;
Invalidate(); }
}
}

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

using (Brush b = new SolidBrush(ForeColor))
{
StringFormat sf = StringFormat.GenericTypographic;
SizeF s = e.Graphics.MeasureString(Text, Font, Width);
e.Graphics.DrawString(Text, Font, b, 0, (Height / 2) -
(s.Height / 2), sf);
if (s.Width + Spacing < Width)
{
Point p = new Point(Convert.ToInt32(s.Width +
Spacing), Convert.ToInt32(s.Height / 2));
ControlPaint.DrawBorder3D(e.Graphics, p.X,
(Height / 2), (Width - p.X), 5, mBorderStyle, Border3DSide.Top);
}
}

}
}


On Apr 14, 5:37*pm, Greg <gcad...@gmail.comwrote:

On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:

VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox. *Did CSharp drop
this?

Dom


Here is a better example if you want to show a 3DLine effect.

* * public class Foo : UserControl
* * {

* * * * int mSpacing = 0;
* * * * Border3DStyle mBorderStyle =
(Border3DStyle)Border3DStyle.Etched;
* * * * private System.ComponentModel.IContainer components = null;

* * * * #region Component Designer generated code

* * * * /// <summary>
* * * * /// Required method for Designer support - do not modify
* * * * /// the contents of this method with the code editor.
* * * * /// </summary>
* * * * private void InitializeComponent()
* * * * {
* * * * * * this.SuspendLayout();
* * * * * * //
* * * * * * // Line
* * * * * * //
* * * * * * this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
* * * * * * this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
* * * * * * this.Name = "Line";
* * * * * * this.Size = new System.Drawing.Size(150, 16);
* * * * * * this.ResumeLayout(false);

* * * * }

* * * * /// <summary>
* * * * /// Clean up any resources being used.
* * * * /// </summary>
* * * * /// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
* * * * protected override void Dispose(bool disposing)
* * * * {
* * * * * * if (disposing && (components != null))
* * * * * * {
* * * * * * * * components.Dispose();
* * * * * * }
* * * * * * base.Dispose(disposing);
* * * * }

* * * * #endregion

* * * * public Foo()
* * * * {

* * * * * * InitializeComponent();

* * * * * * SetStyle(ControlStyles.DoubleBuffer, true);
* * * * * * SetStyle(ControlStyles.SupportsTransparentBackColo r,
true);
* * * * * * SetStyle(ControlStyles.AllPaintingInWmPaint, true);
* * * * * * SetStyle(ControlStyles.ResizeRedraw, true);

* * * * * * Name = "3DLine";
* * * * * * this.Size = new System.Drawing.Size(150, 16);
* * * * * * UpdateStyles();
* * * * }

* * * * public override string Text
* * * * {
* * * * * * get{ return base.Text;}
* * * * * * set { base.Text = value; }
* * * * }

* * * * public string HeaderText
* * * * {
* * * * * * get { return Text; }
* * * * * * set { Text = value; Invalidate(); }
* * * * }

* * * * public Border3DStyle LineStyle
* * * * {
* * * * * * get { return mBorderStyle; }
* * * * * * set
* * * * * * {
* * * * * * * * if (value != mBorderStyle) { mBorderStyle = value;
Invalidate(); }
* * * * * * }
* * * * }

* * * * public int Spacing
* * * * {
* * * * * * get { return mSpacing; }
* * * * * * set
* * * * * * {
* * * * * * * * if (value != mSpacing) { mSpacing = value;
Invalidate(); }
* * * * * * }
* * * * }

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

* * * * * * using (Brush b = new SolidBrush(ForeColor))
* * * * * * {
* * * * * * * * StringFormat sf = StringFormat.GenericTypographic;
* * * * * * * * SizeF s = e.Graphics.MeasureString(Text,Font, Width);
* * * * * * * * e.Graphics.DrawString(Text, Font, b, 0, (Height / 2) -
(s.Height / 2), sf);
* * * * * * * * if (s.Width + Spacing < Width)
* * * * * * * * {
* * * * * * * * * * Point p = new Point(Convert.ToInt32(s.Width +
Spacing), Convert.ToInt32(s.Height / 2));
* * * * * * * * * * ControlPaint.DrawBorder3D(e.Graphics, p.X,
(Height / 2), (Width - p.X), 5, mBorderStyle, Border3DSide.Top);
* * * * * * * * }
* * * * * * }

* * * * }
* * }

Wow. That''s a lot of work. Here is what I ended up doing. I used a
GroupBox with no title, and a height of 5.

Dom


这篇关于有一条“线”吗?在CSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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