How to to draw the outline of a rectangle in a window as I drag the mouse (edited)

Hello,

I would like to draw the outline of a rectangle in a window as I drag the mouse but at mouse-up I want that rectangle to disappear.

How can I do that?

Thanks.

Lennox.

I have this in the MouseDrag event of the window…

These are all properties of the window
MousedownXD as integer
MousedownXU as integer
MousedownYD as integer
MousedownYU as integer
MousePositionX as integer
MousePositionY as integer

Graphics.ForeColor=&cff0000
'Graphics.PenHeight=5
MousePositionX = X
MousePositionY = Y

Graphics.DrawRect(MousedownXD, MousedownYD, MousePositionX-MousedownXD, MousePositionY-MousedownYD)

But as I drag the mouse the array of rectangles are are drawn from mousedown to the position of the mouse.

Lennox

Something like a cropping/select tool I am looking for.

Lennox

May want to use 4 line controls to do this with… so that you don’t have to remember the pixel information under the previous lines you have overdrawn to fill back in before you draw a new ‘box’. That would by my suggestion.

Could you give some more details… a bit of code would be appreciated.
Have no idea about the “4 line controls”

Maybe you can try to use a canvas with useFocusRing, Transparent, AcceptFocus=true to emulate a rectangle with no filling.

This is for selecting an area from a window so that I can then use Drawinto and create a .jpg image of that selected area.
Something like using Command-Shift-4 on a Mac

I tried this, I do not know if this is what you meant…

Graphics.DrawLine ( MousedownXD, MousedownYD, MousePositionX, MousedownYD )
Graphics.DrawLine ( MousedownXD, MousedownYD, MousedownXD, MousePositionY )
Graphics.DrawLine ( MousePositionX, MousedownYD, MousePositionX, MousePositionY )
Graphics.DrawLine ( MousedownXD, MousePositionY, MousePositionX, MousePositionY )

but it does the same as
Graphics.DrawRect(MousedownXD, MousedownYD, MousePositionX-MousedownXD, MousePositionY-MousedownYD)

Lennox

OK I got the second part - make it disappear at mouse-up…
Self.Refresh in the Mouse-up event did that.

But I cannot get rid of all those rectangles as the mouse moves.

Lennox

No, actually use a line… or I think Xojo calls it a ‘separator’ control. Use 4 of them to draw a box, referencing the starting position and the current mouse position. If I have time, I’ll try to throw a sample of source your way soon.

Alright, well… I tried to throw something together really quick using the ‘line’ and ‘separator’ controls. Both erase anything on the screen as the box is being ‘built’ over it. Refreshing during the dragging causes tremendous flicker. I know it’s frustrating, but such a seemingly simple task is going to be extremely difficult to do in Windows and have the outcome you’re looking for.

Hi Eric,

I can use this to draw Lines, how can I modify that for Separators?

Thanks.

Lennox

Why don’t you just overlay a canvas? Draw the rectangle in the canvas’ paint event and handle showing and resizing of the canvas in its parent’s MouseDown, MouseDrag and MouseUp event handler.

For a quick & dirty test project see here: http://osswald.com/xojo/myFrame.zip

When I did this, I eliminated the flicker by drawing offscreen. The code is much the same, but you start with a couple of in memory pictures: one containing the way the canvas/dialog looked before you start. (In my case , a picture)
So , record the start position.
Track the new mouse position
When it is different from the previous x,y co-ords (only if different!)

Copy the starting picture to picture 2
Draw a rectangle on picture 2
Invalidate the canvas/ dialog.
In the paint event, paint the contents of picture2 if it is not nil

So at no time do you have 2 rectangles on picture2
and when it goes to nil, normal screen updates resume without the rectangles.

I am intrigued to know why this post is in the French channel

That was an error, only noticed after it was posted, and I did not know how to correct that.
Lennox

[quote=25016:@Jeff Tullin]When I did this, I eliminated the flicker by drawing offscreen. The code is much the same, but you start with a couple of in memory pictures: one containing the way the canvas/dialog looked before you start. (In my case , a picture)
So , record the start position.
Track the new mouse position
When it is different from the previous x,y co-ords (only if different!)

Copy the starting picture to picture 2
Draw a rectangle on picture 2
Invalidate the canvas/ dialog.
In the paint event, paint the contents of picture2 if it is not nil

So at no time do you have 2 rectangles on picture2
and when it goes to nil, normal screen updates resume without the rectangles.[/quote]

Hi Jeff,
Do you have an example project to demonstrate this? That would be great, or if you don’t mind I can send you my project.
Thanks.
Lennox

Check this. Maybe help you to create your solution. This demo gets the rectangle of a portion of the canvas selected by mouse.

http://tbf.me/a/BAiv0r

Thanks Rick,
Works great, exactly what I wanted to do.
Thanks again.
Lennox

Hi Rick

Can you re-post the demo project you shared 3 years ago. I’m looking for similar stuff.

Thanks & Regards
Divya