簡體   English   中英

我的Windows窗體應用程序大小在不同的筆記本上發生了變化

[英]My Windows Forms application size is changing on different notebooks

編碼我的項目進展順利。 但今天我發現了一個問題。

我的主筆記本有完整的高清(1920x1080)分辨率,我在這里編碼我的項目。 當我將主筆記本的分辨率更改為1280x1024,1280x800或1024x768時,沒有問題。 我的應用程序的分辨率是1024x768,並沒有崩潰。 這是版畫屏幕。

普通屏幕

但我的其他筆記本有1366x768分辨率。 我在這台筆記本上運行我的應用程序。 哦! 令人失望的是。 我的應用程序屏幕移動 這是糟糕的印刷品。

屏幕不好

我不理解為什么。 我該怎么做才能糾正這個錯誤?

它源於不同的DPI設置。 您可以在表單加載中執行此操作:

// Get DPI width
float x = this.CreateGraphics().DpiX;

// If screen is width
if (x == 120)
    // Get big image from Resources
    this.BackgroundImage = Properties.Resources.BigImage;
else
    // Get small image from Resources
    this.BackgroundImage = Properties.Resources.SmallImage;

我在Windows窗體表單中遇到了同樣的問題。 我所要做的就是將表單的AutoScaleMode設置從Font更改為DPI,並將FormBorderStylefor設置從Fixed更改為Sizable。 現在Windows窗體表單在桌面和筆記本電腦上正確顯示。

您可以檢查兩個屏幕的DPI設置是否相同。 你可以通過控制面板或顯示選項來實現這一點(我記不清楚了,而且我面前沒有Windows 7)(你的高清筆記本電腦可能有120 DPI,另一方面有96 DPI) )。

在您的程序中,將窗體的AutoScaleMode設置為None然后重試。

這是一個有助於如何處理自動縮放表單的資源:
Windows窗體中的自動縮放

沒有直接的解決方案來解決像素問題。 但我們可以自己做。 首先,我們必須在表單中找到可用的控件,然后我們必須調整它們的大小。

添加一個類“IdentifyControls.cs”,它標識表單的所有控件並返回應用程序中的控件列表。 可以在應用程序選擇項目中添加一個類 - >從菜單欄添加類。 然后輸入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FallingFlowers // Falling Flowers is the name of my project
{
    public static class IdentifyControl
    {
        public static List<Control> findControls(Control c)
        {
            List<Control> list = new List<Control>();
            foreach (Control control in c.Controls)
                list.Add(control);
            return list;
        }
    }
}

然后在您的應用程序中添加另一個類,例如“demo.cs”:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace FallingFlowers // Falling Flowers is the name of my project
{
    public class demo
    {
        public static void relocate(Form fo, int ox, int oy)
        {
            List<Control> list = new List<Control>();
            list = IdentifyControl.findControls(fo);
            for (int i = 0; i < list.Count; i++)
                reposition(list[i], ox, oy);
        }

        public static void reposition(Control c, int ox, int oy)
        {
            int x, y;
            x = ((c.Location.X * Screen.PrimaryScreen.Bounds.Width) / ox);
            y = ((c.Location.Y * Screen.PrimaryScreen.Bounds.Height) / oy);
            c.Location = new Point(x, y);
            x = ((c.Width * Screen.PrimaryScreen.Bounds.Width) / ox);
            y = ((c.Height * Screen.PrimaryScreen.Bounds.Height) / oy);
            c.Width = x;
            c.Height = y;
            if (c is Label || c is Button)
                resizeText(c, ox, oy);
        }

        public static void resizeText(Control l, int ox, int oy)
        {
            float s;
            float txtsize = l.Font.Size;
            s = ((txtsize * Screen.PrimaryScreen.Bounds.Width) / ox)+1;
            l.Font = new Font(l.Font.Name, s,l.Font.Style);
        }

        public static void repositionForm(Form f, int ox, int oy)
        {
            int x, y;
            x = (f.Location.X * Screen.PrimaryScreen.Bounds.Width) / ox;
            y = (f.Location.Y * Screen.PrimaryScreen.Bounds.Width) / oy;
            f.Location = new Point(x, y);
        }
    }
}

此類包含重定位控件,調整文本大小和調整窗體大小的方法。

在表單的load事件中調用這些函數。

重新定位表單中的所有控件

demo.relocate(this, 1366, 768);

這里1366和768是開發應用程序的原始分辨率。

要重新定位表單:

demo.repositionForm(this, 1366, 768);

1366和768是開發應用程序的原始分辨率。

對你來說它將是demo.relocate(this, 1920, 1080);

我希望這會對你有所幫助: - )...

我同意屏幕設置的代碼可以幫助您找到問題。

但似乎您正在設置圖片以設置坐標點而不是相對於屏幕尺寸的點。 您可能希望使坐標與屏幕大小成比例,以使顯示始終看起來不錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM