winform中 在panel控件中按下鼠标左键后拖动鼠标绘出虚线矩形,松开鼠标键后虚线矩形隐藏

解决方案 »

  1.   

    在 MouseDown 和 MouseUp 事件中处理,通过 e.X 和 e.Y 得到座标,进行画线处理。
      

  2.   

    参考下: private Point m_DownPoint;
    private Point m_CurPoint;
    protected override void OnMouseDown(MouseEventArgs e)
    {
    base.OnMouseDown(e);
    this.m_DownPoint = this.PointToScreen(e.Location);
    this.m_CurPoint = this.PointToScreen(e.Location);
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
    base.OnMouseMove(e);
    if (this.Capture)
    {
    Point curPoint = this.PointToScreen(e.Location);
    ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, this.m_CurPoint.X, this.m_CurPoint.Y), Color.Yellow, FrameStyle.Dashed);
    ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, curPoint.X, curPoint.Y), Color.Yellow, FrameStyle.Dashed);
    this.m_CurPoint = curPoint;
    }
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
    base.OnMouseUp(e);
    ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, this.m_CurPoint.X, this.m_CurPoint.Y), Color.Yellow, FrameStyle.Dashed);
    }
    protected override void OnDeactivate(EventArgs e)
    {
    base.OnDeactivate(e);
    ControlPaint.DrawReversibleFrame(Rectangle.FromLTRB(this.m_DownPoint.X, this.m_DownPoint.Y, this.m_CurPoint.X, this.m_CurPoint.Y), Color.Yellow, FrameStyle.Dashed);
    }
      

  3.   

    http://blog.csdn.net/zgke/archive/2008/12/12/3502920.aspx
      

  4.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication27
    {
        public partial class Form1 : Form
        {
            Point StartPoint;        public Form1()
            {
                InitializeComponent();
                this.MouseMove += new MouseEventHandler(Form1_MouseMove);
                this.MouseUp+=new MouseEventHandler(Form1_MouseUp);
            }
            void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left)
                    StartPoint = e.Location;
                else using (Graphics G = this.CreateGraphics())
                    {
                        Point Start = StartPoint;
                        Point End = e.Location;
                        int Width = e.X - Start.X;
                        int Height = e.Y - Start.Y;                    if (Width < 0)
                        {
                            Start.X += Width;
                            End.X -= Width;
                        }
                        if (Height < 0)
                        {
                            Start.Y += Height;
                            End.Y -= Height;
                        }                    G.Clear(this.BackColor);
                    
                     
                        ControlPaint.DrawFocusRectangle(G, new Rectangle(Start, new Size(Math.Abs(Width), Math.Abs(Height))));
                    }
            }
            void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                   
            }
        }    
    }