Jump to content

Liberty BASIC -- Locating Window Position to Center

- - - - -

  • Please log in to reply
No replies to this topic

#1
Irfan_A

Irfan_A

    Programmer

  • Members
  • PipPipPipPip
  • 186 posts
Outline : Objective-Requirement-Let's Coding-Mini Quiz

Objective:
Locating Window Position to Center
Requirement:
Liberty BASIC (i use Liberty BASIC 4.03), or you can use Just BASIC (FREE), and a little of mathematics skill
Let's Coding:
Firstly, we want to create window with width 400 pixels and height 300 pixels. The window type is window which open a basic window type. So, the code is like this.
     [COLOR=Teal]WindowWidth[/COLOR]=[COLOR=Red]400[/COLOR]:[COLOR=Teal]WindowHeight[/COLOR]=[COLOR=Red]300[/COLOR]
     [COLOR=Blue]open[/COLOR] [COLOR=Green]"Centering Window"[/COLOR][COLOR=Blue] for [/COLOR]window [COLOR=Blue]as[/COLOR] #main
     #main [COLOR=Green]"trapclose [quit]"[/COLOR]
     [COLOR=Blue]wait[/COLOR]
[quit]
     [COLOR=Blue]close[/COLOR] #main:[COLOR=Blue]end[/COLOR]
If we run code above, the window position is not in center. In this tutorial, we will centering it. Let us add two lines code below first line in the code above. So, the code is become like this.
     [COLOR=Teal]WindowWidth[/COLOR]=[COLOR=Red]400[/COLOR]:[COLOR=Teal]WindowHeight[/COLOR]=[COLOR=Red]300[/COLOR]
     [COLOR=Teal]UpperLeftX[/COLOR]=([COLOR=Teal]DisplayWidth[/COLOR]-[COLOR=Teal]WindowWidth[/COLOR])/[COLOR=Red]2[/COLOR]
     [COLOR=Teal]UpperLeftY[/COLOR]=([COLOR=Teal]DisplayHeight[/COLOR]-[COLOR=Teal]WindowHeight[/COLOR])/[COLOR=Red]2[/COLOR]
     ...
UpperLeft X is command to open window whose upper left corner is X pixels from the left of the display, and UpperLeftY is command to open window whose upper left corner Y pixels from the top of the display. DisplayWidth and DisplayHeight are width and height of your display screen in pixels.

Now, we use our mathematics skill. If we have window 400x300 and our display screen is 1024x768, with width and height respectively, in order to centering window we must use simple algebra to solve it.

  • Width. We have 1024 pixels for display screen and 400 pixels for window. Left and right display screen have the same size, and there is our window on its centre. So, our window should be locating at (1024-400)/2 pixels from the left of the display.
  • Height. We have 768 pixels for display screen and 300 pixels for window. Top and bottom display screen have the same size, and there is our window on its centre. So, our window should be locating at (768-300)/2 pixels from the top of the display.
That's easy, isn't it ?
For complete code is given below with more features to print result to mainwindow.
[COLOR=Teal]     WindowWidth[/COLOR]=[COLOR=Red]400[/COLOR]:[COLOR=Teal]WindowHeight[/COLOR]=[COLOR=Red]300[/COLOR]
     [COLOR=Teal]UpperLeftX[/COLOR]=([COLOR=Teal]DisplayWidth[/COLOR]-[COLOR=Teal]WindowWidth[/COLOR])/[COLOR=Red]2[/COLOR]
     [COLOR=Teal]UpperLeftY[/COLOR]=([COLOR=Teal]DisplayHeight[/COLOR]-[COLOR=Teal]WindowHeight[/COLOR])/[COLOR=Red]2[/COLOR]
     [COLOR=Blue]open[/COLOR] [COLOR=Green]"Centering Window"[/COLOR][COLOR=Blue] for [/COLOR]window [COLOR=Blue]as[/COLOR] #main
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"Window Width   = "[/COLOR];[COLOR=Teal]WindowWidth[/COLOR]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"Window Height  = "[/COLOR];[COLOR=Teal]WindowHeight[/COLOR]
     [COLOR=Blue]print[/COLOR][COLOR=Green] "======================="[/COLOR]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"Display Width    = "[/COLOR];[COLOR=Teal]DisplayWidth[/COLOR]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"Display Height   = "[/COLOR];[COLOR=Teal]DisplayHeight[/COLOR]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"======================="[/COLOR]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"UpperLeftX  = "[/COLOR];[COLOR=Teal]UpperLeftX[/COLOR]
     [COLOR=Blue]print[/COLOR] [COLOR=Green]"UpperLeftY  = "[/COLOR];[COLOR=Teal]UpperLeftY[/COLOR]
     #main [COLOR=Green]"trapclose [quit]"[/COLOR]
     [COLOR=Blue]wait[/COLOR]
[quit]
     [COLOR=Blue]close[/COLOR] #main:[COLOR=Blue]end[/COLOR]
Mini Quiz:
For default, Liberty BASIC makes 320x360 for window width and window height respectively and our display screen is set up to 1024x768. Please centering it based on above explanation and find out UpperLeftX and UpperLeftY values!
Answer: UpperLeftX=352, UpperLeftY=204

Edited by Irfan_A, 29 June 2010 - 08:53 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users