For an eBook or printed book on using the API with Liberty BASIC, see: APIs for Liberty BASIC
Native Graphic Colors
Liberty BASIC's color statement causes graphics to be drawn in with a pen of the specified color. Liberty BASIC's backcolor statement causes graphics ojects to be filled with a brush of the specified color.
Windows GDI creates colored pens with CreatePen. GDI creates brushes with CreateSolidBrush and CreateHashBrush.
GDI Brushes and Pens in Liberty BASIC
Pens and brushes can be created with GDI commands, then selected into the Device Context of a graphicbox. (See earlier lessons in this series for more on Device Contexts.) Liberty BASIC will use these pens and brushes when drawing objects such as "box", "boxfilled", "circle", "circlefilled" and so on.
CreatePen
Pens are the equivalent of Liberty BASIC's graphics "color" statement. When a pen is selected into a Device Context it is used to draw the outlines of objects, such as circles, lines, etc. Only one pen may be selected into a Device Context at a time. The selected pen is used for all drawing until another pen is selected. The default pen in a Device Context is a solid, black pen that is one pixel in width.
CreatePen has three input parameters. You must specify a style, a width and a color. All styles other than a solid pen must be one pixel wide. If a different width is specified, the pen style defaults to solid.
The pen styles are as follows:
_PS_SOLID
The pen is solid. Width MUST be one.
_PS_DASH
The pen is dashed. Width MUST be one.
_PS_DASHDOT
The pen is dotted. Width MUST be one.
_PS_DASHDOT
The pen has alternating dashes and dots. Width MUST be one.
_PS_DASHDOTDOT
The pen has alternating dashes and double dots. Width MUST be one.
_PS_NULL
The pen is invisible.
The color value is a long integer created from the desired red, green and blue values. See GDI and the Device Context for more on long color values.
calldll#gdi32,"CreatePen",_
style aslong,_ 'pen style
width aslong,_ 'width in pixels
rgbCol aslong,_ 'long color value
CreatePen asulong'handle to penendfunction
SelectObject
The pen is not used until it is selected into the Device Context with SelectObject. The function returns the handle of the original object of the type and this default object can be selected back into the Device Context later.
CallDLL#gdi32,"SelectObject",_
hDC asuLong,_ 'handle of DC
hObj asuLong,_ 'handle of object to select
SelectObject asuLong'returns previously selected object
DeleteObject
When an object is no longer needed, we use DeleteObject to free the memory consumed by the object. Do not attempt to delete an object that is currently selected into a Device Context. We first use SelectObject to select the default object back into the Device Context. We may then delete the created object, like so:
CallDLL#gdi32,"DeleteObject",_
hObj asuLong,_ 'handle of object
r Aslong
CreateSolidBrush
A solid brush is created with this code, which requires a long color value and returns the handle of the solid brush:
calldll#gdi32,"CreateSolidBrush",_
rgbCol aslong,_ 'long color value
CreateSolidBrush asulong'returns handle of brush
Only one brush may be selected into a Device Context at one time. We do this with SelectObject* just as we did for the created pen discussed earlier. We reserve the handle of the default brush to select back into the Device Context later, just as we did for the pen.
CreateHatchBrush
A hatch brush is not a solid color; it has a cross-hatched pattern. The styles may be any of the following.
_HS_BDIAGONAL
45-degree upward left-to-right hatch
_HS_CROSS
Horizontal and vertical crosshatch
_HS_DIAGCROSS
45-degree crosshatch
_HS_FDIAGONAL
45-degree downward left-to-right hatch
_HS_HORIZONTAL
Horizontal hatch
_HS_VERTICAL
Vertical hatch
CreateHatchBrush also requires a long color value for the brush.
calldll#gdi32,"CreateHatchBrush",_
style aslong,_ 'brush style
rgbCol aslong,_ 'long color value
CreateHatchBrush asulong'handle of brush
Once created, it may be selected into a Device Context in exactly the same way as the pen and solid brush discussed earlier. Like them, it must be deleted when no longer needed, in order to free memory.
GDI Drawn Objects
The examples here demonstrate the creation of pens and brushes and their use with native Liberty BASIC drawing commands. We can also draw graphics with GDI commands, using these pens and brushes. We can even draw on memory Device Contexts. This will be covered in a later lesson.
Demo
Run the code and you will see this:
nomainwin
winWide=700:winHigh=500WindowWidth=winWide+50:WindowHeight=winHigh+50UpperLeftX=1:UpperLeftY=1graphicbox#1.g,0,0,winWide,winHigh
open"GDI Demo"forwindowas#1#1"trapclose [quit]"#1.g "down"
hDC = GetDC(hwnd(#1.g))'device context handle of graphicbox
hPen = CreatePen(_PS_DASHDOT,1,MakeRGB(240,200,140))
oldPen = SelectObject(hDC,hPen)#1.g "place 10 10; box 200 100"
hBrush = CreateSolidBrush(MakeRGB(100,200,255))
oldBrush = SelectObject(hDC,hBrush)#1.g "place 10 120; boxfilled 200 250"
hHatch = CreateHatchBrush(_HS_CROSS,MakeRGB(200,80,170))
hObj = SelectObject(hDC,hHatch)#1.g "place 10 270; boxfilled 200 420"wait[quit]
re = SelectObject(hDC,oldPen)
re = SelectObject(hDC,oldBrush)call ReleaseDC hwnd(#1.g),hDC
call DeleteObject hPen
close#1:endfunction CreateSolidBrush(rgbCol)calldll#gdi32,"CreateSolidBrush",_
rgbCol aslong,_ 'long color value
CreateSolidBrush asulong'returns handle of brushendfunctionfunction CreateHatchBrush(style,rgbCol)'_HS_BDIAGONAL 45-degree upward left-to-right hatch'_HS_CROSS Horizontal and vertical crosshatch'_HS_DIAGCROSS 45-degree crosshatch'_HS_FDIAGONAL 45-degree downward left-to-right hatch'_HS_HORIZONTAL Horizontal hatch'_HS_VERTICAL Vertical hatchcalldll#gdi32,"CreateHatchBrush",_
style aslong,_ 'brush style
rgbCol aslong,_ 'long color value
CreateHatchBrush asulong'handle of brushendfunctionfunction CreatePen(style,width,rgbCol)'styles'_PS_SOLID The pen is solid.'_PS_DASH The pen is dashed. Width MUST be one.'_PS_DOT The pen is dotted. Width MUST be one.'_PS_DASHDOT The pen has alternating dashes and dots. Width MUST be one.'_PS_DASHDOTDOT The pen has alternating dashes and double dots. Width MUST be one.'_PS_NULL The pen is invisible.'If width is >1 a solid pen is created.calldll#gdi32,"CreatePen",_
style aslong,_ 'pen style
width aslong,_ 'width in pixels
rgbCol aslong,_ 'long color value
CreatePen asulong'handle to penendfunctionfunction MakeRGB(red,green,blue)if red<0then red=0if red>255then red=255if green<0then green=0if green>255then green=255if blue<0then blue=0if blue>255then blue=255
MakeRGB=(blue*256*256)+(green*256)+red
endfunctionfunction GetDC(h)'get device context for window:calldll#user32,"GetDC",_
h asulong,_ 'graphicbox handle
GetDC asulong'returns handle to device contextendfunctionfunction SelectObject(hDC, hObj)CallDLL#gdi32,"SelectObject",_
hDC asuLong,_ 'handle of DC
hObj asuLong,_ 'handle of object to select
SelectObject asuLong'returns previously selected objectendfunctionsub DeleteObject hObj
CallDLL#gdi32,"DeleteObject",_
hObj asuLong,_
r Aslongendsubsub ReleaseDC h, hdc
calldll#user32,"ReleaseDC",_
h asulong,_ 'window handle
hdc asulong,_ 'device context
ret aslongendsub
Native Graphic Colors | GDI Brushes and Pens in Liberty BASIC | CreatePen | SelectObject | DeleteObject | CreateSolidBrush | CreateHatchBrush | GDI Drawn Objects | Demo Some text below is copied from the Microsoft Developers Network Library.
For an eBook or printed book on using the API with Liberty BASIC, see:
APIs for Liberty BASIC
Native Graphic Colors
Liberty BASIC's color statement causes graphics to be drawn in with a pen of the specified color. Liberty BASIC's backcolor statement causes graphics ojects to be filled with a brush of the specified color.Windows GDI creates colored pens with CreatePen. GDI creates brushes with CreateSolidBrush and CreateHashBrush.
GDI Brushes and Pens in Liberty BASIC
Pens and brushes can be created with GDI commands, then selected into the Device Context of a graphicbox. (See earlier lessons in this series for more on Device Contexts.) Liberty BASIC will use these pens and brushes when drawing objects such as "box", "boxfilled", "circle", "circlefilled" and so on.CreatePen
Pens are the equivalent of Liberty BASIC's graphics "color" statement. When a pen is selected into a Device Context it is used to draw the outlines of objects, such as circles, lines, etc. Only one pen may be selected into a Device Context at a time. The selected pen is used for all drawing until another pen is selected. The default pen in a Device Context is a solid, black pen that is one pixel in width.CreatePen has three input parameters. You must specify a style, a width and a color. All styles other than a solid pen must be one pixel wide. If a different width is specified, the pen style defaults to solid.
The pen styles are as follows:
The color value is a long integer created from the desired red, green and blue values. See GDI and the Device Context for more on long color values.
SelectObject
The pen is not used until it is selected into the Device Context with SelectObject. The function returns the handle of the original object of the type and this default object can be selected back into the Device Context later.DeleteObject
When an object is no longer needed, we use DeleteObject to free the memory consumed by the object. Do not attempt to delete an object that is currently selected into a Device Context. We first use SelectObject to select the default object back into the Device Context. We may then delete the created object, like so:CreateSolidBrush
A solid brush is created with this code, which requires a long color value and returns the handle of the solid brush:Only one brush may be selected into a Device Context at one time. We do this with SelectObject* just as we did for the created pen discussed earlier. We reserve the handle of the default brush to select back into the Device Context later, just as we did for the pen.
CreateHatchBrush
A hatch brush is not a solid color; it has a cross-hatched pattern. The styles may be any of the following.CreateHatchBrush also requires a long color value for the brush.
Once created, it may be selected into a Device Context in exactly the same way as the pen and solid brush discussed earlier. Like them, it must be deleted when no longer needed, in order to free memory.
GDI Drawn Objects
The examples here demonstrate the creation of pens and brushes and their use with native Liberty BASIC drawing commands. We can also draw graphics with GDI commands, using these pens and brushes. We can even draw on memory Device Contexts. This will be covered in a later lesson.Demo
Run the code and you will see this:
GDI Tutorials Home