zip's

while(true) { Write it down; Think about it; Refine it; Sleep(); }

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

处理在不同DPI情况下GUI显示的问题,有2个方法:

  • 用 ViewBox 包一层
  • 运行时转换转换像素单位
使用 MarkupExtension
namespace Program
{
public class pxExtension : MarkupExtension
{
private readonly double _len;
public pxExtension(string len)
{
try
{
_len
= double.Parse(len);
}
catch (Exception e)
{
throw new ArgumentException("pxExtension ctor argument invalid.", e);
}
}

public override object ProvideValue(IServiceProvider serviceProvider)
{
return ToUnits(_len);
}

public static double ToUnits(double desiredPixels)
{
double finalUnits = desiredPixels * 96 / Dpi;
return finalUnits;
}

private static readonly int Dpi = DpiX;

public static int DpiX
{
get
{
IntPtr dc
= GetDC(IntPtr.Zero);
return GetDeviceCaps(dc, LOGPIXELSX);
}
}
public static int DpiY
{
get
{
IntPtr dc
= GetDC(IntPtr.Zero);
return GetDeviceCaps(dc, LOGPIXELSY);
}
}

#region Win32 Interop

[DllImport(
"User32.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
private static extern IntPtr GetDC(IntPtr hWnd);

[DllImport(
"Gdi32.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)]
private static extern int GetDeviceCaps(IntPtr hdc, int nIndex);

private const int HORZRES = 8;
private const int VERTRES = 10;
private const int LOGPIXELSX = 88;
private const int LOGPIXELSY = 90;

#endregion
}
}
<TextBlock Width="{local:px 50}" FontSize="{local:px 12}" Text="lalala" HorizontalAlignment="Center"/>
posted on 2011-01-12 14:07  zip's  阅读(720)  评论(0编辑  收藏  举报