Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OSD Line from A to B
#1
I'd like to draw a line on the screen from one point to another.. So you could once click some where and that would be the start point. Then you click another place and that would be the end point. Then it draws a line between them.

I tried to do it, but I didn't manage to get it working well..

test:
Code:
Copy      Help
type OSDCOLORANDBORDER color border
OSDCOLORANDBORDER a
a.color=ColorFromRGB(255 0 0)
a.border=2

int px = xm
int py = ym
1
OnScreenDraw px py xm-px ym-py &OSD_line &a
10

OSD_line:
Code:
Copy      Help
function hwnd hdc cx cy OSDCOLORANDBORDER&a
int hpen oldpen
;create/select pen and draw rectangle
hpen=CreatePen(0 a.border a.color); oldpen=SelectObject(hdc hpen)
LineTo hdc, cx, cy
DeleteObject SelectObject(hdc oldpen)
#2
Needs to adjust coordinates. For example, if second click is at left from first click, rectangle width would be negative.

Code:
Copy      Help
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
Code:
Copy      Help
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

Code:
Copy      Help
;/
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
#3
Thanks! Smile


Forum Jump:


Users browsing this thread: 1 Guest(s)