6

how can i draw a zero opacity rubber band over a windows form with 0.3 opacity? (The rubber band is made after a Microsoft example


Update:

I need that rubber band to work something like a mask. If you use Jing or any other screen shot tool, you will see EXACTLY what I need to do when do you try to make a screenshot: the screen goes semi-opaque and when you make the selection, you will see the 0 opacity selection

3
  • 2
    Zero opacity means fully transparent. I think you mean opaque.
    – SLaks
    Jan 14, 2010 at 15:28
  • how will you be able to see it?
    – Matt
    Jan 14, 2010 at 15:29
  • By the way, that article is wrong. You can simply call ControlPaint.DrawReversibleFrame: msdn.microsoft.com/en-us/library/…. (Although it didn't exist in .Net 1.0)
    – SLaks
    Jan 14, 2010 at 15:30

4 Answers 4

8

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);
        }
    }
1
  • 1
    this was totally the droid i was looking for
    – Allen Rice
    Mar 27, 2010 at 16:00
1

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 );
1
  • sorry but that is not working. i don't know why but here is a screen shoot screencast.com/t/NjM1NjBkZTc . The grayed color is from my .4 opacity full screen form and the dashed rectangle is the DrawReversibleFrame method that completely ignores the backcolor parameter. Thank you and excuse my English.
    – andySF
    Jan 15, 2010 at 6:45
1

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

0

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)

1
  • If i will not success with anything this probably will be my solution to. Thank you
    – andySF
    Jan 15, 2010 at 6:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.