GSI example

Archives Forums/Win32 Discussion/GSI example

JoshK(Posted 2008) [#1]
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



Brucey(Posted 2008) [#2]
And here's a cross-platform GDI example :-)




JoshK(Posted 2008) [#3]
Your distribution for the wxmax module is horrendous. I can't even figure out what it is I am supposed to download.


Brucey(Posted 2008) [#4]
I wouldn't worry about it really.


plash(Posted 2008) [#5]
I don't know what you were going for on this, if your going to be using GDI in a project take a look at this: http://www.blitzbasic.com/Community/posts.php?topic=56055

BTW wxmax is well worth the time.


altitudems(Posted 2008) [#6]
I don't really see why I would ever need GDI, however OpenGL inside wxMax would rock.


altitudems(Posted 2008) [#7]
I don't really see why I would ever need GDI, however OpenGL inside wxMax would rock.


JoshK(Posted 2008) [#8]
All those GDI examples use a non-sizable window to hide the fact they flicker badly when resized. That's why I wrote my own.


xlsior(Posted 2008) [#9]
Here's a simple GDI example that clears a window, and draws a line and text, with no flickering when the window is resized.


It does flicker on my machine... Not as bad as many other GDI apps, but it's definitely visible in the line and very noticable in the dropdown box when I resize it.

(XP Pro SP2, X1650XT)


JoshK(Posted 2008) [#10]
The dropdown box will flicker in a normal BMX app when resized.


skidracer(Posted 2008) [#11]
I still get flickering when the window is resized.

To reduce the flicker of the dropdown box to just the knob try making it an older child of the window not the canvas.