WinAPI - Get

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

I'm trying to make a generic procedure to Center any Window on the computer screen but I can't seem to 'extract' the WinAPI RECT structure to get the proper Windows coordinates.

Here's what I have so far:

Code: Select all

;
; ------------------------------------------------------------
;
; Center the Main Menu Screen
;                                  aug.18,2001
; ------------------------------------------------------------
;
Global hWnd.l   ; a handle for each window

Procedure.l CenterWindow(hWnd)
; Generic routine to center any window given its handle.
;
; Structure is already declared (must be in library info?)
;      Structure RECT
;         Left.l
;         Top.l
;         Right.l
;         Bottom.l
;      EndStructure

*RECT = GetWindowRect_(hWnd, RECT)
;-------------------------------------------------
;Original stuff
;x.w = (GetSystemMetrics_((SM_CXSCREEN) - (WndRect\Right-WndRect\Left)))/2 
;y.w = (GetSystemMetrics_((SM_CYSCREEN)- (WndRect\Bottom-WndRect\Top + GetSystemMetrics_(SM_CYCAPTION)))/2
;SetWindowPos_(hWnd, NULL, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER)
;------------------------------------------------  
;x.w =  GetSystemMetrics_((SM_CXSCREEN))
x = *RECT\Right

result = messagerequester("title", str(x), 0)


SetWindowPos_(hWnd, NULL, x, y, 0, 0, SWP_NOSIZE Or SWP_NOZORDER)
EndProcedure

        
result = GetWindow_(hWnd, NULL)

If OpenWindow(hWnd, 100, 150, 450, 400, #PB_Window_SystemMenu, "System - Main Menu Screen")

CenterWindow(hWnd)

  If CreateStatusBar(0, WindowID())
    AddStatusBarField(100)
    AddStatusBarField(250)
    AddStatusBarField(100)
  EndIf
  
  Prog.s = "Power is in the programmer"

  SetStatusBarText(0, 0, "Area 1", 0)
  SetStatusBarText(0, 1, Prog, 0)
  SetStatusBarText(0, 2, "Area 3", 0)
  
  Repeat
  Until WaitWindowEvent() = #PB_EventCloseWindow
  
EndIf
 
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Why not stick with the commands supplied by PB instead of the API calls??

Code: Select all

width.l=GetSystemMetrics_(#SM_CXSCREEN) 
height.l=GetSystemMetrics_ (#SM_CYSCREEN)

If OpenWindow(hWnd, 10, 10, 450, 400, #PB_Window_SystemMenu, "System - Main Menu Screen")
wh=WindowHeight()
ww=WindowWidth()

neww=(width/2)-(ww/2)
newh=(height/2)-(wh/2)
MoveWindow(neww,newh)

If CreateStatusBar(0, WindowID())
  AddStatusBarField(100)
  AddStatusBarField(250)
  AddStatusBarField(100)
EndIf

Prog.s = "Power is in the programmer"
SetStatusBarText(0, 0, "Area 1", 0)
SetStatusBarText(0, 1, Prog, 0)
SetStatusBarText(0, 2, "Area 3", 0)

Repeat
Until WaitWindowEvent() = #PB_EventCloseWindow

EndIf

End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

Or even shorter code...

Code: Select all

If OpenWindow(0, 10, 10, 450, 400, #PB_Window_SystemMenu, "System - Main Menu Screen")

  MoveWindow((GetSystemMetrics_(#SM_CXSCREEN)-WindowWidth())/2,(GetSystemMetrics_(#SM_CYSCREEN)-WindowHeight())/2)
  
  If CreateStatusBar(0, WindowID())
    AddStatusBarField(100)
    AddStatusBarField(250)
    AddStatusBarField(100)
  EndIf
  
  Prog.s = "Power is in the programmer"
  SetStatusBarText(0, 0, "Area 1", 0)
  SetStatusBarText(0, 1, Prog, 0)
  SetStatusBarText(0, 2, "Area 3", 0)
  
  Repeat
  Until WaitWindowEvent() = #PB_EventCloseWindow
  
EndIf

End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Thanks for the 2 quick programs Paul.

I'll look them over to see if I can make a
genereic procedure from your info.

The real question was 'How do I extract the
RECT structure information out of the WinAPI
information?'

A lot of the Windows DLL's usestructures of
some type and being able to usethem in PureBasic
is almost a requirement.

Thanks again,
blueb
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.
Or even shorter code...

Code: Select all

If OpenWindow(0, 10, 10, 450, 400, #PB_Window_SystemMenu, "System - Main Menu Screen")

  MoveWindow((GetSystemMetrics_(#SM_CXSCREEN)-WindowWidth())/2,(GetSystemMetrics_(#SM_CYSCREEN)-WindowHeight())/2)
  
  If CreateStatusBar(0, WindowID())
    AddStatusBarField(100)
    AddStatusBarField(250)
    AddStatusBarField(100)
  EndIf
  
  Prog.s = "Power is in the programmer"
  SetStatusBarText(0, 0, "Area 1", 0)
  SetStatusBarText(0, 1, Prog, 0)
  SetStatusBarText(0, 2, "Area 3", 0)
  
  Repeat
  Until WaitWindowEvent() = #PB_EventCloseWindow
  
EndIf

End
And why using MoveWindow() when you can put it in OpenWindow() ? Even shorter :)

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.
Thanks for the 2 quick programs Paul.

I'll look them over to see if I can make a
genereic procedure from your info.

The real question was 'How do I extract the
RECT structure information out of the WinAPI
information?'

A lot of the Windows DLL's usestructures of
some type and being able to usethem in PureBasic
is almost a requirement.

Thanks again,
blueb

To use a structure:

MyRect.RECT ; Declare MyRect of rect type. Like 'struct RECT MyRect' in C :)
MyRect\top = 10 ...

That's your question ?

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shagwana.

I have just figured out how to do such a thing!!

As the answer has not been posted here, I thought i would enlighten you!. What follows is a little snippet of calculating the client area from the RECT structure.

Code: Select all

r.RECT
If GetClientRect_(pMainWindowHandle, @r.RECT)#FALSE      
x=r.RECT\right-r.RECT\left
y=r.RECT\bottom-r.RECT\top
EndIf
Hope this helps

http://www.sublimegames.com

Edited by - Shagwana on 11 December 2001 13:02:07
Post Reply