如何使用GDI+划直线?
下面是代码,很容易看懂的,没有必要进行解释了吧。:)
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Windows.Forms;
publicclassLine:System.Windows.Forms.UserControl
{
///<summary>
///Requireddesignervariable.
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
publicLine()
{
//ThiscallisrequiredbytheWindows.FormsFormDesigner.
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.FixedHeight|ControlStyles.FixedWidth|ControlStyles.ResizeRedraw,true);
SetStyle(ControlStyles.Selectable,false);
TabStop=false;
}
///<summary>
///Cleanupanyresourcesbeingused.
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
ComponentDesignergeneratedcode
protectedoverridevoidOnPaint(PaintEventArgse)
{
base.OnPaint(e);
intx1,x2,x3,x4,y1,y2,y3,y4;
if(LineStyle==LineStyle.Horizontal)
{
x1=0;
y1=0;
x2=ClientRectangle.Width;
y2=0;
x3=0;
y3=1;
x4=ClientRectangle.Width;
y4=1;
}
else//if(LineStyle==LineStyle.Vertical)
{
x1=0;
y1=0;
x2=0;
y2=ClientRectangle.Height;
x3=1;
y3=0;
x4=1;
y4=ClientRectangle.Height;
}
using(Penp=newPen(m_FirstColor,1))
{
p.DashStyle=mDashStyle;
e.Graphics.DrawLine(p,x1,y1,x2,y2);
}
using(Penp=newPen(m_SecondColor,1))
{
p.DashStyle=mDashStyle;
e.Graphics.DrawLine(p,x3,y3,x4,y4);
}
}
privateSystem.Drawing.Drawing2D.DashStylemDashStyle=System.Drawing.Drawing2D.DashStyle.Solid;
publicSystem.Drawing.Drawing2D.DashStyleDashStyle
{
get{returnmDashStyle;}
set{mDashStyle=value;Invalidate();}
}
privateColorm_FirstColor=Color.FromKnownColor(KnownColor.ControlDark);
publicColorFirstColor
{
get{returnm_FirstColor;}
set{m_FirstColor=value;Invalidate();}
}
privateColorm_SecondColor=Color.FromKnownColor(KnownColor.ControlLightLight);
publicColorSecondColor
{
get{returnm_SecondColor;}
set{m_SecondColor=value;Invalidate();}
}
privateLineStylem_LineStyle=LineStyle.Horizontal;
publicLineStyleLineStyle
{
get{returnm_LineStyle;}
set
{
m_LineStyle=value;
Size=newSize(Height,Width);
Invalidate();
}
}
protectedoverridevoidOnSizeChanged(EventArgse)
{
base.OnSizeChanged(e);
ChangeControlSize();
}
privatevoidChangeControlSize()
{
if(LineStyle==LineStyle.Horizontal)
Height=2;
elseif(LineStyle==LineStyle.Vertical)
Width=2;
}
}
publicenumLineStyle
{
Horizontal=1,
Vertical=2
}
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Drawing;
usingSystem.Windows.Forms;
publicclassLine:System.Windows.Forms.UserControl
{
///<summary>
///Requireddesignervariable.
///</summary>
privateSystem.ComponentModel.Containercomponents=null;
publicLine()
{
//ThiscallisrequiredbytheWindows.FormsFormDesigner.
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint|ControlStyles.FixedHeight|ControlStyles.FixedWidth|ControlStyles.ResizeRedraw,true);
SetStyle(ControlStyles.Selectable,false);
TabStop=false;
}
///<summary>
///Cleanupanyresourcesbeingused.
///</summary>
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
ComponentDesignergeneratedcode
protectedoverridevoidOnPaint(PaintEventArgse)
{
base.OnPaint(e);
intx1,x2,x3,x4,y1,y2,y3,y4;
if(LineStyle==LineStyle.Horizontal)
{
x1=0;
y1=0;
x2=ClientRectangle.Width;
y2=0;
x3=0;
y3=1;
x4=ClientRectangle.Width;
y4=1;
}
else//if(LineStyle==LineStyle.Vertical)
{
x1=0;
y1=0;
x2=0;
y2=ClientRectangle.Height;
x3=1;
y3=0;
x4=1;
y4=ClientRectangle.Height;
}
using(Penp=newPen(m_FirstColor,1))
{
p.DashStyle=mDashStyle;
e.Graphics.DrawLine(p,x1,y1,x2,y2);
}
using(Penp=newPen(m_SecondColor,1))
{
p.DashStyle=mDashStyle;
e.Graphics.DrawLine(p,x3,y3,x4,y4);
}
}
privateSystem.Drawing.Drawing2D.DashStylemDashStyle=System.Drawing.Drawing2D.DashStyle.Solid;
publicSystem.Drawing.Drawing2D.DashStyleDashStyle
{
get{returnmDashStyle;}
set{mDashStyle=value;Invalidate();}
}
privateColorm_FirstColor=Color.FromKnownColor(KnownColor.ControlDark);
publicColorFirstColor
{
get{returnm_FirstColor;}
set{m_FirstColor=value;Invalidate();}
}
privateColorm_SecondColor=Color.FromKnownColor(KnownColor.ControlLightLight);
publicColorSecondColor
{
get{returnm_SecondColor;}
set{m_SecondColor=value;Invalidate();}
}
privateLineStylem_LineStyle=LineStyle.Horizontal;
publicLineStyleLineStyle
{
get{returnm_LineStyle;}
set
{
m_LineStyle=value;
Size=newSize(Height,Width);
Invalidate();
}
}
protectedoverridevoidOnSizeChanged(EventArgse)
{
base.OnSizeChanged(e);
ChangeControlSize();
}
privatevoidChangeControlSize()
{
if(LineStyle==LineStyle.Horizontal)
Height=2;
elseif(LineStyle==LineStyle.Vertical)
Width=2;
}
}
publicenumLineStyle
{
Horizontal=1,
Vertical=2
}
本文地址:http://www.45fan.com/dnjc/73573.html