📄 dpihelper.cs
字号:
#region Using directives
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
#endregion
namespace PocketEarth
{
/// <summary>
/// A helper object to adjust the sizes of controls based on the DPI.
/// </summary>
public class DpiHelper
{
/// <summary>
/// The real dpi of the device.
/// </summary>
private static int dpi = SafeNativeMethods.GetDeviceCaps(IntPtr.Zero, /*LOGPIXELSX*/88);
/// <summary>
/// Adjust the sizes of controls to account for the DPI of the device.
/// </summary>
/// <param name="parent">The parent node of the tree of controls to adjust.</param>
public static void AdjustAllControls(Control parent)
{
if (dpi == 96)
{
return;
}
foreach (Control child in parent.Controls)
{
AdjustControl(child);
AdjustAllControls(child);
}
}
public static void AdjustControl(Control control)
{
control.Bounds = new Rectangle(
control.Left * dpi / 96,
control.Top * dpi / 96,
control.Width * dpi / 96,
control.Height * dpi / 96 );
}
/// <summary>
/// Scale a coordinate to account for the dpi.
/// </summary>
/// <param name="x">The number of pixels at 96dpi.</param>
/// <returns></returns>
public static int Scale(int x)
{
return x * dpi / 96;
}
private class SafeNativeMethods
{
[DllImport("coredll.dll")]
static internal extern int GetDeviceCaps(IntPtr hdc, int nIndex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -