TIPCORNER - Window Placement Techniques

Home

Window Placement Techniques
Listview Report by Brent Thorn
Enhancing Liberty Basic [Array Handling]
The Road to Release
Formatted ListBox

 

This month's tip corner is focusing on the positioning of windows when they are opened. Many people ask often: "How can I open a window centered on the screen?", or "How can I open a window full screen?". Luckly Liberty Basic has several system variables that are designed to help with these problems. These are variables are our allies in our quest. Let's get to know them:

UpperLeftX and UpperLeftY both control where the top left corner of the window will be placed when it is opened.

DisplayWidth and DisplayHeight return the actual display size in pixels, this is the total area of your computers screen.

WindowWidth and WindowHeight you probably already use. Set these with the width and height of the window you
want to open.

With these six variables we can explicitly and precisely position a window on the computer display. If we want to open a 400x260 window that is ten pixels down from the top of the display and ten pixels from the left of the display, then we simply set up our variables to do just this:

WindowWidth = 400
WindowHeight = 260
UpperLeftX = 10
UpperLeftY = 10
Open "Test" For Window As #main

But what if we want to center the window on the screen? Well we know the width and height of our window. We can get the actual width and height of the display. In the X direction (which is width) we would find the UpperLeftX by subtracting the window width from the display width and then dividing the result by 2 (that get the center). If we do this also for height (in the Y direction) we will know the X and Y coordinates to locate our window at. In Liberty Basic code it looks like the following:

WindowWidth = 400
WindowHeight = 260
UpperLeftX = Int((DisplayWidth-WindowWidth)/2)
UpperLeftY = Int((DisplayHeight-WindowHeight)/2)
Open "Test" For Window As #main 

So that addresses centering a window, but what about opening it full screen? Well the obvious answer it to set the Window Width = Display Width and the Window Height = Display Height. This works, but it causes the window to obstruct the Windows Task Bar. This can be overcome by reducing the Window Height by 20 or so and then forcing the window into the upper left corner like this:

WindowWidth = DisplayWidth
WindowHeight = DisplayHeight - 20
UpperLeftX = 0
UpperLeftY = 0
Open "Test" For Window As #main 

Which is not a bad work around, but there is a much better method that is much more flexible. It involves calling a Windows API function. The function is ShowWindow. There are several states that you can change to. Below is a list and the numeric value of each:

SW_HIDE = 0
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9 

This is a system value, which is passed as the stateflag in the function. You also must pass the handle of the window you want to change. Here is the function from the 32bit library:

CallDLL #user32, "ShowWindow", _
     hWnd As long, _
     stateflag As long, _
     result As boolean

Finally here is an example program (it will run in LB3) demonstrating the API function maximizing a window that Liberty Basic created.

WindowWidth = 400
WindowHeight = 260
UpperLeftX = Int((DisplayWidth-WindowWidth)/2)
UpperLeftY = Int((DisplayHeight-WindowHeight)/2)
Open "Test" For Window As #main
Print #main, "trapclose [quit]"
Print #main, "font ms_sans_serif 10"
hWnd = hWnd(#main)
stateflag = 3 'SW_MAXIMIZE
CallDLL #user32, "ShowWindow", _
     hWnd As long, _
     stateflag As long, _
     result As boolean
 Wait
 [quit]
     Close #main
     End

Use the other values mentioned above in the dll call to get different results, such as a minimized window or a hidden window. Have fun with it.

By Brad Moore, July 2002


Home

Window Placement Techniques
Listview Report by Brent Thorn
Enhancing Liberty Basic [Array Handling]
The Road to Release
Formatted ListBox