att: Eikon/Grisu re: window centreing

BlitzMax Forums/BlitzMax Programming/att: Eikon/Grisu re: window centreing

Grey Alien(Posted 2006) [#1]
Eikon and Grisu. I have some more information about your code to centre a window in BMax. I couldn't find it in Code Archives? Is it there, if not, maybe I could post it on your behalf?

Basically if you set the size to be bigger or the same as the desktop resolution, BlitzMax resizes the window so you don't want to use the GFX_WIDTH and GFX_HEIGHT variables any more for centring. My modifications below get the final window dimensions and use those instead.

Also, and this is weird, if you run my example below and your desktop is 1024x768, BlitzMax will resize the window but make it too short, worse still it starts drawing the window from line 13, thus the top 12 lines of pixels are NOT drawn. You can see this because the DrawText "test" output cannot be seen AND the top one of my four orange lines cannot be seen unless you change the y coords to 13! This is a bit annoying. However, if you DON'T recentre the window the top 12 lines ARE there BUT they are missing from the bottom instead. Talk about weird! [EDIT], commenting out the centring still results in the same error, my mistake due to running two tests at once.

' //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
' // BlitzMax Window Framework by Eikon
' //                                modified by Grisu and GreyAlien
' // BMX 1.18
' //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
Strict

' // Framework & Modules //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
Framework BRL.GLMax2D
Import BRL.Basic
Import BRL.System

' // Win32 API //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
Extern "win32"
   Function GetActiveWindow%()
   Function GetDesktopWindow%()
   Function GetWindowRect%(hWnd%, lpRect:Byte Ptr)
   Function SetWindowText%(hWnd%, lpString$z) = "SetWindowTextA@8"
   Function SetWindowPos%(hWnd%, after%, x%, y%, w%, h%, flags%)
End Extern

Type lpRECT
   Field l%, t%, r%, b%
End Type

' // Create Window %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%//
Const GFX_WIDTH = 1024, GFX_HEIGHT = 768, BIT_DEPTH = 0, HERTZ = -1

Graphics GFX_WIDTH, GFX_HEIGHT, BIT_DEPTH, HERTZ
Local hWnd% = GetActiveWindow()
Local desk_hWnd% = GetDesktopWindow(), l:lpRect = New lpRECT
Local window:lpRect = New lpRect

GetWindowRect desk_hWnd, l:lpRECT ' Get Desktop Dimensions
SetWindowText hWnd, "My Window"  ' Set Window Text
' Get Window Dimensions because final window may have been resized to fit the desktop resultion! (Grey Alien)
GetWindowRect hWnd, window:lpRECT

' Center Window
SetWindowPos hWnd, -2, (l.r / 2) - ((window.r-window.l) / 2), (l.b / 2) - ((window.b-window.t) / 2), 0, 0, 1

'No longer need this as garbage collection is now automatic (Grey Alien)
'l:lpRECT = Null ' Mark for garbage collection
'GCCollect() ' Do collection :)

Repeat;

	SetColor 255,200,0
	DrawText "test",0,0
	DrawLine 100,0,210,0 'top
	DrawLine 100,767,210,767 'bottom	
	DrawLine 0,100,0,210 'left
	DrawLine 1023,100,1023,210 'right
	
Flip; Until KeyDown(KEY_ESCAPE) Or (AppTerminate()=True) ' also exit if windowbutton is used!!! 



Eikon(Posted 2006) [#2]
Thanks for updating the code for me. I've put the new version in the old code archive entry

http://www.blitzbasic.com/codearcs/codearcs.php?code=1318

Also, I can't recreate the bug you're describing when using 1024x768 as my desktop size. The text and line appear at the top as they should


TartanTangerine (was Indiepath)(Posted 2006) [#3]
I can't re-create the issue either. And sorry to be a bore but I hate windows that automatically centre.


Grey Alien(Posted 2006) [#4]
Eikon: cool

@both, weird you don't get the same issue. OK, so when this runs, you may get text and a line at the top, but I bet you don't get one at the bottom because the window is smaller than 768 tall. Is this correct?

Indie: My problem with non-auto centred window is that it can literally appear anywhere. Also if your window is the same size as the desktop, centering it makes more of it visible.


Eikon(Posted 2006) [#5]
@both, weird you don't get the same issue. OK, so when this runs, you may get text and a line at the top, but I bet you don't get one at the bottom because the window is smaller than 768 tall. Is this correct?
The graphics area is actually 768 tall, you've just got to account for the titlebar in the height, which pushes the bottom area off screen.


Grey Alien(Posted 2006) [#6]
weird because on mine, If I click on the title bar and drag it as high as it goes, the bottom of the window can be seen with space underneath it, because it's 13 pixels short. now that shouldn't happen if it was 768 pixels.


Grey Alien(Posted 2006) [#7]
update: It wasn't anything to do with the centring, just merely that it was in OpenGL graphics. On my PC OpenGL decides to start the client area at Y Coord 13 instead of 0, great huh!