[英]How can you design a 1920px by 1080px WinForm on a computer with a maximum resolution of 1366x768?
I am developing an winforms application on my notebook (Screen Resolution: 1366x768). 我正在笔记本上开发一个winforms应用程序(屏幕分辨率:1366x768)。 The terminal device for the application is windows surface (Screen Resolution 1920x1080).
该应用程序的终端设备是Windows Surface(屏幕分辨率1920x1080)。 Now I want to make my forms and user controls bigger so that they can fit full screen on the surface.
现在,我想使窗体和用户控件变大,以便它们可以在表面上全屏显示。 I´v done this with this code on my main form:
我在主表单上使用以下代码完成了此操作:
Left = Top = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;
But in my UserControls I need to place the buttons textboxes etc. on the right position. 但是在我的UserControls中,我需要将按钮文本框等放置在正确的位置。 Otherwise a large area of the Application is unused.
否则,该应用程序的大部分区域都无法使用。 So I need to make the size of the user control and forms bigger but the screen resolution of my notebook is too small so it wouldn't allow numbers above 1366x768 pixels.
因此,我需要使用户控件的尺寸和表格更大,但笔记本的屏幕分辨率太小,因此不允许数字大于1366x768像素。 How can I still design the application for the screen resolution of the surface?
我如何仍可以为表面的屏幕分辨率设计应用程序?
The following code is a direct translation (via a tool) from a VB.Net answer that I posted several years ago on CodeProject , so it will look strange when viewed through the current C# fashion trend lens. 以下代码是我几年前在CodeProject上发布的VB.Net 答案的直接翻译(通过工具),因此从当前C#时尚趋势的角度来看,它看起来很奇怪。 However it is functional on Windows based PC's.
但是,它在基于Windows的PC上可以使用。
Add this Form
definition to your project. 将此
Form
定义添加到您的项目。 Perform a build. 执行构建。 Then you can use the Inherited Form item template to add a Form derived from this one.
然后,您可以使用“继承的表单”项目模板添加从该表单派生的表单。 Via the propertygrid, set the
MaxDesignWidth
and MaxDesignHeight
properties to the values that match your target resolution. 通过propertygrid,将
MaxDesignWidth
和MaxDesignHeight
属性设置为与目标分辨率匹配的值。
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class FormWithDesignSize : Form
{
private const Int32 DefaultMax = 2000; //set this to whatever you need
private Int32 _MaxDesignWidth = DefaultMax;
private Int32 _MaxDesignHeight = DefaultMax;
[Category("Design"), DisplayName("MaxDesignWidth")]
public Int32 aaa_MaxDesignWidth //Prefix aaa_ is to force Designer code placement before ClientSize setting
{
get // avoids need to write customer serializer code
{
return _MaxDesignWidth;
}
set
{
_MaxDesignWidth = value;
}
}
[Category("Design"), DisplayName("MaxDesignHeight")]
public Int32 aaa_MaxDesignHeight //Prefix aaa_ is to force Designer code placement before ClientSize setting
{
get // avoids need to write customer serializer code
{
return _MaxDesignHeight;
}
set
{
_MaxDesignHeight = value;
}
}
protected override void SetBoundsCore(Int32 x, Int32 y, Int32 width, Int32 height, BoundsSpecified specified)
{
if (this.DesignMode)
{
// The Forms.Form.SetBoundsCore method limits the size based on SystemInformation.MaxWindowTrackSize
// From the GetSystemMetrics function documentation for SMCXMINTRACK:
// "The minimum tracking width of a window, in pixels. The user cannot drag the window frame to a size
// smaller than these dimensions. A window can override this value by processing the WMGETMINMAXINFO
// message."
// See: http://msdn.microsoft.com/en-us/library/windows/desktop/ms724385%28v=vs.85%29.aspx
// This message also appears to control the size set by the MoveWindow API,
// so it is intercepted and the maximum size is set to MaxWidth by MaxHeight
// in the WndProc method when in DesignMode.
// Form.SetBoundsCore ultimately calls Forms.Control.SetBoundsCore that calls SetWindowPos but,
// MoveWindow will be used instead to set the Window size when in the designer as it requires less
// parameters to achieve the desired effect.
MoveWindow(this.Handle, this.Left, this.Top, width, height, true);
}
else
{
base.SetBoundsCore(x, y, width, height, specified);
}
}
private const Int32 WMGETMINMAXINFO = 0x24;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (this.DesignMode && m.Msg == WMGETMINMAXINFO)
{
MINMAXINFO MMI = new MINMAXINFO();
// retrieve default MINMAXINFO values from the structure pointed to by m.LParam
Marshal.PtrToStructure(m.LParam, MMI);
// reset the ptMaxTrackSize value
MMI.ptMaxTrackSize = new POINTAPI(_MaxDesignWidth, _MaxDesignHeight);
// copy the modified structure back to LParam
Marshal.StructureToPtr(MMI, m.LParam, true);
}
}
[StructLayout(LayoutKind.Sequential)]
private class MINMAXINFO
{
public POINTAPI ptReserved;
public POINTAPI ptMaxSize;
public POINTAPI ptMaxPosition;
public POINTAPI ptMinTrackSize;
public POINTAPI ptMaxTrackSize;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct POINTAPI
{
public Int32 X;
public Int32 Y;
public POINTAPI(Int32 X, Int32 Y) : this()
{
this.X = X;
this.Y = Y;
}
public override string ToString()
{
return "(" + X.ToString() + ", " + Y.ToString() + ")";
}
}
[DllImport("user32.dll")]
private extern static bool MoveWindow(IntPtr hWnd, Int32 x, Int32 y, Int32 nWidth, Int32 nHeight, bool bRepaint);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.