日期:2014-05-17  浏览次数:20864 次

如何实现按下鼠标画任意方向矩形,松开矩形框消失
本帖最后由 hjx595 于 2013-01-08 14:46:58 编辑
我想实现按下鼠标画任意方向矩形,松开鼠标矩形框消失,并选中框内的控件,然后按需对齐。类似VS那样。
private bool MouseIsDown = false;
        Rectangle MouseRect = Rectangle.Empty;
        Point start, end;
        Graphics g;
       private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            //MouseIsDown = false;    
            //MouseRect = Rectangle.Empty;

            MouseIsDown = false;
            g.Clear(Color.White);
            g.Dispose();
           
        }
       private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseIsDown)
            {
                Pen pen1 = new Pen(Color.White, 1);
                pen1.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                Pen pen2 = new Pen(Color.Black, 1);
                pen2.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                g.DrawRectangle(pen1, start.X, start.Y, Math.Abs(end.X - start.X), Math.Abs(end.Y - start.Y));
                end.X = e.X;
                end.Y = e.Y;
                g.DrawRectangle(pen2, start.X, start.Y, Math.Abs(end.X - start.X), Math.Abs(end.Y - start.Y));

                //end.X = e.X;
                //end.Y = e.Y;
                //Rectangle rect = this.RectangleToScreen(MouseRect);
                //ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);
                //MouseRect.Width = end.X - start.X;