MFC里面要用到SetROP2,但是C#里面没有。实现类似橡皮线的效果。具体来说,如何实现,随着鼠标的移动画矩形线(及时删除原来的矩形框),松开鼠标时显示最后状态下的矩形框?

解决方案 »

  1.   

    GDI+的话,没有这个功能如果要用,就得通过API调用GDI接口
      

  2.   

    不知道你要怎么画  最简单的方法 ControlPaint.DrawFocusRectangle 
    你可以参考 这个是画一次记录下 矩形 下次画的时候在绘制一个透明的矩形
    http://blog.csdn.net/zgke/archive/2009/03/04/3956936.aspx
      

  3.   

    这是个DrawReversibleFrame的例子
    public partial class Form1 : Form
    {
        Point startPoint = new Point(-1, -1);
        Rectangle lastRect = Rectangle.Empty;
        List<Rectangle> rectangles = new List<Rectangle>();    public Form1()
        {
            InitializeComponent();
        }    protected override void OnMouseDown(MouseEventArgs e)
        {
            this.startPoint = this.PointToScreen(e.Location);
        }    protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.startPoint.X >= 0)
            {
                Point current = this.PointToScreen(e.Location);
                Size size = new Size(current.X - startPoint.X, current.Y - startPoint.Y);            ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);  //擦旧
                lastRect = new Rectangle(startPoint, size);
                ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);  //画新
            }
        }    protected override void OnMouseUp(MouseEventArgs e)
        {
            if (this.startPoint.X >= 0)
            {
                ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);  //擦
                this.startPoint.X = -1;            this.rectangles.Add(Normalize(this.RectangleToClient(lastRect)));
                this.lastRect = Rectangle.Empty;
                this.Invalidate();
            }
        }    protected override void OnPaint(PaintEventArgs e)
        {
            foreach (Rectangle rect in this.rectangles)
            {
                e.Graphics.DrawRectangle(Pens.Black, rect);
            }
        }    private Rectangle Normalize(Rectangle rect)
        {
            if (rect.Width < 0) { rect.X += rect.Width; rect.Width = -rect.Width; }
            if (rect.Height < 0) { rect.Y += rect.Height; rect.Height = -rect.Height; }
            return rect;
        }
    }
      

  4.   

    4楼的方法很好 ,
    不过这个简易画板也很不错,有画直线,画椭圆,画矩形,等等 
    http://download.csdn.net/source/1149352