GSI example

Miscellaneous Forums/Win32 Discussion/GSI example

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


And here's a cross-platform GDI example :-)

SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import wx.wxScrolledWindow


New MyApp.run()

Type MyApp Extends wxApp

	Field frame:MyFrame

	Method OnInit:Int()

		frame = MyFrame(New MyFrame.Create(,,"Cross-platform GDI!"))

		frame.Center()
		frame.show()
		
		Return True
	
	End Method

End Type

Type MyFrame Extends wxFrame

	Method OnInit()
		New MyCanvas.Create(Self, -1)
	End Method

End Type

Type MyCanvas Extends wxScrolledWindow

	Method OnDraw(dc:wxDC)

		dc.SetPen(wxBLACK_PEN())
		dc.DrawLine(0,0,100,200)
		dc.SetBackgroundMode(wxTRANSPARENT)
		dc.DrawText("Testing",50,50)
		dc.SetPen(wxRED_PEN())
		dc.SetBrush(wxGREEN_BRUSH())
		dc.DrawRectangle(120,120,100,80)
		
		dc.Free()
		
	End Method

End Type


Your distribution for the wxmax module is horrendous. I can't even figure out what it is I am supposed to download.

I wouldn't worry about it really.

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.

I don't really see why I would ever need GDI, however OpenGL inside wxMax would rock.

I don't really see why I would ever need GDI, however OpenGL inside wxMax would rock.

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.

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)

The dropdown box will flicker in a normal BMX app when resized.

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.