如何在最大分辨率为 1366x768 的计算机上设计一个 1920px x 1080px 的 WinForm? [英] How can you design a 1920px by 1080px WinForm on a computer with a maximum resolution of 1366x768?

查看:113
本文介绍了如何在最大分辨率为 1366x768 的计算机上设计一个 1920px x 1080px 的 WinForm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的笔记本上开发一个 winforms 应用程序(屏幕分辨率:1366x768).应用程序的终端设备是 windows Surface(屏幕分辨率 1920x1080).现在我想让我的表单和用户控件更大,以便它们可以在表面上全屏显示.我在主表单上使用此代码完成了此操作:

I am developing an winforms application on my notebook (Screen Resolution: 1366x768). The terminal device for the application is windows surface (Screen Resolution 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;

但在我的 UserControls 中,我需要将按钮文本框等放在正确的位置.否则,很大一部分应用程序是未使用的.因此,我需要将用户控件和窗体的大小设置得更大,但我的笔记本的屏幕分辨率太小,因此不允许超过 1366x768 像素的数字.我怎样才能仍然为表面的屏幕分辨率设计应用程序?

But in my UserControls I need to place the buttons textboxes etc. on the right position. 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. How can I still design the application for the screen resolution of the surface?

推荐答案

以下代码是从 VB.Net 我几年前在 CodeProject 上发布的答案,所以通过当前查看它会看起来很奇怪C#时尚潮流镜头.但是它可以在基于 Windows 的 PC 上运行.

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. However it is functional on Windows based PC's.

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

Add this Form definition to your project. 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.

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);

}

这篇关于如何在最大分辨率为 1366x768 的计算机上设计一个 1920px x 1080px 的 WinForm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆