gpt4 book ai didi

winforms - Winform App 不同的 Win 7 与 Win 8

转载 作者:行者123 更新时间:2023-12-01 06:36:47 26 4
gpt4 key购买 nike

我在使用标准数据驱动的 Winform 应用程序时遇到了一个有趣的问题。

该应用程序最初是在 Windows 7 和 Visual Studio 2010 上开发的。然后我用 Windows 8 和 Visual Studio 2010 构建了一个新的开发机器。我发布了一个新版本的 Winform 应用程序,它构建在 Windows 8 机器上。没有源代码更改,相同的 .NET 4.0 框架目标。在 Windows 7 上运行 Winform 应用程序的客户端电脑现在会遇到表单呈现问题。控件在 Windows 7 上似乎有点变形,在视觉上改变了表单,在某些情况下,渲染功能被破坏(由于渲染问题,控件在屏幕外)。

我已经升级到 VS2012,并针对 .NET 4.5。同样的问题仍然存在。

我需要做些什么才能在 Windows 7 和 Windows 8 之间获得一致的表单渲染?

最佳答案

我有类似的问题。为了修复它,我检查当前的 DPI 设置并在需要的地方水平和垂直缩放尺寸。这个辅助类给出了水平和垂直比例:HDpiScale、VDpiScale。

用法:

MyControl.Height = (int) (MyControl.Height * util.VDpiScale);
MyControl.Width = (int) (MyControl.Width * util.HDpiScale);

仅当字体大小为 x1.25 时才有所不同
public class PresentationUtils : IPresentationUtils
{
private double vDpiScale = -1;
private double hDpiScale = -1;

public double HDpiScale
{
get
{
if (hDpiScale < 0)
SetDpiScales();
return hDpiScale;
}
}

public double VDpiScale
{
get
{
if (vDpiScale < 0)
SetDpiScales();
return vDpiScale;
}
}

private void SetDpiScales()
{
vDpiScale = 1;
hDpiScale = 1;

IntPtr dc = GetDC(IntPtr.Zero);
try
{
int hPixels = GetDeviceCaps(dc, (int) DeviceCap.LOGPIXELSX);
int vPixels = GetDeviceCaps(dc, (int) DeviceCap.LOGPIXELSY);
vDpiScale = vPixels/96.0;
hDpiScale = hPixels/96.0;
}
finally
{
ReleaseDC(IntPtr.Zero, dc);
}
}

[DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);
}

public enum DeviceCap
{
/// <summary>
/// Logical pixels inch in X
/// </summary>
LOGPIXELSX = 88,

/// <summary>
/// Logical pixels inch in Y
/// </summary>
LOGPIXELSY = 90,
}

关于winforms - Winform App 不同的 Win 7 与 Win 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14105889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com