GDI can do math.
Posted: Mon May 22, 2006 10:58 pm
Code updated for 5.20+
Allthough not very clever...
Allthough not very clever...

Code: Select all
Enumeration
#ImageID = 0
#ImgGadgetID
#TxtGadgetID
#LstGadgetID
EndEnumeration
Global Dim PPoint.POINT(3)
Global Dim PointCoords.POINT(3)
Global Dim PointTypes.b(3)
Global cursor_pos.POINT
Global prv_cursor_pos.POINT
Global lDraw
Global x.f,y.f,zx.f,zy.f
Global rect.RECT
Global hBGBrush, ImgH, ImgW
Procedure RefreshImage()
NumPoints=0
wdc=GetDC_(WindowID(0))
hdc=CreateCompatibleDC_(wdc)
SelectObject_(hdc,ImageID(#ImageID))
;Box
FillRect_(hdc,@rect,hBGBrush)
;Pen
newpen = CreatePen_(#PS_SOLID, 1, RGB(255,0,0))
oldpen = GetCurrentObject_(hdc,#OBJ_PEN)
SelectObject_(hdc,newpen)
;Bezier
If y<0
y=0
ElseIf y>=ImgH
y=ImgH
EndIf
If x<0
x=0
ElseIf x>=ImgW
x=ImgW
EndIf
;StartPoint
PPoint(0)\x = 0
PPoint(0)\y = ImageHeight(#ImageID)-1
;ControlPoints
PPoint(1)\x = x
PPoint(1)\y = y
PPoint(2)\x = x
PPoint(2)\y = y
;EndPoint
PPoint(3)\x = ImageWidth(#ImageID)-1
PPoint(3)\y = 0
;Path
BeginPath_(hdc)
PolyBezier_(hdc,@PPoint(),4)
EndPath_(hdc)
FlattenPath_(hdc)
NumPoints=GetPath_(hdc,0,0,0)
ReDim PointCoords.POINT(NumPoints)
ReDim PointTypes.b(NumPoints)
Nr=GetPath_(hdc, PointCoords(), PointTypes(), NumPoints)
StrokePath_(hdc)
;CleanUp
SelectObject_(hdc,oldpen)
DeleteObject_(newpen)
DeleteDC_(hdc)
ReleaseDC_(WindowID(0),wdc)
ProcedureReturn NumPoints
EndProcedure
;Main
ImgW=580
ImgH=460
CreateImage(#ImageID, ImgW,ImgH)
hBGBrush=CreateSolidBrush_(RGB(0,0,0))
rect\top=0
rect\left=0
rect\bottom=ImageHeight(#ImageID)
rect\right=ImageWidth(#ImageID)
x=ImageWidth(#ImageID)/2
y=ImageHeight(#ImageID)/2
zy=5
zx=zy*(ImageWidth(#ImageID)/ImageHeight(#ImageID))
hWnd1 = OpenWindow(0,0,0,800,600,"GDI does Math (Click On Image and Drag cursor)",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If hWnd1
SetWindowColor(0, 0)
hGadgetList = CreateGadgetList(hWnd1)
If hGadgetList
ImageGadget(#ImgGadgetID, 0, 0, ImgW, ImgH, ImageID(#ImageID),#PB_Image_Border)
TextGadget(#TxtGadgetID,ImgW+10,0,200,25,"")
ListViewGadget(#LstGadgetID,ImgW+10,40,200,500)
d=ImageWidth(#ImageID)/ImageHeight(#ImageID)
RefreshImage()
SetGadgetState(#ImgGadgetID, ImageID(#ImageID))
Repeat
EventID=WaitWindowEvent()
If EventID=#WM_LBUTTONUP
lDraw=0
EndIf
If EventID=#WM_MOUSEMOVE
If lDraw
GetCursorPos_(@cursor_pos.POINT)
ScreenToClient_(WindowID(hWin),@cursor_pos)
If cursor_pos\y<prv_cursor_pos\y
y-zy
x-zx
NumPoints=RefreshImage()
prv_cursor_pos\y=cursor_pos\y
ElseIf cursor_pos\y>prv_cursor_pos\y
y+zy
x+zx
NumPoints=RefreshImage()
prv_cursor_pos\y=cursor_pos\y
EndIf
SetGadgetState(#ImgGadgetID, ImageID(#ImageID))
SetGadgetText(#TxtGadgetID,"Bezier was drawn with " + Str(NumPoints) + " points")
ClearGadgetItemList(#LstGadgetID)
For i=0 To NumPoints-1
AddGadgetItem (#LstGadgetID, -1, "Point-" + Str(i) + ": " + Str(PointCoords(i)\x) + "-" + Str(PointCoords(i)\y) )
StartDrawing(ImageOutput(#ImageID))
Box(PointCoords(i)\x-2,PointCoords(i)\y-2,4,4,RGB(0,255,0))
StopDrawing()
SetGadgetState(#ImgGadgetID, ImageID(#ImageID))
Next
EndIf
EndIf
If EventID=#PB_Event_Gadget
If EventType()=#PB_EventType_LeftClick
lDraw=1
EndIf
EndIf
Until EventID=#PB_Event_CloseWindow
EndIf
EndIf