首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在最高分辨率为1366x768的计算机上设计1920px x 1080px的WinForm?

如何在最高分辨率为1366x768的计算机上设计1920px x 1080px的WinForm?
EN

Stack Overflow用户
提问于 2019-02-22 03:59:09
回答 1查看 490关注 0票数 0

我正在我的笔记本上开发一个winforms应用程序(屏幕分辨率: 1366x768)。应用程序的终端设备是windows surface (屏幕分辨率1920x1080)。现在我想让我的窗体和用户控件变得更大,这样它们就可以在表面上全屏显示了。我在我的主窗体上用下面的代码做到了这一点:

代码语言:javascript
运行
复制
        Left = Top = 0;
        Width = Screen.PrimaryScreen.WorkingArea.Width;
        Height = Screen.PrimaryScreen.WorkingArea.Height;

但在我的UserControls中,我需要将按钮、文本框等放在正确的位置。否则,应用程序的一大块区域将被闲置。因此,我需要使用户控件和窗体的大小更大,但我的笔记本电脑的屏幕分辨率太小,因此它不允许超过1366x768像素的数字。我如何仍然可以设计应用程序的屏幕分辨率的表面?

EN

Stack Overflow用户

发布于 2019-02-22 05:22:29

以下代码是VB.Net answer that I posted several years ago on CodeProject的直接翻译(通过工具),因此当通过当前C#时尚趋势镜头查看时,它看起来会很奇怪。然而,它在基于Windows的PC上是有效的。

将此Form定义添加到您的项目中。执行构建。然后,您可以使用继承的表单项模板添加从该表单派生的表单。通过属性网格,将MaxDesignWidthMaxDesignHeight属性设置为与目标分辨率匹配的值。

代码语言:javascript
运行
复制
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;

代码语言:javascript
运行
复制
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);

}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54815288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档