GSI example

Miscellaneous Forums/Win32 Discussion/GSI example

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's a simple GDI example that clears a window, and draws a line and text, with no flickering when the window is resized.

Strict

Extern "win32"
	Function ReleaseDC( hwnd:Int, hdc:Int )
	Function GetCurrentObject( hdc:Int, objectType:Int )
	Function MoveToEx( hdc:Int, x:Int, y:Int, point:Int )
	Function LineTo( hdc:Int, x:Int, y:Int )
	Function CreatePen:Int(fnPenStyle:Int,nWidth:Int,crColor:Int)
	Function FillRect(hdc:Int,rect:Byte Ptr,hbr:Int)
	Function GetDeviceCaps(hdc%,index%)
	Function DrawTextA(hdc%,txt$z,nCount%,tRECT:Byte Ptr,format%)
	Function MulDiv(nNumber%,nNumerator%,nDenominator%)
	Function SetBkMode(hdc%,mode%)
	Function SetTextColor(hdc%,color%)
	Function CreateFontA(nHeight%,nWidth%,nEscapement%,nOrientation%,fnWeight%,fdwItalic%,fdwUnderline%,fdwStrikeOut%,fdwCharSet%,fdwOutputPrecision%,fdwClipPrecision%,fdwQuality%,fdwPitchAndFamily%,lpszFace$z)
EndExtern

Global win:TGadget = CreateWindow("LineTest", 0,0, 400,400 )
Global canvas:TGadget = CreateCanvas( 0,0,400,400, win )
SetGadgetLayout canvas,1,1,1,1
SetGraphics CanvasGraphics(canvas)

Local g:TGadget=CreateComboBox(200,200,100,26,canvas)
SetGadgetLayout g,1,0,1,0
AddGadgetItem g,"Test"
SelectGadgetItem g,0

AddHook EmitEventHook,hook

While WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			End
	EndSelect
Wend

Function Hook:Object(id:Int,data:Object,context:Object)
	Local event:TEvent=TEvent(data)
	If Not event Return data
	Select event.id
		Case EVENT_GADGETPAINT,EVENT_WINDOWSIZE
			SetGraphics CanvasGraphics(canvas)
			GDICls()
			gdiDrawLine(10,10,200,100)
			Return
	EndSelect
	Return data
EndFunction

Function gdiCls()
	Local pen:Int=CreatePen(0,10,$00FF0000)
	Local hwnd:Int=QueryGadget( canvas, QUERY_HWND )
	Local hdc:Int=GetDC( hwnd )
	SelectObject(hdc,pen)
	fillrect hdc,[0,0,GraphicsWidth(),GraphicsHeight()],pen
	ReleaseDC(hwnd, hdc)
	DeleteObject( pen )	
EndFunction

Function gdiDrawLine(x#,y#,x2#,y2#,draw_last_pixel=True)
	Local pen:Int=CreatePen(0,1,$0000FF00)
	Local hwnd:Int=QueryGadget( canvas, QUERY_HWND )
	Local hdc:Int=GetDC( hwnd )
	SelectObject(hdc,pen)
	
	MoveToEx(hdc,x,y,0)
	LineTo(hdc,x2,y2)
	gdiDrawText "Hello",0,0
	
	ReleaseDC(hwnd, hdc)
	DeleteObject( pen )	
EndFunction

Function gdiDrawText(text$,x,y)
	Local hwnd:Int=QueryGadget(canvas,QUERY_HWND)
	Local hdc:Int=GetDC( hwnd )
	Local size=12
	Const LOGPIXELSYS = 90
	Const DEFAULT_CHARSET = 1
	Const OUT_DEFAULT_PRECIS = 0
	Const CLIP_DEFAULT_PRECIS = 0
	Const PROOF_QUALITY = 2
	Const DEFAULT_PITCH = 0
	Local weight%=FW_NORMAL , italics%=False , underline%=False
	Local textformat=0
	Const DT_NOCLIP=$100
	Local width,height
	Local fontobject%=CreateFontA(-MulDiv(size,GetDeviceCaps(hdc,LOGPIXELSYS),72), 0, 0, 0, weight, italics, underline, False, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, "Arial")
	SelectObject hdc,fontobject
	SetTextColor hdc, 255 | 0 Shl 8 | 0 Shl 16
	Local tf%=TextFormat|DT_NOCLIP
	If width>0 And height>0 tf=TextFormat
	SetBkMode hdc,$1
	DrawTextA hdc,text$,text.Length,[x,y,x+width,y+height],tf
EndFunction