我在MouseMove事件中Graphics graphics = this.CreateGraphics();
                Pen myPen = new Pen(Color.Red, 2);
                graphics.DrawRectangle(myPen, ptStart.X, ptStart.Y, e.X - ptStart.X, e.Y - ptStart.Y);
使用画矩形框,一点反应也没有,请问怎么实现我的目的呢?private void pb_InqImagePart_MouseDown(object sender, MouseEventArgs e)
        {
            pictureOperation = PICTURE_OPERATION.START;
            ptStart          = new Point(e.X, e.Y);
        }        private void pb_InqImagePart_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureOperation == PICTURE_OPERATION.START)
            {
                pictureOperation = PICTURE_OPERATION.WORKING;
                Graphics graphics = this.CreateGraphics();
                Pen myPen = new Pen(Color.Red, 2);
                graphics.DrawRectangle(myPen, ptStart.X, ptStart.Y, e.X - ptStart.X, e.Y - ptStart.Y);
            }           
        }        private void pb_InqImagePart_MouseUp(object sender, MouseEventArgs e)
        {
            if (pictureOperation == PICTURE_OPERATION.WORKING)
            {
                ptEnd = new Point(e.X, e.Y);
                RectangleF rect = new RectangleF(ptStart.X / pb_InqImagePart.Width, ptStart.Y / pb_InqImagePart.Height,
                                                (ptEnd.X - ptStart.X) / pb_InqImagePart.Width,
                                                (ptEnd.Y - ptStart.Y) / pb_InqImagePart.Height);
                if (selectInquireNode!=null)
                {
                    selectInquireNode.CloneImage(rect, System.Drawing.Imaging.PixelFormat.DontCare);
                }
                pictureOperation = PICTURE_OPERATION.IDLE;
            }
        }

解决方案 »

  1.   


    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 ERP
    {
        public partial class Form13 : Form
        {
            public Form13()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
              
            }
            Point start = new Point();
            bool state = false;
            private void Form13_MouseDown(object sender, MouseEventArgs e)
            {
                start.X = e.X;
                start.Y = e.Y;
                state = true;
            }        private void Form13_MouseMove(object sender, MouseEventArgs e)
            {
                if (state)
                {
                  
                    Graphics gs = this.CreateGraphics();
                    gs.Clear(Color.Red);
                    Rectangle Rect = new Rectangle();
                    Rect.X = start.X;
                    Rect.Y = start.Y;
                    Rect.Height = e.Y - start.Y;
                    Rect.Width = e.X - start.X;
                    gs.DrawRectangle(Pens.Purple, Rect);
                   
                }
            }        private void Form13_MouseUp(object sender, MouseEventArgs e)
            {
                state = false;
            }
        }
    }
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication236
    {
        public partial class Form1 : Form
        {
            Point StartPoint;        public Form1()
            {
                InitializeComponent();            this.MouseMove += new MouseEventHandler(Form1_MouseMove);         }        void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button != MouseButtons.Left)
                    StartPoint = e.Location;
                else using (Graphics G = this.CreateGraphics())
                    {
                        G.Clear(this.BackColor);  
                        G.DrawRectangle(Pens.Red, new Rectangle(StartPoint, new Size(e.X - StartPoint.X, e.Y - StartPoint.Y)));
                    }
            }
        }
    }
      

  3.   

    支持任何方向的拉伸using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication236
    {
        public partial class Form1 : Form
        {
            Point StartPoint;        public Form1()
            {
                InitializeComponent();            this.MouseMove += new MouseEventHandler(Form1_MouseMove);        }        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);                    G.DrawRectangle(Pens.Red, new Rectangle(Start, new Size(Math.Abs(Width), Math.Abs(Height))));
                    }
            }
        }
    }