Code Comments
Programming Forum and web based access to our favorite programming groups.I've been using code like that below to resize forms. However, I just noticed something that leads me to believe that there must be a better way. I usually run with a resolution of 1280 x 1024. The Form I am using, created at 1280 x 1024, has a Height of 9210 and a Width of 8910. There's a Text box with Top = 7800, Left = 75, Height = 855, and Width = 6735. If I run the Form, then, while the program is still running, change to 640 x 480, the code below gets the job done. However, if I first change to 640 x 480, then run the program, the Text box is not displayed. Do I have to run MoveControls somewhere in the Load, or another, event to get the proper result when the Form is first displayed? If so, where? If not, is there a better solution? Here's the code. In the Form, insert: Private ControlPropertiesArray() As ControlProperties Private Sub Form_Load() With Me ReDim ControlPropertiesArray(0 To .Controls.Count - 1) .Top = (Screen.Height / 2) - (.Height / 2) .Left = (Screen.Width / 2) - (.Left / 2) End With InitializeControlPropertiesArray Me, ControlPropertiesArray() End Sub Private Sub Form_Resize() MoveControls Me, ControlPropertiesArray() End Sub In a module, insert: Public Type ControlProperties Width As Single Height As Single Top As Single Left As Single End Type Public Sub InitializeControlPropertiesArray(AForm As Form, ControlPropertiesArray() As ControlProperties) Dim ctrl As Control Dim i As Integer Dim sngWidth As Single Dim sngHeight As Single On Error Resume Next With AForm sngWidth = .ScaleWidth sngHeight = .ScaleHeight For i = 0 To .Controls.Count - 1 Set ctrl = .Controls(i) With ctrl If TypeOf ctrl Is Timer Then ' ElseIf TypeOf ctrl Is CommonDialog Then Else With ControlPropertiesArray(i) .Width = ctrl.Width / sngWidth .Height = ctrl.Height / sngHeight .Left = ctrl.Left / sngWidth .Top = ctrl.Top / sngHeight End With End If End With Next i End With On Error GoTo 0 Set ctrl = Nothing End Sub Public Sub MoveControls(AForm As Form, ControlPropertiesArray() As ControlProperties) Dim ctrl As Control Dim i As Integer Dim sngWidth As Single Dim sngHeight As Single On Error Resume Next With AForm sngWidth = .ScaleWidth sngHeight = .ScaleHeight For i = 0 To .Controls.Count - 1 Set ctrl = .Controls(i) If TypeOf ctrl Is Timer Then ' ElseIf TypeOf ctrl Is CommonDialog Then Else With ControlPropertiesArray(i) ctrl.Move .Left * sngWidth, _ .Top * sngHeight, _ .Width * sngWidth, _ .Height * sngHeight End With End If Next i End With On Error GoTo 0 Set ctrl = Nothing End Sub
Post Follow-up to this message"Howard Kaikow" <kaikow@standards.com> wrote in message news:%23ssfU9OJGHA.2912@tk2msftngp13.phx.gbl... > I've been using code like that below to resize forms. > > However, I just noticed something that leads me to believe that there must > be a better way. > > I usually run with a resolution of 1280 x 1024. > > The Form I am using, created at 1280 x 1024, has a Height of 9210 and a > Width of 8910. > There's a Text box with Top = 7800, Left = 75, Height = 855, and Width = > 6735. > > If I run the Form, then, while the program is still running, change to 640 x > 480, the code below gets the job done. > > However, if I first change to 640 x 480, then run the program, the Text box > is not displayed. > > Do I have to run MoveControls somewhere in the Load, or another, event to > get the proper result when the Form is first displayed? > If so, where? > If not, is there a better solution? > > Here's the code. > > In the Form, insert: > > Private ControlPropertiesArray() As ControlProperties > > Private Sub Form_Load() > With Me > ReDim ControlPropertiesArray(0 To .Controls.Count - 1) > .Top = (Screen.Height / 2) - (.Height / 2) > .Left = (Screen.Width / 2) - (.Left / 2) > End With > InitializeControlPropertiesArray Me, ControlPropertiesArray() > End Sub > > Private Sub Form_Resize() > MoveControls Me, ControlPropertiesArray() > End Sub > > In a module, insert: > > Public Type ControlProperties > Width As Single > Height As Single > Top As Single > Left As Single > End Type > > Public Sub InitializeControlPropertiesArray(AForm As Form, > ControlPropertiesArray() As ControlProperties) > Dim ctrl As Control > Dim i As Integer > Dim sngWidth As Single > Dim sngHeight As Single > > On Error Resume Next > With AForm > sngWidth = .ScaleWidth > sngHeight = .ScaleHeight > For i = 0 To .Controls.Count - 1 > Set ctrl = .Controls(i) > With ctrl > If TypeOf ctrl Is Timer Then > ' ElseIf TypeOf ctrl Is CommonDialog Then > Else > With ControlPropertiesArray(i) > .Width = ctrl.Width / sngWidth > .Height = ctrl.Height / sngHeight > .Left = ctrl.Left / sngWidth > .Top = ctrl.Top / sngHeight > End With > End If > End With > Next i > End With > On Error GoTo 0 > Set ctrl = Nothing > End Sub > > Public Sub MoveControls(AForm As Form, ControlPropertiesArray() As > ControlProperties) > Dim ctrl As Control > Dim i As Integer > Dim sngWidth As Single > Dim sngHeight As Single > > On Error Resume Next > With AForm > sngWidth = .ScaleWidth > sngHeight = .ScaleHeight > For i = 0 To .Controls.Count - 1 > Set ctrl = .Controls(i) > If TypeOf ctrl Is Timer Then > ' ElseIf TypeOf ctrl Is CommonDialog Then > Else > With ControlPropertiesArray(i) > ctrl.Move .Left * sngWidth, _ > .Top * sngHeight, _ > .Width * sngWidth, _ > .Height * sngHeight > End With > End If > Next i > End With > On Error GoTo 0 > Set ctrl = Nothing > End Sub > > As far as centering a form here is some old code I found that I include in all my projects. It seems to work OK but since I wrote it sometime in the late '90s there very well could be a better way of doing it. ' Find location of taskbar and\or Microsoft Office shortcut bar Declare Function GetSystemMetrics Lib "USER32" (ByVal nIndex As Long) As Long ' ScreenCenter Private Const SM_CXFULLSCREEN = 16 Private Const SM_CYFULLSCREEN = 17 Public Sub ScreenCenter(hForm As Form) ' Takes in a handle to a form and centers the form within the screen ' taking into account the taskbar and\or Office shortcut bar Dim lLeft As Long, lTop As Long On Error GoTo ScreenCenterError lLeft = (Screen.TwipsPerPixelX * (GetSystemMetrics(SM_CXFULLSCREEN) / 2)) - (hForm.Width / 2) lTop = (Screen.TwipsPerPixelY * (GetSystemMetrics(SM_CYFULLSCREEN) / 2)) - (hForm.Height / 2) hForm.Move lLeft, lTop Exit Sub ScreenCenterError: End Sub Jim Edgar
Post Follow-up to this messagePrivate Sub Form_Resize() If Me.WindowState = 0 Or Me.WindowState = 2 Then Form1.Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2.5 End If End Sub
Post Follow-up to this message> Private Sub Form_Resize() > If Me.WindowState = 0 Or Me.WindowState = 2 Then > Form1.Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2.5 > End If > End Sub 2.5 ??? Rick
Post Follow-up to this message"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote > > 2.5 ??? Yeah, I agree, direct center is not as visually pleasent as slightly above c enter, but in this case, I think the code is meant to offset the Taskbar. It would n't work on my system though because I have always kept the taskbar on the right side of the screen! <g> LFS
Post Follow-up to this message> > > Private Sub Form_Resize() > > Yeah, I agree, direct center is not as visually pleasent as slightly above center, > but in this case, I think the code is meant to offset the Taskbar. It wouldn't > work on my system though because I have always kept the taskbar on the > right side of the screen! I guess one could use the following code as a general approach to center forms on the Desktop's work area... Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function MoveWindow Lib "user32" _ (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, _ ByVal nWidth As Long, ByVal nHeight As Long, _ ByVal bRepaint As Long) As Long Private Const SPI_GETWORKAREA = 48 Private Declare Function SystemParametersInfo Lib "user32" _ Alias "SystemParametersInfoA" _ (ByVal uAction As Long, ByVal uParam As Long, _ lpvParam As Any, ByVal fuWinIni As Long) As Long Sub Center(FormToCenter As Form) Dim rcWork As RECT SystemParametersInfo SPI_GETWORKAREA, 0, rcWork, 0 With rcWork .Left = .Left * Screen.TwipsPerPixelX .Top = .Top * Screen.TwipsPerPixelY .Right = .Right * Screen.TwipsPerPixelX .Bottom = .Bottom * Screen.TwipsPerPixelY End With With FormToCenter .Move (rcWork.Left + rcWork.Right - .Width) / 2, _ (rcWork.Top + rcWork.Bottom - .Height) / 2 End With End Sub As set up, all one would have to do is execute Center Me in order to center the form on the screen. If more than one form will need to be centered, the above code could be moved to a BAS Module. Rick
Post Follow-up to this message"Rick Rothstein [MVP - Visual Basic]" <rickNOSPAMnews@NOSPAMcomcast.net> wrote in message news:uHu8vKRJGHA.604@TK2MSFTNGP14.phx.gbl... 2.5 above > center, > wouldn't > > I guess one could use the following code as a general approach to center > forms on the Desktop's work area... > > Private Type RECT > Left As Long > Top As Long > Right As Long > Bottom As Long > End Type > > Private Declare Function MoveWindow Lib "user32" _ > (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, _ > ByVal nWidth As Long, ByVal nHeight As Long, _ > ByVal bRepaint As Long) As Long > > Private Const SPI_GETWORKAREA = 48 > > Private Declare Function SystemParametersInfo Lib "user32" _ > Alias "SystemParametersInfoA" _ > (ByVal uAction As Long, ByVal uParam As Long, _ > lpvParam As Any, ByVal fuWinIni As Long) As Long > > Sub Center(FormToCenter As Form) > Dim rcWork As RECT > SystemParametersInfo SPI_GETWORKAREA, 0, rcWork, 0 > With rcWork > .Left = .Left * Screen.TwipsPerPixelX > .Top = .Top * Screen.TwipsPerPixelY > .Right = .Right * Screen.TwipsPerPixelX > .Bottom = .Bottom * Screen.TwipsPerPixelY > End With > With FormToCenter > .Move (rcWork.Left + rcWork.Right - .Width) / 2, _ > (rcWork.Top + rcWork.Bottom - .Height) / 2 > End With > End Sub > > As set up, all one would have to do is execute > > Center Me > > in order to center the form on the screen. If more than one form will need > to be centered, the above code could be moved to a BAS Module. > > Rick > > I thought someone would post an update to the old code I use. Anyway, why do you declare MoveWindow() and then not use it? It looks like MoveWindow() is a bit more tedious to use but would it be more efficient? Jim Edgar
Post Follow-up to this messageThis thread has gone way off my intended topic, so I have started a separate thread.
Post Follow-up to this message> I thought someone would post an update to the old code > I use. Anyway, why do you declare MoveWindow() and > then not use it? I cannibalized the code for this posting from another routine I found in my archives and I apparently missed that I didn't retain a call to the MoveWindows function after my modification. Rick
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.