Change the resolution of the form based on the screen resolution (without changing the resolution of the monitor and using the Maximum screen option)

I searched the forums and I tried a few things ... but they really didn't work. Let me outline my problem.

I have a very high screen resolution on my laptop: 1400x1050. I am developing my application on this.

My colleague tried this on his laptop (which had a lower resolution), and the application did not fit on his laptop. The buttons were pulled out of the screen.

So, I want my application to automatically resize / adjust depending on the screen resolution. I found several similar forums, and I tried a few things suggested by the developers, but that didn't help me.

I tried: Solution 1 : But instead of customizing the form, the user screen changes.

I do not want to use the Maximum screen option and do not want to change the settings of the user PC. Unfortunately, I do not use the table layout pane.

Please offer me a simple solution.

+4
source share
6 answers

I know this is stupid, but ... did you try to establish control "bindings" ?

They allow your management to resize when you resize your form, they may help you, and also think about using scroll.

+2
source

OK, it's about as simple as it gets. Just iterate over the VB controls and adjust their sizes depending on the ratio of the new screen resolution to the screen resolution of your design. 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 use reflection to see if each control has properties that we need to configure. The way I do this is clean, but uses late binding and requires the Strict Off option. Tested and approved.

+2
source

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

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

Given this, you should check when the form loads so that your width of the form is less than the width of the screens:

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

This should do the trick in most scenarios (as long as your form already handles resizing). If you want to serve multiple screens, you will need additional logic to get the screen sizes that your form starts with, but that sounds like redundant for what you want.

+1
source

A simple solution?

Create the application with the lowest expected resolution (EG 800x600) so that it can be scaled up.

+1
source

If you cannot or want to reduce some controls, you can or want to use some panels that can be docked / displayed / hidden by users. This will give you more flexibility.

Look at them.

0
source

vb.net 2013 Found some of this code on this site, cannot find it now to give credit! :-( Made with a resolution of 15.5 hp 1780x760, changes the working area of ​​the user's main screen. Resizes the controls for new shapes and fonts, too, if it falls on a certain res on top of the original. Try playing 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 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 
0
source

Source: https://habr.com/ru/post/1334880/


All Articles