Flicker when centring window

BlitzMax Forums/BlitzMax Programming/Flicker when centring window

Grey Alien(Posted 2006) [#1]
Hi, I'm using Eikon and Grisu's window centring code that centres a BMax graphics window (not BMax GUI, I don't want to use that). The only thing I've noticed is that the window appears a the default position for a fraction of a second before centring. The results in a quick little flicker sometimes and also the centred window can show part of the desktop (or IDE) in the corner for a fraction of second.

I'm creating the window with Graphics, and I need to crate it without showing it, centre it and then show it. Hmm, I wonder if this is posible with CreateGraphics ... I'm not sure it is though as the window won't have a handle yet that can be passed to Windows API calls will it? I need to test this I guess...


Grey Alien(Posted 2006) [#2]
bump?

Yeah basically, anyone know how I can get the handle of the window made by CreateGraphics *before* it's shown on screen. this is assuming there is a window that is hidden that has a valid Windows handle...Otherwise there doesn't seem to be a way to centre it without altering the modules, which is not very desireable...


tonyg(Posted 2006) [#3]
Well? Did you try it?
This seems to work OK for me...
mygraphics:TGraphics = CreateGraphics(640 , 480,0,0,GRAPHICS_BACKBUFFER)
ccCentreWindow()
SetGraphics mygraphics
Repeat
	Cls
	DrawText "Hello World!" , 0 , 0
	Flip
Forever


<Edit> Just noticed a slight flicker.


Grey Alien(Posted 2006) [#4]
tonyg: Uh, no as I figured it wouldn't work as what window is GetActiveWindow returning? Sorry for being lazy.

Are you using this code for ccCentreWindow?:

Function ccCentreWindow()
	'Centres the current graphics window on the desktop
	?Win32
	Local hWnd% = GetActiveWindow()
	ccCentreWindowHandle(hWnd%)
	?
End Function

Function ccCentreWindowHandle(hWnd%)
	'Centres the current graphics window on the desktop
	'Pass a handle in
	?Win32
	Local desk_hWnd% = GetDesktopWindow()
	Local desk:TRect = New TRect
	Local window:TRect= New TRect

	GetWindowRect(desk_hWnd,desk) ' Get Desktop Dimensions
    'Get Window Dimensions because final window may have been resized (by BlitzMax) to fit the desktop resultion! (Grey Alien)
	GetWindowRect(hWnd,window)
	
	'Centre Window
	SetWindowPos(hWnd, -2, (desk.r / 2) - ((window.r-window.l) / 2), (desk.b / 2) - ((window.b-window.t) / 2), 0, 0, 1)
	?
End Function



Grey Alien(Posted 2006) [#5]
Strange: I tried your code and indeed it centres, even though I wasn't expecting GetActiveWindow to work. However, yes there is a flicker.

here's my full code:

Strict

Local mygraphics:TGraphics = CreateGraphics(640 , 480,0,0,GRAPHICS_BACKBUFFER)
'ccCentreWindow()
SetGraphics mygraphics
EnablePolledInput()
Repeat
	Cls
	DrawText "Hello World!" , 0 , 0
	Flip
Until KeyHit(KEY_ESCAPE)

?win32
Extern "win32"
	Function GetActiveWindow%()
	Function GetDesktopWindow%()
    Function GetWindowRect%(hWnd%, lpRect: Byte Ptr)
    Function SetWindowPos%(hWnd%, after%, x%, y%, w%, h%, flags%)
End Extern
?

Type TRect
	Field L%, T%, R%, B%
End Type

Function ccCentreWindow()
	'Centres the current graphics window on the desktop
	?Win32
	Local hWnd% = GetActiveWindow()
	ccCentreWindowHandle(hWnd%)
	?
End Function

Function ccCentreWindowHandle(hWnd%)
	'Centres the current graphics window on the desktop
	'Pass a handle in
	?Win32
	Local desk_hWnd% = GetDesktopWindow()
	Local desk:TRect = New TRect
	Local window:TRect= New TRect

	GetWindowRect(desk_hWnd,desk) ' Get Desktop Dimensions
    'Get Window Dimensions because final window may have been resized (by BlitzMax) to fit the desktop resultion! (Grey Alien)
	GetWindowRect(hWnd,window)
	
	'Centre Window
	SetWindowPos(hWnd, -2, (desk.r / 2) - ((window.r-window.l) / 2), (desk.b / 2) - ((window.b-window.t) / 2), 0, 0, 1)
	?
End Function




tonyg(Posted 2006) [#6]
Yes. Did you, at least, try it?
It's better than straight graphics command.


tonyg(Posted 2006) [#7]
I get hardly any flicker at all. You'd *really* have to be checking it to notice.


Grey Alien(Posted 2006) [#8]
yes it is slightly better than with just Graphics. Must be that graphics takes longer setup once the window is shown so it's on screen for longer.

As well as enabling polled input, is there anything else I need to do if just using CreateGraphics?

However, this is only a partial solution. Odd how it makes a flicker even though the window is repositioned *before* it is shown with SetGraphics.


Grey Alien(Posted 2006) [#9]
Aha, found out that even without setgraphics and any drawing commands and the centreing code, the window get's shown ANYWAY!

Strict

Local mygraphics:TGraphics = CreateGraphics(640 , 480,0,0,GRAPHICS_BACKBUFFER)
'ccCentreWindow()
'SetGraphics mygraphics
EnablePolledInput()

'Graphics(640 , 480,0,0,GRAPHICS_BACKBUFFER)
'ccCentreWindow()

Repeat
'	Cls
'	DrawText "Hello World!" , 0 , 0
'	Flip
Until KeyHit(KEY_ESCAPE)


why is that!


tonyg(Posted 2006) [#10]
Not sure but haven't you asked this before ?
It'd be nice if BRL responded on these things once in a while. However about an Enhancement tag in the bug reports?


Grey Alien(Posted 2006) [#11]
Thanks Tonyg: Yeah I had this feeling it was a familiar issue but I never bookmarked that thread.

As I said in that thread, I don't want to alter the module but would prefer either a) centred as default or b) a centred flag. And it would be lovely if BRL responds.

Meanwhile, CreateGraphics results in less flicker. Thanks for you help Tonyg. Anyone else got anything to add?

[edit] Maybe there's a way to override the Graphics function and duplicate all the code in it but create the window centred instead...It's not future compatible with BRL module changes but it would work without altering their modules... Why don't I want to alter their mods? Well, a) I'd have to do it after every Sync mods (or at least check to see if the mod got altered by BRL and thus replaced) and b) my framework customers will need to make module alterations which is a pain for them and need to be documented by me etc


tonyg(Posted 2006) [#12]
I'm sure I didn't change that module but it now says...
		hwnd=CreateWindowExA( 0,DX_CLASS_NAME,wndTitle,style,rect[0],rect[1],width,height,0,0,hinst,Null )

rather than CW_USEDEFAULT


Grey Alien(Posted 2006) [#13]
I'll check mine...mine still says:

		hwnd=CreateWindowExA( 0,DX_CLASS_NAME,wndTitle,style,CW_USEDEFAULT,CW_USEDEFAULT,width,height,0,0,hinst,Null )
but I haven't done a sync mods in the last few weeks. The date time on mine is 01/06/06 12:14

And Rect is specified just above like this:
 Local rect[]=[0,0,width,height]
so we can't alter it :-(