Why is it, that in the Visual Studio WinForms designer I cannot increase the size of my Form above the resolution of the screen I am currently working on? I think it should
I have found a work around for this issue. In my case I was needing a design time solution.
I found that using a multi display system with the desktop extended to each display, this effectively tells the system your max display is the combination of displays.
This is a work around and is a pain. Especialy for me while I was in the field with just a laptop.
Scaling at runtime is ok if you're targeting multiple systems. But if its just a single target system (one size display), then it is an unneeded overhead in design coding and runtime.
Microsoft should have realized this was a bad idea or at least given a choice to override this "design feature".
This worked for me, copied from
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class Form1 : Form
{
[DllImport("User32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool Repaint);
private void Form1_Load(System.Object sender, System.EventArgs e)
{
this.MaximumSize = new Size(5000, 800);
bool Result = MoveWindow(this.Handle, this.Left, this.Top, 5000, 500, true);
}
public Form1()
{
Load += Form1_Load;
}
}
As Neil N
mentioned you can specify it at the code. however if you want to get the screen size or resolution and then set the form to that size you can get that by Screen.PrimaryScreen.Bounds
and Screen.PrimaryScreen.BitsPerPixel
...
I found a dumb work-around that lets me view and use the larger form sizes in the design space.
I am working on a 1440x900 monitor for a 1240x1024 monitor, so can't reach the vertical component. What I did was simply right-click my desktop, change the resolution to 1240x1024, adjust the form size, and then change the resolution back. Upon changing back, it'll retain the given Form size, even is it's outside the acceptable limit.
The only issue this really poses is that if you want to run the Form in your first resolution, it'll be too large to view properly.
I know this is an old question, I figured I'd share what I'd found. The quick fix might help someone out there.
Unfortunately (I hope someone else will post a better solution!), the only workaround I'm aware of is to place a panel inside the form.
Set the Autoscroll
and AutoSize
properties of the Parent Form to true. Then increase the panel size to the desired size. The form itself will still not get any larger than your screen resolution, but it will show scroll bars, so at least you can use the designer to drop controls etc beyond your size limitations onto the larger panel.
Then, you may need to add some code to adjust the the forms size at run-time so that it is large enough to show the panel without scroll bars (and perhaps also disable the Autoscroll property).
I know, It's not a particularly nice workaround...
EDIT:
Looks like this is intentional and by design:
MSDN
Property Form.Size: The maximum value of this property is limited by the resolution of the screen on which the form runs. The value cannot be greater than 12 pixels over each screen dimension (horizontal + 12 and vertical + 12).
and again at Microsoft Connect/Public Bug Tracking:
Posted by Microsoft on 10/9/2008 at 12:18 AM
Thanks for your feedback on the .NET Framework!
The issue that you have reported is actually By Design.
In MSDN at http://msdn.microsoft.com/en-us/library/25w4thew.aspx, you can find the following information at the topic Form.Size Property:
The maximum value of this property is limited by the resolution of the screen on which the form runs. The value cannot be greater than 12 pixels over each screen dimension (horizontal + 12 and vertical + 12).
Therefore, we can't enlarge our forms indefinitely. This behavior is consistent with other software, such as Notepad and Microsoft Paint.
This behavior is defined in the mothed Form.SetBoundsCore(...) with the following code:
Size max = SystemInformation.MaxWindowTrackSize;
if (height > max.Height) {
height = max.Height; }
if (width > max.Width) {
width = max.Width; }
[...]
Thanks, UIFx Team
EDIT2:
Since the check is hardcoded in Forms.SetBoundsCore like (using ILSpy as a decompiler):
if (this.WindowState == FormWindowState.Normal && (base.Height != height || base.Width != width))
{
Size maxWindowTrackSize = SystemInformation.MaxWindowTrackSize;
if (height > maxWindowTrackSize.Height)
{
height = maxWindowTrackSize.Height;
}
if (width > maxWindowTrackSize.Width)
{
width = maxWindowTrackSize.Width;
}
}
and SetBoundsCore is a protected function, perhaps you could try deriving a class from Windows.Forms.Form, override SetBoundsCore and don't enforce this check in your version of SetBoundsCore? I haven't tried if it works though...
Deriving from Form, overriding some properties and using interop. This is VB.NET sorry but you get the idea.
Using the derived form you can use "SizeDesign" and "SizeRuntime" properties for design and runtime respectively.
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class FormEx
Inherits Form
Private Declare Function MoveWindow Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer, ByVal Repaint As Boolean) As Boolean
Private Const DEFAULTSIZE_X = 1024
Private Const DEFAULTSIZE_Y = 768
Protected Overrides Sub OnHandleCreated(e As System.EventArgs)
MyBase.OnHandleCreated(e)
If mSizeRuntime = System.Drawing.Size.Empty Then
SizeRuntime = New System.Drawing.Size(DEFAULTSIZE_X, DEFAULTSIZE_Y)
End If
If mSizeDesign = System.Drawing.Size.Empty Then
SizeDesign = New System.Drawing.Size(DEFAULTSIZE_X, DEFAULTSIZE_Y)
End If
End Sub
<Browsable(False)> _
Public Shadows Property Size As System.Drawing.Size
Private mSizeDesign As System.Drawing.Size = System.Drawing.Size.Empty
<Category("Layout"), _
Description("Sets the size of the form at design time."), _
RefreshProperties(RefreshProperties.All)> _
Public Property SizeDesign As System.Drawing.Size
Get
Return mSizeDesign
End Get
Set(value As System.Drawing.Size)
mSizeDesign = value
If Me.DesignMode Then
MoveWindow(Me.Handle, Me.Left, Me.Top, value.Width, value.Height, True)
End If
End Set
End Property
Private mSizeRuntime As System.Drawing.Size = System.Drawing.Size.Empty
<Category("Layout"), _
Description("Sets the size of the form at runtime."), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Property SizeRuntime As System.Drawing.Size
Get
Return mSizeRuntime
End Get
Set(value As System.Drawing.Size)
mSizeRuntime = value
If Not Me.DesignMode Then
MyBase.Size = value
End If
End Set
End Property
End Class
A.J.