用GDI+划直线

下面是代码,很容易看懂的,没有必要进行解释了吧。:)

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Drawing;
using  System.Windows.Forms;


    
public   class  Line : System.Windows.Forms.UserControl
    
{
        
/// <summary> 
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Line()
        
{
            
// This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint 
| ControlStyles.UserPaint | ControlStyles.FixedHeight | ControlStyles.FixedWidth | ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Selectable, 
false);
            TabStop 
= false;
        }


        
/// <summary> 
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose(bool disposing)
        
{
            
if (disposing)
            
{
                
if (components != null)
                
{
                    components.Dispose();
                }

            }

            
base.Dispose(disposing);
        }


        
Component Designer generated code

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

            
int x1, 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 (Pen p = new Pen(m_FirstColor, 1))
            
{
                p.DashStyle 
= mDashStyle;
                e.Graphics.DrawLine(p, x1, y1, x2, y2);
            }

            
using (Pen p = new Pen(m_SecondColor, 1))
            
{
                p.DashStyle 
= mDashStyle;
                e.Graphics.DrawLine(p, x3, y3, x4, y4);
            }

        }


        
private System.Drawing.Drawing2D.DashStyle mDashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
        
public System.Drawing.Drawing2D.DashStyle DashStyle
        
{
            
get return mDashStyle; }
            
set { mDashStyle = value; Invalidate(); }
        }


        
private Color m_FirstColor = Color.FromKnownColor(KnownColor.ControlDark);
        
public Color FirstColor
        
{
            
get return m_FirstColor; }
            
set { m_FirstColor = value; Invalidate(); }
        }


        
private Color m_SecondColor = Color.FromKnownColor(KnownColor.ControlLightLight);
        
public Color SecondColor
        
{
            
get return m_SecondColor; }
            
set { m_SecondColor = value; Invalidate(); }
        }


        
private LineStyle m_LineStyle = LineStyle.Horizontal;
        
public LineStyle LineStyle
        
{
            
get return m_LineStyle; }
            
set
            
{
                m_LineStyle 
= value;
                Size 
= new Size(Height, Width);
                Invalidate();
            }

        }


        
protected override void OnSizeChanged(EventArgs e)
        
{
            
base.OnSizeChanged(e);

            ChangeControlSize();
        }


        
private void ChangeControlSize()
        
{
            
if (LineStyle == LineStyle.Horizontal)
                Height 
= 2;
            
else if (LineStyle == LineStyle.Vertical)
                Width 
= 2;
        }

    }


    
public   enum  LineStyle
    
{
        Horizontal 
= 1,
        Vertical 
= 2
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值