Results 1 to 2 of 2

Thread: Selection box on a picture

  1. #1

    Thread Starter
    Join Date
    Aug 1999
    Anyone have some code that will draw an expanding (or shrinking) rectangle on a form or picture, after I mousedown, move over a selected area, and then mouseup.

    Mousemove fills in my rectangle for each increement in X and Y, and is too messy.

    Using mousedown and moseup alone, I can draw the box at mouseup, but want to draw a rectangle according to the mouse movement after mousedown. I would also like the rectangle's lines to be dashed or even colored.


  2. #2

    Not that hard...


    Well, here is you code. This will allow you to draw a rectangle with a Click-and-drag method

    Dim Clicked as Boolean
    Dim first_corner_x as Single
    Dim first_corner_y as Single
    Dim last_pos_x as Single
    Dim last_pos_y as single
    Dim drawn as Boolean
    Private Sub MouseDown_click(Index as integer, X as Single....)
         Clicked = true 'Records the fact that the button is pressed
         first_corner_x = X 'saves the x position of the cursor
         first_corner_y = Y 'saves the y position of the cursor
         drawn = false 'saves the fact that there is no rectangle drawn yet, so none to erase when the mouse will move
    end sub
    Private MouseMove_click(Index as integer,....)
        If Clicked then 'verify the button is pressed
            If drawn then
                 Line (first_corner_x,first_corner_y)-(last_pos_x,last_pos_y),,B 'erases the previous rectangle
            end if
            If X <> first_corner_x or Y <> first_corner_y then
                 Line (first_corner_x,first_corner_y)-(x,y),,B 'draws a new one
                 'save the new coordinates of the rectangle
                 'in order to erase it on the next mousemove
                 last_pos_x = X
                 last_pos_y = y
                 drawn = true 'saves the fact that a rectangle is now drawn and will need to be erased on the next mousemove
            end if
        End if
    end sub
    Private Sub MouseUp_click(Index as integer' X as Single....)
         Clicked = False
    end sub

    The drawmode property set to XOR pen will allow the line to invert colors on every pixel which is not the background color. This way you can simply draw the same object over an exiting one to erase it without loosing the rest of the drawn elements.

    That should do it.

    Good luck!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width