我正在我的笔记本上开发一个winforms应用程序(屏幕分辨率: 1366x768)。应用程序的终端设备是windows surface (屏幕分辨率1920x1080)。现在我想让我的窗体和用户控件变得更大,这样它们就可以在表面上全屏显示了。我在我的主窗体上用下面的代码做到了这一点:
Left = Top = 0;
Width = Screen.PrimaryScreen.WorkingArea.Width;
Height = Screen.PrimaryScreen.WorkingArea.Height;但在我的UserControls中,我需要将按钮、文本框等放在正确的位置。否则,应用程序的一大块区域将被闲置。因此,我需要使用户控件和窗体的大小更大,但我的笔记本电脑的屏幕分辨率太小,因此它不允许超过1366x768像素的数字。我如何仍然可以设计应用程序的屏幕分辨率的表面?
发布于 2019-02-22 05:22:29
以下代码是VB.Net answer that I posted several years ago on CodeProject的直接翻译(通过工具),因此当通过当前C#时尚趋势镜头查看时,它看起来会很奇怪。然而,它在基于Windows的PC上是有效的。
将此Form定义添加到您的项目中。执行构建。然后,您可以使用继承的表单项模板添加从该表单派生的表单。通过属性网格,将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);
}https://stackoverflow.com/questions/54815288
复制相似问题