Results 1 to 8 of 8

Thread: How to make MDI Form fix with user resolution

  1. #1

    Thread Starter
    Addicted Member girl81's Avatar
    Join Date
    Apr 2006
    Location
    Malaysia
    Posts
    211

    How to make MDI Form fix with user resolution

    hi all..me again.. still regarding with MDI Form

    how can i make or there is any codes for MDI Form(Maximized) fix with user screen resolution.

    So if i run my system on user computer..it size fix based on user screen resolution

    tq
    Last edited by girl81; Jun 23rd, 2006 at 02:40 AM.

  2. #2
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: How to make MDI Form fix with user resolution

    Use MDI Windows Status as maxmized In Property

  3. #3
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: How to make MDI Form fix with user resolution

    How to make resolution free form??

    Solution is
    People often ask how can they make their form look the same in different sizes/resolutions, or change the resolution to suit their form.

    Changing the resolution is a bad idea for several reasons:
    • Anything else on the users screen (the taskbar, or other programs) while your program is running will look wrong.
    • If your program doesn't set the resolution back properly (or crashes before it has the chance) then the user will be stuck at the 'wrong' resolution.
    • When the resolution is changed, any icons on the desktop are moved - but will not be moved back if you restore the previous resolution.
    • The resolution you want may not be available, or may look odd (which is often true for LCD monitors).
    • The person running the program may not have permission to change the resolution (if they are not an Admin user on the PC).

    Ok, so how do I make my Form look the same at different sizes?

    Every Form in a VB project has Resize event, which is called whenever the Height and/or Width of the Form are changed.

    During the Form Resize event, you must also change the size (Height and Width) and position (Top and Left) of all the controls (command buttons, lines, text boxes etc) to make them look like they're in the right place.

    NOTE: All the Codes posted here are to be placed under Form's Resize event


    What is the difference between Height and ScaleHeight (or Width and ScaleWidth) properties?

    1 - The ScaleHeight property is only applicable for a Form, and it is the height of the client portion of the form (not counting the height of the TitleBar, MenuBar or the Border of the form). So basically the ScaleHeight property should be used to position the controls within the form.

    2 - All controls and Forms have a Height property and is for postioning/sizing the form/control relative to others. It is the Actual Size of Controls and the Form (including the Borders and the TitleBar) on the Screen.

    The same applies to the Width and ScaleWidth properties.


    Resizing Controls

    The basic logic is to keep the Size_of_the_Control proportional to the ScaleSize_of_the_Form

    Loop thru all of the controls within the Form, and set the ratio of the Previous_Size_of_Control to Previous_ScaleSize_of_Form.

    Eg: (previous_ctrl_height / previous_form_scaleheight) = (resized_ctrl_height / resized_form_scaleheight)

    visual basic code:

    VB Code:
    1. For Each tmpControl In Me.Controls
    2.        
    3.         tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth
    4.         tmpControl.Top = tmpControl.Top / PrevResizeY * Me.ScaleHeight
    5.         tmpControl.Width = tmpControl.Width / PrevResizeX * Me.ScaleWidth
    6.         tmpControl.Height = tmpControl.Height / PrevResizeY * Me.ScaleHeight
    7.        
    8.     Next tmpControl
    Relocating Controls

    Sometimes you will just want to relocate the control instead of resizing it, for example you cannot set the Height of a ComboBox at run-time, and making its width too large also makes the look of your form bad.

    The basic logic is similar to Resizing form except that you need not to change .Width and .Height properties of the control.

    visual basic code:

    VB Code:
    1. For Each tmpControl In Me.Controls
    2.  
    3.         tmpControl.Left = tmpControl.Left / PrevResizeX * Me.ScaleWidth
    4.         tmpControl.Top = tmpControl.Top / PrevResizeY * Me.ScaleHeight
    5.  
    6.     Next tmpControl

    Fixing the Form Border

    Sometimes, it becomes necessary to fix the Form's Border so that user gets no liberty to fiddle with the beauty of the application. For this, just set the Form's MaxButton property to False.

    But then you cannot change the WindowState to Maximized. So use this:
    visual basic code:

    VB Code:
    1. If Me.Height <> 7200 Or Me.Width <> 9600 Then
    2.         Me.Height = 7200
    3.         Me.Width = 9600
    4.     End If

    Please see attached project for further Tips.


    Resize Containers

    Just for Information, that containers must be set or resized according to their properties set during Design Time. Resizing Containers (which I believe) must be avoided.

    visual basic code:

    VB Code:
    1. Private Sub Form_Load()
    2.     Frame1.Left = Me.ScaleLeft + 50
    3.     Frame1.Top = Me.ScaleTop + 50
    4.     Frame1.Height = Me.ScaleHeight / 2
    5.     Frame1.Width = Me.ScaleWidth / 2
    6. End Sub
    7.  
    8. Private Sub Form_Resize()
    9.     Frame1.Left = Me.ScaleLeft + 50
    10.     Frame1.Top = Me.ScaleTop + 50
    11.     Frame1.Height = Me.ScaleHeight / 2
    12.     Frame1.Width = Me.ScaleWidth / 2
    13. End Sub


    Resizing Line Controls

    Line Controls are not resized along with other controls because a Line control does not have .Left, .Top, .Height and .Width properties. Instead, it has .X1, .X2, .Y1 and .Y2 properties. So it is quite similar to the Resize fundamental used for controls, except:

    visual basic code:

    VB Code:
    1. For Each tmpControl In Me.Controls
    2.         If TypeOf tmpControl Is Line Then
    3.  
    4.             tmpControl.X1 = tmpControl.X1 / PrevResizeX * Me.ScaleWidth
    5.             tmpControl.X2 = tmpControl.X2 / PrevResizeX * Me.ScaleWidth
    6.             tmpControl.Y1 = tmpControl.Y1 / PrevResizeY * Me.ScaleHeight
    7.             tmpControl.Y2 = tmpControl.Y2 / PrevResizeY * Me.ScaleHeight
    8.  
    9.         End If
    10.     Next tmpControl


    Resizing Fonts

    The topic is quite understandable, isn't it!! Resizing a control does not resize the Font of it.

    There are 2 samples attached for it, 1 downloaded and modified from PSCode.com and the other being made by me. Just use the Previous to Present ratio idea to resize the font.


    Resizing UserControls

    Well I tried Resizing a UC using my code and it is working perfectly but I found this on MSDN:
    Resizing OCX Does Not Resize Its Component Control(s)

    I could not figure out what the problem is, though the UC I have attached is not made by me and is from another project.


    Minimized Forms
    When the form is minimized you do not need to re-position/re-size controls, as they cannot be seen anyway - so you are effectively wasting time. If you set values according to calculations based on the size of the form you are likely to get errors too.

    In order to resolve this you should not process the resize event if the form is minimized, which you can do like this:

    visual basic code:

    VB Code:
    1. Private Sub Form_Resize()
    2.   If Me.WindowState = vbMinimized Then Exit Sub
    3.  
    4.   'code to re-size/re-position controls
    5.  
    6. End Sub



    Examples of the above methods (except Minimized Forms) can be found in separate projects in the attachment.

    In case you come across any problems, please post a thread in the Classic Visual Basic Forum so that others members can participate in it (any replies to this thread will not be visible!).

    Always use Option Explicit in your code. While working on this, I realised how important is using this.

  4. #4
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: How to make MDI Form fix with user resolution

    Check MartinLiss's signature for code to resize your forms.

  5. #5

    Thread Starter
    Addicted Member girl81's Avatar
    Join Date
    Apr 2006
    Location
    Malaysia
    Posts
    211

    Re: How to make MDI Form fix with user resolution

    thanks everyone..let me try it out

  6. #6
    Junior Member
    Join Date
    Nov 2006
    Posts
    23

    Re: How to make MDI Form fix with user resolution

    hi all, i have a few questions. i have disabled the max button and set window state to maximized, and want to relocate the controls only when loading the form. shakti says that if the max button is disabled, window state cant be set to Maximized and must be used the code below.

    Code:
       1. If Me.Height <> 7200 Or Me.Width <> 9600 Then
       2.     Me.Height = 7200
       3.     Me.Width = 9600
       4. End If
    but why those values? if the screen resolution is different, shouldn't those values be also different? how can you know the screen size when its maximized and in run time?

  7. #7
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: How to make MDI Form fix with user resolution

    No you never hard code values of that sort. Have you attempted to maximize a form with the maximize button disabled.

  8. #8
    Junior Member
    Join Date
    Nov 2006
    Posts
    23

    Re: How to make MDI Form fix with user resolution

    yup, i tried and the form is "maximized" but my status bar dissapears

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