Drawing Pictures using the GDI DLL
Keywords: GDID32.DLL GDI Dll
This is a pretty interesting example of how to use the GDI32.DLL to draw pictures in boxes with Winbatch.
winx1 = 100
winy1 = 100
winx2 = 900
winy2 = 900
wincoords = StrCat(winx1, ",", winy1, ",", winx2, ",", winy2)
BoxesUp(wincoords, @Normal)
UserDLL = (StrCat(DirWindows(1),"user32.dll"))
GDIDLL = (StrCat(DirWindows(1),"GDI32.dll"))
UserH = DllLoad (UserDLL)
GdiH = DllLoad (GDIDLL)
win = WinName()
hwnd = DllHwnd(win)
MM_TEXT = 1
MM_ISOTROPIC = 7
PS_DASH = 1
PS_DASHDOT = 3
PS_DASHDOTDOT = 4
PS_DOT = 2
PS_INSIDEFRAME = 6
PS_NULL = 5
PS_SOLID = 0
GDI_ERROR = 65535
penWidth = 1
penStyle = PS_SOLID
hRgn = 0
bbMax = 100000
bb = BinaryAlloc(bbMax)
WinhDC = DllCall (Userh, long:"GetDC", long:hWnd)
MaxX = WinMetrics(0)
MaxY = WinMetrics(1)
MapMode = MM_Isotropic
OldMapMode = DllCall (GdiH, long:"SetMapMode", long:WinhDC, long:MapMode)
SizeStruct = BinaryAlloc(8)
nXextent = 1000
nYextent = 1000
result = DllCall (GdiH, long:"SetWindowExtEx", long:WinhDC, long:nXextent, long:nYextent, lpBinary:SizeStruct)
nXextent = MaxX
nYextent = MaxY
result = DllCall (GdiH, long:"SetViewportExtEx", long:WinhDC, long:nXextent, long:nYextent, lpBinary:SizeStruct)
Params = "128 128 128"
Gosub CreateBrush
FirstObject = DllCall (GdiH, long:"SelectObject", long:WinhDC, long:HBrush)
Params = "8 155 8"
Gosub CreateBrush
Params = "253 10 15 1 3"
Gosub CreatePen
Params = "300 100"
Gosub DrawLineTo
Params = "100 100 250 250"
Gosub DrawEllipse
Params = "700 100 950 250"
Gosub DrawRect
Params = "400 400 650 750 440 472 630 700"
Gosub DrawArc
Params = "0 0 1000 1000 0 0 0 0"
Gosub DrawArc
Params = "500 600 750 750 540 672 730 700"
Gosub DrawChord
Params = "400 100 750 650 400 400"
Gosub DrawRoundRect
Params = "140 400 650 750 140 172 330 400"
Gosub DrawPie
;dotted lines only possible with penwidth of 1 (as followin shows)
Params = "100 190 225 5 3"
Gosub CreatePen
Params = "5"
Params2 = "0,200 250,0 500,200 400,500 100,500"
Gosub DrawPolygon
Params = "228 128 128"
Gosub CreateBrush
Params = "5"
Params2 = "0,200 500,200 100,500 250,0 400,500"
Gosub DrawPolygon
Params = "0 0"
Gosub DrawLineTo
Params = "830 600"
Gosub DrawLineTo
BoxTitle("Press control key to exit.")
While @True
TimeDelay(0.2)
If IsKeyDown(@ctrl) then break
EndWhile
Dummy = DllCall (GdiH, long:"DeleteObject", long:HPen)
Dummy = DllCall (GdiH, long:"DeleteObject", long:HBrush)
Dummy = DllCall (GdiH, long:"SelectObject", long:WinhDC, long:FirstObject)
Dummy = DllCall (GdiH, long:"SetMapMode", long:WinhDC, long:OldMapMode)
Result = DllCall (Userh, long:"ReleaseDC", long:hWnd, long:WinhDC)
DllFree (GdiH)
DllFree (UserH)
BinaryFree(bb)
BinaryFree(SizeStruct)
EXIT
:CreateBrush
ParseData(Params)
If Param0 != 3 then Message("Incorrect # of parameters for CreateBrush", "3 needed, %Param0% sent")
r = Param1
g = Param2
b = Param3
brushColor = (0 << 24) | (b << 16) | (g << 8) | r
if IsDefined(HBrush) then Dummy = DllCall (GdiH, long:"DeleteObject", long:HBrush)
HBrush = DllCall (GdiH, long:"CreateSolidBrush", long:brushColor)
Params = HBrush
Gosub SelectObject
RETURN
:CreatePen
;params = Red Green Blue penWidth penStyle
ParseData(Params)
If Param0 < 3 || Param0 > 5 then Message("Incorrect # of parameters for CreatePen", "3 to 5 needed, %Param0% sent")
r = Param1
g = Param2
b = Param3
if Param0 > 3 then penWidth = Param4
if Param0 == 5 then penStyle = Param5
penColor = (0 << 24) | (b << 16) | (g << 8) | r
if IsDefined(HPen) then Dummy = DllCall (GdiH, long:"DeleteObject", long:HPen)
HPen = DllCall (GdiH, long:"CreatePen", long:penStyle, long:penWidth, long:penColor)
Params = HPen
Gosub SelectObject
RETURN
:SelectObject
ParseData(Params)
if Param0 != 1 then Message("Incorrect # of parameters for SelectObject", "1 needed, %Param0% sent")
LastObject = DllCall (GdiH, long:"SelectObject", long:WinhDC, long:Param1)
if LastObject == 0 || LastObject == GDI_ERROR
Message("Last object error is:", LastObject)
endif
RETURN
:DrawArc
ParseData(Params)
if Param0 != 8 then Message("Incorrect # of parameters for Arc", "8 needed, %Param0% sent")
Result = DllCall (GdiH, long:"Arc", long:WinhDC, long:Param1, long:Param2, long:Param3, long:Param4, long:Param5, long:Param6, long:Param7, long:Param8)
;Result = DllCall (GdiH, long:"Arc", long:WinhDC, long:x1, long:y1, long:x2, long:y2, long:ax1, long:ay1, long:ax2, long:ay2)
if Result == 0 then Message("Arc failed", "")
RETURN
:DrawChord
ParseData(Params)
if Param0 != 8 then Message("Incorrect # of parameters for Chord", "8 needed, %Param0% sent")
Result = DllCall (GdiH, long:"Chord", long:WinhDC, long:Param1, long:Param2, long:Param3, long:Param4, long:Param5, long:Param6, long:Param7, long:Param8)
if Result == 0 then Message("Chord failed", "")
RETURN
:DrawEllipse
ParseData(Params)
if Param0 != 4 then Message("Incorrect # of parameters for Ellipse", "4 needed, %Param0% sent")
Result = DllCall (GdiH, long:"Ellipse", long:WinhDC, long:Param1, long:Param2, long:Param3, long:Param4)
;Result = DllCall (GdiH, long:"Ellipse", long:WinhDC, long:x1, long:y1, long:x2, long:y2)
if Result == 0 then Message("Ellipse failed", "")
RETURN
:DrawLineTo
ParseData(Params)
if Param0 != 2 then Message("Incorrect # of parameters for LineTo", "2 needed, %Param0% sent")
Result = DllCall (GdiH, long:"LineTo", long:WinhDC, long:Param1, long:Param2)
if Result == 0 then Message("LineTo failed", "")
RETURN
:DrawPie
ParseData(Params)
if Param0 != 8 then Message("Incorrect # of parameters for Pie", "8 needed, %Param0% sent")
Result = DllCall (GdiH, long:"Pie", long:WinhDC, long:Param1, long:Param2, long:Param3, long:Param4, long:Param5, long:Param6, long:Param7, long:Param8)
if Result == 0 then Message("Pie failed", "")
RETURN
:DrawPolygon
ParseData(Params)
if Param0 != 1 then Message("Incorrect # of parameters for Polygon", "1 needed (plus Params2), %Param0% sent")
Params2 = StrReplace(Params2, ",", " ")
pcount = ItemCount(Params2, " ")
pcount2 = pcount / 2
if pcount < 4 then Message("Params2 needs at least 2 points (x,y)", "only %pcount% sent")
if pcount mod 2 != 0
Message("Odd number of params sent in Params2", "Must be sent in pairs")
Param0 = 0
EndIf
if Param1 != Pcount2 then Message("Warning: Polygon param1 does not match number of points", "%param1% vs. %pcount2%")
if Param0 != 1 || pcount < 4 then RETURN
psize = pcount * 4
ib = BinaryAlloc(psize)
for dploop = 1 to pcount
temp = ItemExtract(dploop, params2, " ")
temp2 = (dploop - 1) * 4
BinaryPoke4(ib, temp2, temp)
next dploop
Result = DllCall (GdiH, long:"Polygon", long:WinhDC, lpbinary:ib, long:pcount2)
BinaryFree(ib)
if Result == 0 then Message("Polygon failed", "")
RETURN
:DrawRect
ParseData(Params)
if Param0 != 4 then Message("Incorrect # of parameters for Rectangle", "4 needed, %Param0% sent")
Result = DllCall (GdiH, long:"Rectangle", long:WinhDC, long:Param1, long:Param2, long:Param3, long:Param4)
;Result = DllCall (GdiH, long:"Rectangle", long:WinhDC, long:x1, long:y1, long:x2, long:y2)
if Result == 0 then Message("Rectangle failed", "")
RETURN
:DrawRoundRect
ParseData(Params)
if Param0 != 6 then Message("Incorrect # of parameters for RoundRect", "6 needed, %Param0% sent")
Result = DllCall (GdiH, long:"RoundRect", long:WinhDC, long:Param1, long:Param2, long:Param3, long:Param4, long:Param5, long:Param6)
if Result == 0 then Message("RoundRect failed", "")
RETURN
Article ID: W14720
Filename: Drawing Pictures using the GDI Dll_1.txt