Needs to adjust coordinates. For example, if second click is at left from first click, rectangle width would be negative.
type OSDCOLORANDBORDER color border x y x2 y2
OSDCOLORANDBORDER a
a.color=ColorFromRGB(255 0 0)
a.border=2
;get current mouse coords, wait for click, get mouse coords
POINT p1 p2
xm p1
wait 0 ML
xm p2
int x(p1.x) y(p1.y) cx(p2.x-p1.x) cy(p2.y-p1.y)
;out
;out "%i %i %i %i" x y cx cy
;adjust rect coords if cx or cy negative
if(cx<0) cx=-cx; x-cx
if(cy<0) cy=-cy; y-cy
;adjust rect because line width is not 0
int c=a.border/2 ;;half of line width
x-c; y-c; cx+a.border; cy+a.border
;set line coords within the rect
a.x=p1.x-x; a.y=p1.y-y; a.x2=p2.x-x; a.y2=p2.y-y
;out "%i %i %i %i" x y cx cy
OnScreenDraw x y cx cy &OSD_line &a 0 1
3
Also, in the rectangle, the line does not always begin in top-left. Use MoveToEx to set starting point.
Function OSD_line
function hwnd hdc cx cy OSDCOLORANDBORDER&a
;Rectangle hdc 0 0 cx cy ;;for debugging
int hpen oldpen
;create/select pen and draw line
hpen=CreatePen(0 a.border a.color); oldpen=SelectObject(hdc hpen)
MoveToEx hdc a.x a.y 0 ;;set current position
LineTo hdc a.x2 a.y2
DeleteObject SelectObject(hdc oldpen)
Also change OnScreenDraw, because now it interprets 0 and negative coordinates as screen center or offset from screen right/bottom.
On-screen drawing
;/
function# x y cx cy drawfunction [param] [transparency] [flags] ;;flags: 1 don't adjust coordinates
;Calls an user-defined function to perform on-screen drawing.
;The function can draw anything (shapes, text, icons, etc) using Windows API functions.
;The on-screen image disappears when the macro ends. To remove the image earlier, call OnScreenDrawEnd.
;Returns window handle that can be used with OnScreenDrawEnd. Also can be used to add controls, etc (not tested).
;The dialog background color is transparent, except on Windows 9x.
;x, y, cx, cy - bounding rectangle on screen. Depends on flag 1.
;drawfunction - address of user-defined function that performs drawing. Must begin with:
;;function hwnd hdc cx cy param
;;Arguments:
;;hwnd - transparent window that is used for on-screen drawing.
;;hdc - device context.
;;cx, cy - width and height of bounding rectangle.
;;param - value passed to OnScreenDraw.
;param - value to pass to the draw function.
;transparency - value between 1 (almost transparent) and 255 (opaque). If omitted or 0, the drawn image will be opaque.
;flags:
;;;1 - don't adjust coordinates. By default, 0 would be interpreted as screen center, and negative - as offset from screen right/bottom.
type OSDA fa param cx cy transp
OSDA* d._new
d.fa=drawfunction
d.param=param
d.cx=cx
d.cy=cy
d.transp=transparency
int hDlg
if(flags&1)
,hDlg=ShowDialog("OSD_Dialog" &OSD_Dialog 0 0 1 0 WS_VISIBLE d)
,mov x y hDlg
,hid- hDlg
else hDlg=ShowDialog("OSD_Dialog" &OSD_Dialog 0 0 1 0 0 d x y)
atend OnScreenDrawEnd hDlg
opt waitmsg 2
0
ret hDlg