filled rubber band in Winforms Application

自闭症网瘾萝莉.ら 提交于 2019-11-28 10:20:42

Is this the droid you were looking for?

    public Form1()
    {
        InitializeComponent();
        DoubleBuffered = true;
    }

    bool mouseDown = false;
    Point mouseDownPoint = Point.Empty;
    Point mousePoint = Point.Empty;

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        mouseDown = true;
        mousePoint = mouseDownPoint = e.Location;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        mouseDown = false;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        mousePoint = e.Location;
        Invalidate();
    }

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

        if (mouseDown)
        {
            Region r = new Region(this.ClientRectangle);
            Rectangle window = new Rectangle(
                Math.Min(mouseDownPoint.X, mousePoint.X),
                Math.Min(mouseDownPoint.Y, mousePoint.Y),
                Math.Abs(mouseDownPoint.X - mousePoint.X),
                Math.Abs(mouseDownPoint.Y - mousePoint.Y));
            r.Xor(window);
            e.Graphics.FillRegion(Brushes.Red, r);
            Console.WriteLine("Painted: " + window);
        }
    }

You need to use a partially opaque color when drawing:

Updated line from linked article, in the MyDrawReversibleRectangle method:

ControlPaint.DrawReversibleFrame( rc, Color.FromArgb(80, 120, 120, 120), FrameStyle.Dashed );

I used the code that @Dearmash supplied in the screen capture utility that comes with my open source app BugTracker.NET. The app isn't very big, so if you are doing screen capture stuff, it might be a good starting point. More info here:

http://ifdefined.com/blog/post/Screen-capture-utility-in-C-NET.aspx

Just use an additional form, without showing it in the taskbar, or other Form-options. Set the region of the form that it only shows the rubber band. And make sure both windows behave as if it was one window (moving, closing,...). I know it's not an elegant way, but with a little work it can produce good results. You can make sure that the form is on top in the form-hierarchy and still doen't receive focus.

By setting the region good, all events will go to the other form.

That's the way I worked out an equivalent problem (I don't say it's a good solution, but it works)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!