vs develop winform configuration in the high score screen

First, the size of the form controls

The first method: using a grid divisible avoid errors

in LayoutMode (layout pattern) option in the Windows Form Designer SnapToGrid into (aligned to the grid), and the Default Grid Cell Size (mesh size default) set minimum scaling unit (or a multiple thereof), to avoid generating an error divisible during transplanting. And because these units are visible, but also makes the control onto a suitable size is very simple.

At the same time, the form should be changed to AutoScaleMode Dpi. Default Font Scaling use the system default font size is scaled, but the system default font and DPI is not exactly proportional, this can also cause divisible error.

If the text is not aligned, you can consider adjusting TextAlign properties, such as single-line Label recommended MiddleLeft, MiddleCenter or MiddleRight alignment, instead of the default TopLeft alignment, and other controls to the alignment of unity.

The minimum scaling unit shown in the following table (= minimum scaling unit scaling / 25% = DPI / 24) :

缩放    DPI值   最小可缩放单元
100%    96      4
125%    120     5
150%    144     6
175%    168     7
200%    192     8
225%    216     9
250%    240     10


Windows Forms Designer option (150%): Effect: The second method: using a layout container layout if carefully studied WPF, WPF will know that can layout by Grid, StackPanel, WrapPanel and other layout containers. In fact, Windows Forms also has two such vessels, called TableLayoutPanel and FlowLayoutPanel, where the former is the same as the form and Grid layout container, but as the latter and is WrapPanel flow layout container. After the new form, using the TableLayoutPanel (table layout panel) layout controls the following sequence: 1. drag parent window to the appropriate size, attributes are set as follows: the Padding: [provided appropriate padding minimum scaling unit (e.g., 150% scaling is under 6)] or a multiple of 2. drag a TableLayoutPanel control, the following property: the dock: set fill (= docks filled) 3 divided columns (columns) and the rows (rows) needed requirements: a cells can only place a control (or container), but may control a span multiple rows or columns 4. drag other controls (or container) to the corresponding cell, provided dock: set fill (= docks filled) Margin : set the appropriate minimum margins [scale units (such as the scaling is 150% 6)] or a multiple of the ColumnSpan: the number of columns needed across RowSpan: rows to span Note: Windows Forms Height attributes in some controls sometimes does not work (such as single-line TextBox etc.), provided Dock property does not guarantee cell is completely filled, but still can be aligned as long as the text It.
pic



pic

























The FlowLayoutPanel (flow layout panel) and different TableLayoutPanel Child controls to set the Width, Height, Margin properties, Dock property can not be set, and Width, Height, and must also be the same as Margin is the smallest unit of the zoom magnification. In other words, still need to use "Snap to Grid" feature to complete the design.

Operational phase

By default, Windows Forms program because of the high DPI support is not complete, the system after Windows Vista is by the DWM system to scaling (DPI called virtualization), so will result in fuzzy interface. You can modify and call SetProcessDPIAware function Program.cs close DPI virtualization, by the program to manage their own interface:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; // 导入System.Runtime.InteropServices命名空间 using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 {     static class Program     {         // 外部函数声明         [DllImport("kernel32.dll")]         private static extern IntPtr GetModuleHandle(string name);         // 这个函数只能接受ASCII,所以一定要设置CharSet = CharSet.Ansi,不然会失败         [DllImport("kernel32.dll", CharSet = CharSet.Ansi)]         private static extern IntPtr GetProcAddress(IntPtr hmod, string name);         private delegate void FarProc();         /// <summary>         /// The main entry point for the application.         /// </summary>         [STAThread]         static void Main()         {             // SetProcessDPIAware是Vista以上才有的函数,需兼容XP的话不能直接调用,需按如下所示间接调用             IntPtr hUser32 = GetModuleHandle("user32.dll");             IntPtr addrSetProcessDPIAware = GetProcAddress(hUser32, "SetProcessDPIAware");             if (addrSetProcessDPIAware != IntPtr.Zero)             {                 FarProc SetProcessDPIAware = (FarProc)Marshal.GetDelegateForFunctionPointer(addrSetProcessDPIAware, typeof(FarProc));                 SetProcessDPIAware();             }             // C#的原有代码             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new Form1());         }     } }


If you do not compatible with Windows XP, you can more easily direct call SetProcessDPIAware:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; // 导入System.Runtime.InteropServices命名空间 using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 {     static class Program     {         // 外部函数声明         [DllImport("user32.dll")]         private static extern void SetProcessDPIAware();         /// <summary>         /// The main entry point for the application.         /// </summary>         [STAThread]         static void Main()         {             // SetProcessDPIAware是Vista以上才有的函数,这样直接调用会使得程序不兼容XP             SetProcessDPIAware();             // C#的原有代码             Application.EnableVisualStyles();             Application.SetCompatibleTextRenderingDefault(false);             Application.Run(new Form1());         }     } }


.NET Framework 4.5.1 provides an optimized high DPI support, but need to manually open, add app.config in the project (you need to publish files with .exe files and .exe.config when released), reads as follows:

<?xml version="1.0" encoding="utf-8"?> <configuration>     <appSettings>         <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" />     </appSettings> </configuration>

Two, FormSize and design does not match

High DPI is supported by Windows DWM (Desktop Window Manager) scaling to achieve, but sometimes we do not want this effect (such as zoom will make some content becomes blurred), and therefore need to disable Windows high DPI scaling program. There are two ways to achieve this effect: One is to use an application manifest file, using a system API.

1, using the manifest file

Winform here for an example, right-Project -> Add -> New Item -> application manifest file containing attribute dpiAware label uncommented code is as follows:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
</windowsSettings>
</application>
2、使用系统API

The following code disables high DPI to Win7 and above systems.

if (Environment.OSVersion.Version.Major >= 6)
{
SetProcessDPIAware();
}

[DllImport("user32.dll")]

Guess you like

Origin www.cnblogs.com/z45281625/p/11090140.html