Results 1 to 8 of 8

Thread: Obtaining Form Height before Form is visible?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    Obtaining Form Height before Form is visible?

    Based on an .ini file, a subsidiary form in a VB6 project is either created as maximized or normal.

    We'd like to position its objects based on the form's height, during Form Load before it's visible. (The form becomes visible later in the code.)

    But we've found the height of the form is not adjusted after setting the windowstate to maximum. The value is the full height only after making the Form visible.

    Example:
    Code:
    Me.Height = screen2h   'value read in
    If screen2Max  Then
      Me.WindowState = vbMaximized
    Else
      Me.WindowState = vbNormal
    End If
    msgbox "Height is " & Me.Height
    The height is correct only if we have the following after setting the form's WindowState to max:
    Code:
    Me.Visible = True
    Me.Visible = False  'turn off bec we don't want the user to see this form yet
    This makes sense, I guess bec the form doesn't "exist" before visibility.

    Is there another way to find the maximized height without flashing the form before the user?

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,666

    Re: Obtaining Form Height before Form is visible?

    First, form's visibility has nothing to do.

    About "to position its objects based on the form's height", if the form is maximized you also would have to position the object based on the height of the full screen.

    You usually do that in the Form_Resize event, according to form's ScaleWidth and ScaleHeight.

    If you save the WindowState (whether the form has been closed being maximized or normal) and the form size when it is not maximized, on Form_Load you should set the size(Width and Height) before setting WindowState.

    About how to get the correct non-maximized size before closing the form, to be able to save the correct values when the form is closed being maximized, you could first set Visible to False and then WindowState to vbNormal. And then save the Width and Height.

    Or, if you want to go API, you can use GetWindowPlacement and SetWindowPlacement.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    Re: Obtaining Form Height before Form is visible?

    Thx for your reply. I don't think it answers the question, so I'll rephrase and clarify.

    In the .ini file, we save the height and width for a vbNormal setting. We also store if the window was maximized or normal.

    For a new session, if the window is to be Normal, of course we use the previously stored h+w values.

    However, if the window is to be Maximized, the previous max values may not apply: in this new session, the user may be running the project on a different monitor that has different dimensions.

    Thus this code ...
    Code:
     If screen2Max Then
          Me.WindowState = vbMaximized
          msgbox "Height is " & Me.Height
        Else
          Me.WindowState = vbNormal
        End If
    ... fails to report the correct, maximized height. Only by making the Form Visible do we get a correct value:
    Code:
     If screen2Max Then
          Me.WindowState = vbMaximized
          Me.Visible = True
          msgbox "Height is " & Me.Height
        Else
          ...
    But we don't want to make the form visible until all objects are placed correctly; said placement depends on Me.Height. Is there another way to obtain what Me.Height will be before making the form visible? Again, the user may be on a different monitor.

  4. #4
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,619

    Question Re: Obtaining Form Height before Form is visible?

    Why don't you save the width/height regardless whether the form is maximized or not? Also it's better to save that in the registry rather than an "INI" file.

  5. #5
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    616

    Re: Obtaining Form Height before Form is visible?

    You do know Screen.Width exists, right? Screen.Height also. If the form is to be maximised you can get a basic idea of what the screen size is regardless of the resolution and adjust things accordingly, but you won't need screen width or height to set form width or height because you are setting the form as maximised.

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,666

    Re: Obtaining Form Height before Form is visible?

    Usually you use the Form_Resize event to place the controls. And use form's ScaleHeight and ScaleWidth.

    I think I recall that at that event the form was still not visible, but it seems it is already visible now in current Windows.

    To get the height of the screen, you can use Screen.Height, and to get its usable height that is the height that a form will have maximized, you can use SystemParametersInfo with SPI_GETWORKAREA

    If you want to the program to be ready to handle multiple monitors, you need other API: GetMonitorInfo

  7. #7
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,619

    Re: Obtaining Form Height before Form is visible?

    Screen.Height is likely to return incorrect information if your app isn't manifested as DPI aware.

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,666

    Re: Obtaining Form Height before Form is visible?

    Screen.Height is likely to return incorrect information if your app isn't manifested as DPI aware.
    Hummm, yes and no. Rather no, because everything will be scaled to 96 DPI, also your own API calls and VB6 sizes.
    So if your program is not DPI aware, it lives in a virtual world where there is no DPI issues, and everything related to the subject is automatically handled by the OS.

    If you want to know the true screen size that other programs (that are DPI aware) see, so yes, the screen size that you get will be "incorrect".

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width