CENTER statement

Purpose: CENTER displays the control centered on screen, on a window or in a position relative to the vertical and/or horizontal midpoint of a window. In calculating the vertical component of CENTER, the area of the system TaskBar is taken into account.


Syntax 1:

 CENTER(HndlWnd AS HWND)

Parameters:

  • Data type: HWND
    HndlWnd The handle to control to be centered. Parentheses are optional.

Syntax 2:

 CENTER(HndlWnd AS HWND [, XYhwnd AS HWND])

Parameters:

  • Data type: HWND
    HndlWnd The handle to control to be centered. Parentheses are optional.
  • Data type: HWND
    XYhwnd [OPTIONAL] The handle to control on which HndlWnd is to be centered.

Syntax 3:

 CENTER(HndlWnd AS HWND [, X_hwnd AS HWND] [, Y_hwnd AS HWND])

Parameters:

  • Data type: HWND
    HndlWnd The handle to control to be centered. Parentheses are optional.
  • Data type: HWND
    X_hwnd [OPTIONAL] first handle to control on which HndlWnd is to be horizontally centered.
  • Data type: HWND
    Y_hwnd [OPTIONAL] second handle to control on which HndlWnd is to be vertically centered.

Remarks:
Most times, we want to center a form on the screen,


  GUI "Center1"
  
  SUB FORMLOAD
    LOCAL Form1 AS CONTROL
 
    Form1 = BCX_FORM("Form1", 10, 10, 150, 80)
 
    CENTER(Form1) 'Center Form1 On Screen 
 
    CALL SHOW(Form1)
  END SUB
  
  BEGIN EVENTS
  END EVENTS

but sometimes we want to center one form on another form,


 GUI "Form1_On_Form2"
   
 SUB FORMLOAD
   LOCAL Form1 AS CONTROL
   LOCAL Form2 AS CONTROL
  
   Form1 = BCX_FORM("Form1", 10, 10, 120, 120)
   Form2 = BCX_FORM("Form2", 100, 100, 200, 200)
  
   CENTER(Form1, Form2) 'Center Form1 On Form2 
  
   CALL SHOW(Form2)
   CALL SHOW(Form1)
 END SUB
   
 BEGIN EVENTS
 END EVENTS
 

or place a button in the center of a form.


 GUI "Test"

 SUB FORMLOAD
  DIM Form AS CONTROL
  DIM Butt AS CONTROL
  Form = BCX_FORM("Test")
  Butt = BCX_BUTTON("Button", Form, 123, 0, 0)
  CENTER(Butt, Form) 'CENTER Button on Form
  SHOW(Form)
 END SUB

 BEGIN EVENTS
 END EVENTS

And sometimes we want to use our current Y position with a new X position,


 GUI "Test_New_Center"
 
 SUB FORMLOAD
   LOCAL Form1 AS CONTROL
   LOCAL Form2 AS CONTROL
 
   Form1 = BCX_FORM("Form1", 100, 10, 80, 80)
   Form2 = BCX_FORM("Form2", 385, 165, 80, 80)
 
   CENTER(Form1, Form2, Form1)
 
   CALL SHOW(Form1)
   CALL SHOW(Form2)
 
 END SUB
 
 BEGIN EVENTS
 END EVENTS

and sometimes (albeit rarely), we may want to center between two forms.


 GUI "BetweenTwo"
 
 SUB FORMLOAD
   LOCAL Form1 AS CONTROL
   LOCAL Form2 AS CONTROL
   LOCAL Form3 AS CONTROL
 
   Form1 = BCX_FORM("Form1", 100, 10, 80, 80)
   Form2 = BCX_FORM("Form2", 385, 165, 80, 80)
   Form3 = BCX_FORM("Form3",  10, 200)
 
   CENTER(Form3,Form1,Form2) 'Center Form3 On Form1(x) AND Form2(y) 
 
   CALL SHOW(Form1)
   CALL SHOW(Form2)
   CALL SHOW(Form3)
 END SUB
 
 BEGIN EVENTS
 END EVENTS

Although it does not use the CENTER function, this example which snap-centers a window along the four edges of the display, may be useful.

 
 GUI "Form Centering Demo"
 
 ENUM
   ID_Button
 END ENUM>
 
 SUB FORMLOAD
   GLOBAL Form1 AS HWND
   GLOBAL Butt1 AS HWND
   Form1 = BCX_FORM ("New Ways To Center Your Form", 0, 0, 180, 65)
   Butt1 = BCX_BUTTON("Start Demonstration", Form1, ID_Button, 50, 25, 60, 14)
   CENTER(Form1)
   SHOW (Form1)
 END SUB
 
 BEGIN EVENTS
   SELECT CASE CBMSG
   CASE WM_COMMAND
 
     SELECT CASE CBCTL
     CASE ID_Button
       RunDemo()
 
     END SELECT
   END SELECT
 END EVENTS
 
 SUB RunDemo
   EnableWindow(Butt1,FALSE)
   FOR INTEGER i = 1 TO 3
     CENTER_RIGHT  (Form1) : UpdateWindow(Form1) : SLEEP(500)
     CENTER_TOP    (Form1) : UpdateWindow(Form1) : SLEEP(500)
     CENTER_LEFT   (Form1) : UpdateWindow(Form1) : SLEEP(500)
     CENTER_BOTTOM(Form1) : UpdateWindow(Form1) : SLEEP(500)
     CENTER        (Form1) : UpdateWindow(Form1) : SLEEP(500)
   NEXT
   EnableWindow(Butt1,TRUE)
 END SUB
 
 SUB CENTER_TOP (HndlWnd AS HWND)
   DIM Rect AS RECT, MiddleX, w
   GetWindowRect(HndlWnd,&Rect)
   w = Rect.right-Rect.left
   MiddleX = (GetSystemMetrics(SM_CXSCREEN)*0.5) - (w*0.5)
   SetWindowPos(HndlWnd,0,MiddleX,0,0,0,SWP_NOSIZE)
 END SUB
 
 SUB CENTER_BOTTOM (HndlWnd AS HWND)
   DIM Rect AS RECT, MiddleX, BottomY, w, h
   GetWindowRect(HndlWnd,&Rect)
   w = Rect.right-Rect.left
   h = Rect.bottom-Rect.top
   MiddleX = (GetSystemMetrics(SM_CXSCREEN)*0.5) - (w * 0.5)
   BottomY = GetSystemMetrics(SM_CYSCREEN) - (h + 31)
   SetWindowPos(HndlWnd,0,MiddleX,BottomY,0,0,SWP_NOSIZE)
 END SUB
 
 SUB CENTER_LEFT (HndlWnd AS HWND)
   DIM Rect AS RECT, MiddleY, w, h
   GetWindowRect(HndlWnd,&Rect)
   h = Rect.bottom-Rect.top
   MiddleY = (GetSystemMetrics(SM_CYSCREEN)*0.5) - (h * 0.5)
   SetWindowPos(HndlWnd,0,0,MiddleY,0,0,SWP_NOSIZE)
 END SUB
 
 SUB CENTER_RIGHT (HndlWnd AS HWND)
   DIM Rect AS RECT, MiddleY, x,h
   GetWindowRect(HndlWnd,&Rect)
   h = Rect.bottom-Rect.top
   x = GetSystemMetrics(SM_CXSCREEN) - (Rect.right-Rect.left)
   MiddleY = (GetSystemMetrics(SM_CYSCREEN)*0.5) - (h * 0.5)
   SetWindowPos(HndlWnd,0,x,MiddleY,0,0,SWP_NOSIZE)
 END SUB

The default initial location, on invocation of a MSGBOX, is at the center of the monitor. The following code shows how to center a MSGBOX on a form.


 GUI "Form1",PIXELS
 
 CONST Btn1_Style    = WS_CHILD|WS_VISIBLE|WS_TABSTOP
 CONST Btn1_ExtStyle = WS_EX_STATICEDGE
 
 ENUM
  ID_Btn1
 END ENUM
 
 GLOBAL Form1 AS HWND
 GLOBAL hBtn1 AS CONTROL
 GLOBAL CmDlgHook AS HHOOK
 
 SUB FORMLOAD()
  Form1 = BCX_FORM("Form1",  200, 200, 400, 300)
  hBtn1 = BCX_BUTTON("Button",Form1,ID_Btn1, 30, 110, 130, 50, Btn1_Style, Btn1_ExtStyle)
  SHOW(Form1)
 END SUB
 
 BEGIN EVENTS
  SELECT CASE CBMSG
  CASE WM_COMMAND
   SELECT CASE CBCTL
   CASE ID_Btn1
    MSGBOX "This is MSGBOX default position at the center of the monitor."
    CALL SetOurHook
    MSGBOX "This is MSGBOX hooked to the center of Form1."
   END SELECT
  END SELECT
 END EVENTS
 
 SUB SetOurHook
  Hook=SetWindowsHookEx(WH_CBT, MBProc,(HINSTANCE)NULL,GetCurrentThreadId())
 END SUB
 
 FUNCTION MBProc (Msg, wParam AS WPARAM, lParam AS LPARAM) AS LRESULT CALLBACK
  GLOBAL Hook AS HHOOK
  '**************************************************************************** 
  ' CALL this HOOK once before each MSGBOX statement to CENTER IT IN A FORM 
  '**************************************************************************** 
  IF Msg = HCBT_ACTIVATE THEN
   CENTER((HWND)wParam,Form1,Form1)
 
   SetWindowPos((HWND)wParam, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE OR SWP_NOMOVE)
   UnhookWindowsHookEx(Hook)
  END IF
  FUNCTION = 0
 END FUNCTION