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

  1. ‹ Older
  2. 6 years ago

    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

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

    Lennox

  4. Eric B

    3 Aug 2013 Arlington, Texas

    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.

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

  6. Rick A

    3 Aug 2013 Pre-Release Testers (Brazil. UTC-3:00)
    Edited 6 years ago

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

  7. 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

  8. 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

  9. 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

  10. Eric B

    3 Aug 2013 Arlington, Texas
    Edited 6 years ago

    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.

  11. Eric B

    3 Aug 2013 Arlington, Texas
    Edited 6 years ago

    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.

  12. Hi Eric,

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

    Thanks.

    Lennox

  13. Oliver O

    4 Aug 2013 Pre-Release Testers, Xojo Pro https://udemy.seminar.pro

    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

  14. Jeff T

    4 Aug 2013 Pre-Release Testers Midlands of England, Europe

    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.

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

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

  17. @Jeff T 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.

    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

  18. Rick A

    5 Aug 2013 Pre-Release Testers (Brazil. UTC-3:00)

    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

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

  20. 3 years ago

    Hi Rick

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

    Thanks & Regards
    Divya

  21. 2 weeks ago

or Sign Up to reply!