Here is my rubber banding program.
Hope this helps.
Matthew Bastian
National Research Council of Canada
pro RubberBand, LeftHandSide, RightHandSide, Bottom, Top
;
; this program will draw a rubber band box that follows the cursor
; the first corner starts when the left mouse button is pressed
; and the other corner ends when the mouse button is released
;
!err = 0
LeftButtonPressed = 1
LoopCount = 0L
;
; set the clip rectangle to it's maximum limits in order for "zoom" to work
;
!p.clip = [0,0,!d.x_size,!d.y_size,0,0]
;
; keep looping until the user presses the left mouse button
;
while !err ne LeftButtonPressed do begin
;
; every 100 times through this loop print the usage message
;
if LoopCount mod 100L eq 0 then begin
print, 'Press left mouse button to activate rubber band box'
LoopCount = 0
endif
LoopCount = LoopCount + 1
;
; get the current mouse location in pixels relative to lower lhs corner
; note that this function return immediatly without waiting for a mouse
; button to be pressed
;
cursor, x1, y1, /nowait, /data
endwhile
print, 'Release left mouse button to de-activate rubber band box'
;
; keep looping and drawing the rubber band box until the button is released
;
!linetype = 0
x2 = x1
y2 = y1
x = -1
y = -1
device, set_graphics=6, get_graphics=oldg
oplot,[x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1]; /device
while (!err eq LeftButtonPressed) or ((x eq -1) and (y eq -1)) do begin
cursor, x, y, /nowait, /data
if (x ne x2) or (y ne y2) then begin
oplot,[x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1]
x2 = x
y2 = y
oplot,[x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1]
endif
endwhile
oplot,[x1, x1, x2, x2, x1], [y1, y2, y2, y1, y1]
device, set_graphics=oldg
;
; set up the return values
; converting from screen coordinates to world coordinates
;
if x1 lt x2 then begin
LeftHandSide = float (x1)
RightHandSide = float (x2)
endif else begin
RightHandSide = float (x1)
LeftHandSide = float (x2)
endelse
if y1 lt y2 then begin
Bottom = float (y1)
Top = float (y2)
endif else begin
Top = float (y1)
Bottom = float (y2)
endelse
return
end
|