开发者

Change Resolution Of Form Based On Screen Resolution ( without changing monitor resolution and using Maximized screen option )

I searched on the forums, and I tried a few things... but they didn't really seem to work. Let me lay out my problem.

I have a very high screen resolution on my lap开发者_运维百科top: 1400x1050. I'm designing my app on this.

My colleague tried it out on his laptop (which had lesser resolution), and the application did not fit on his laptop. The buttons were dragging out of the screen space.

So, I want my application to automatically resize/adjust based upon the screen resolution. I found some similar forums, and I tried a few things suggested by developers, but that did not really work out for me.

I tried : Solution 1 : But is changing user's screen resution in place of adjusting form .

I don't want to use Maximized screen option and don't want to change user's pc settings. Unfortunatly I am not using Table Layout panel.

Please suggest me a simple solution.


OK, this is just about as simple as it gets. Just iterate through the VB controls and adjust their sizes based on the ratio of the new screen resolution to your design screen resolution. i.e., something like:

    Dim DesignScreenWidth As Integer = 1600
    Dim DesignScreenHeight As Integer = 1200
    Dim CurrentScreenWidth As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim CurrentScreenHeight As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim RatioX as Double = CurrentScreenWidth / DesignScreenWidth
    Dim RatioY as Double = CurrentScreenHeight / DesignScreenHeight
    For Each iControl In Me.Controls
        With iControl
            If (.GetType.GetProperty("Width").CanRead) Then .Width = CInt(.Width * RatioX)
            If (.GetType.GetProperty("Height").CanRead) Then .Height = CInt(.Height * RatioY)
            If (.GetType.GetProperty("Top").CanRead) Then .Top = CInt(.Top * RatioX)
            If (.GetType.GetProperty("Left").CanRead) Then .Left = CInt(.Left * RatioY)
        End With
    Next

NOTE that I'm using reflection to see if each control has the properties we need to adjust. The way I'm doing it is clean but uses "late binding" and requires Option Strict Off. Tested and approved.


I know it's stupid but... did you try to set control "anchors"?

They allows your control to resize when you resize your form, maybe can help you, also think about using scrollbars


You can use the following code to get the height and width of the primary screen:

Dim intX As Integer = Screen.PrimaryScreen.Bounds.Width
Dim intY As Integer = Screen.PrimaryScreen.Bounds.Height

Given this you should perform a check when the form loads to ensure your forms width is smaller than the screens width:

// This is pseudocode, as I usually do C#:
MyForm.Width = Min(ScreenWidth, MyForm.Width)
MyForm.Height = Min(ScreenHeight, MyForm.Height)

That should do the trick in most scenarios (as long as your form already handles being resized) - if you want to cater for multiple screens then some additional logic will be needed to get the dimensions of the screen that your form starts in, but this sounds like overkill for what you want.


Simple solution?

Design your application at the lowest expected resolution (E.G. 800x600) so that it can be scaled UP.


If you are not able or willing to scale down some controls you might be able or willing to use some kind of panels that can be pinned/shown/hidden at the users will. That will give you more flexibility.

Have a look at these.


vb.net 2013 Found some of this code on this site, can't find it now to give credit!:-( Made at 15.5 laptop res of 1780x760, changes to users primary screen working area. Resize controls to match forms new size, and the fonts too if it hits a certain res over original. Try it, play with it.

Option Strict On
Option Explicit On
Public Class Form1
    ' For screen size changes. 
    Dim cw As Integer ' Forms current Width.
    Dim ch As Integer ' Forms current Height.
    Dim iw As Integer = 1280 ' Forms initial width.
    Dim ih As Integer = 760 ' Forms initial height.
    ' Retrieve the working rectangle from the Screen class using the        PrimaryScreen and the WorkingArea properties.  
    Dim workingRectangle As System.Drawing.Rectangle =     Screen.PrimaryScreen.WorkingArea

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Set the size of the form slightly less than size of working rectangle. 
    Me.Size = New System.Drawing.Size(workingRectangle.Width - 5, workingRectangle.Height - 5)
    ' Set the location so the entire form is visible. 
    Me.Location = New System.Drawing.Point(3, 3)
End Sub

Private Sub Main_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    ' Change controls size and fonts to fit screen working area..
    Dim rw As Double = (Me.Width - cw) / cw ' Ratio change of original form width.
    Dim rh As Double = (Me.Height - ch) / ch ' Ratio change of original form height.
    ' Change controls size to fit users screen working area.
    For Each Ctrl As Control In Controls
        Ctrl.Width += CInt(Ctrl.Width * rw)
        Ctrl.Height += CInt(Ctrl.Height * rh)
        Ctrl.Left += CInt(Ctrl.Left * rw)
        Ctrl.Top += CInt(Ctrl.Top * rh)
    Next
    cw = Me.Width
    ch = Me.Height
    ' Change all the forms controls font size.
    Dim nfsize As Single
    If cw > iw + 500 Then
        For Each Ctrl As Control In Controls
            ' Get the forms controls font size's property and increase it. Reset the font to the new size. 
            nfsize = Me.Font.Size + 3
            Ctrl.Font = New Font(Ctrl.Font.Name, nfsize, FontStyle.Bold, Ctrl.Font.Unit)
        Next
    Else
        Exit Sub
    End If
End Sub


I think in VB.Net there is easier way to do that, by changing two values of form property.

  1. AutoSize = True

  2. AutoScaleMode = Dpi

Thank You.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜