PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: V/H center  (Read 1013 times)

James Fuller

  • FireFly3 Registered User
  • Senior Member
  • *
  • Posts: 277
  • FF3 User
V/H center
« on: October 30, 2016, 03:54:42 PM »

Jose,
  Are there functions to Vertically or Horizontally center a control on a CWindow? If not would you add them?

I had them in my PB library but not sure how to do it with a dpi aware CWindow.
I looked at the AfxCenterWindow code but ....

There are times I just want to call a VCenter or HCenter not the both that AfxCenterWindow provides.

Thank you,

James
Logged

Josť Roca

  • Moderator
  • Master Member
  • *****
  • Posts: 3103
    • Jos
Re: V/H center
« Reply #1 on: October 30, 2016, 06:22:51 PM »

In the same way as if it had been created by other means.

Code: [Select]
SUB AfxCenterControlH (BYVAL hCtrl AS HWND)
   DIM rc AS RECT, rcParent AS RECT
   ' // Get the coordinates of the control
   GetWindowRect hCtrl, @rc
   DIM nWidthControl AS LONG = rc.Right - rc.Left
   DIM nHeightControl AS LONG = rc.Bottom - rc.Top
   ' // Get the coordinates of the parent window
   DIM hParent AS HWND = GetParent(hCtrl)
   GetClientRect hParent, @rcParent
   DIM nWidthParent AS LONG = rcParent.Right - rcParent.Left
   ' // Calculate the x coordinate to center the control horizontally
   DIM x AS LONG = (nWidthParent - nWidthControl) \ 2
   ' // Convert the y coordinate of the control to client area coordinate
   DIM pt AS POINT
   pt.y = rc.Top
   ScreenToClient hParent, @pt
   MoveWindow hCtrl, x, pt.y, nWidthControl, nHeightControl, CTRUE
END SUB

SUB AfxCenterControlV (BYVAL hCtrl AS HWND)
   DIM rc AS RECT, rcParent AS RECT
   ' // Get the coordinates of the control
   GetWindowRect hCtrl, @rc
   DIM nWidthControl AS LONG = rc.Right - rc.Left
   DIM nHeightControl AS LONG = rc.Bottom - rc.Top
   ' // Get the coordinates of the parent window
   DIM hParent AS HWND = GetParent(hCtrl)
   GetClientRect hParent, @rcParent
   DIM nHeightParent AS LONG = rcParent.Bottom - rcParent.Top
   ' // Calculate the x coordinate to center the control vertically
   DIM y AS LONG = (nHeightParent - nHeightControl) \ 2
   ' // Convert the x coordinate of the control to client area coordinate
   DIM pt AS POINT
   pt.x = rc.Left
   ScreenToClient hParent, @pt
   MoveWindow hCtrl, pt.x + (nWidthControl \ 2), y, nWidthControl, nHeightControl, CTRUE
END SUB

James Fuller

  • FireFly3 Registered User
  • Senior Member
  • *
  • Posts: 277
  • FF3 User
Re: V/H center
« Reply #2 on: October 30, 2016, 07:36:36 PM »

Thank you sir!

James
Logged