100分求问!!winform编写的窗口程序,如何在win7下忽略高DPI设置,不跟随其放大放大? 本帖最后由 yuzhenhao 于 2013-03-13 10:41:03 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以尝试下TableLayoutPanel,把控件放到格子里。 DPI适应处理方式:1.所有设置了BackgroundImage的控件背景图 BackgroundImageLayout 属性设置为Stretch2.窗体打开后获取DPI系数private void Form1_Load(object sender, EventArgs e) { //获取系统DPI try { SetProcessDPIAware(); //重要 IntPtr screenDC = GetDC(IntPtr.Zero); int dpi_x = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSX); int dpi_y = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSY); CommonInfo.scaleUIX = dpi_x / 96.0;//横向系数 CommonInfo.scaleUIY = dpi_y / 96.0;//纵向系数 ReleaseDC(IntPtr.Zero, screenDC); } catch (Exception) { //throw; //没有管理员权限就获取不到了 } }/*----------------------------------------获取系统DPI-----------------------------------------------------*/ [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr ptr); [DllImport("user32.dll", EntryPoint = "ReleaseDC")] public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("gdi32.dll")] public static extern IntPtr CreateDC( string lpszDriver, // driver name string lpszDevice, // device name string lpszOutput, // not used; should be NULL Int64 lpInitData // optional printer data ); [DllImport("gdi32.dll")] public static extern int GetDeviceCaps( IntPtr hdc, // handle to DC int nIndex // index of capability ); [DllImport("user32.dll")] internal static extern bool SetProcessDPIAware(); const int DRIVERVERSION = 0; const int TECHNOLOGY = 2; const int HORZSIZE = 4; const int VERTSIZE = 6; const int HORZRES = 8; const int VERTRES = 10; const int BITSPIXEL = 12; const int PLANES = 14; const int NUMBRUSHES = 16; const int NUMPENS = 18; const int NUMMARKERS = 20; const int NUMFONTS = 22; const int NUMCOLORS = 24; const int PDEVICESIZE = 26; const int CURVECAPS = 28; const int LINECAPS = 30; const int POLYGONALCAPS = 32; const int TEXTCAPS = 34; const int CLIPCAPS = 36; const int RASTERCAPS = 38; const int ASPECTX = 40; const int ASPECTY = 42; const int ASPECTXY = 44; const int SHADEBLENDCAPS = 45; const int LOGPIXELSX = 88; const int LOGPIXELSY = 90; const int SIZEPALETTE = 104; const int NUMRESERVED = 106; const int COLORRES = 108; const int PHYSICALWIDTH = 110; const int PHYSICALHEIGHT = 111; const int PHYSICALOFFSETX = 112; const int PHYSICALOFFSETY = 113; const int SCALINGFACTORX = 114; const int SCALINGFACTORY = 115; const int VREFRESH = 116; const int DESKTOPVERTRES = 117; const int DESKTOPHORZRES = 118; const int BLTALIGNMENT = 119;所有后台生成的控件,比如Label lbl = new Label();这样生成的,控件的Location、Size属性,全部乘以得到的系数CommonInfo.scaleUIX、CommonInfo.scaleUIY 设置 Form 的 AutoScaleMode 属性不行么 解决过这个问题,,布局不受DPI影响,需要把主窗体,子窗体,以及控件的AutoScale全调整,还不行我给你远程协助 将 Form 设置为this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;控件都设置为Control.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; 抱歉不理解您所谓这行代码:Control.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;好多控件都没有这个属性,比如button,比如pictrue控件都没有,怎么设置? 这样设置固然是可以的,布局不会变,但是字体会放大。这样的话,如果是按钮、链接等控件,不会看起来不协调吗?有一种方式控制同时字体不会变大,控制界面不变比较好。所以还是要窗体布局随着DPI的变化而变。否则的话,用户设置DPI不是就没有意义了吗?有一些用户的屏幕尺寸小而显示器的分辨率大,他们就是希望放大DPI,调整显示效果。否则我们的窗体在屏幕中只显示一点点,也不好看。 参考:http://stackoverflow.com/questions/4089237/how-do-i-force-a-winform-c-sharp-application-to-ignore-when-a-user-choose-125-o 怎样写一个QQ邮件群发系统 Devexpress中 GridControl,行中的事件怎么弄? 关于WPF的Style datagridview的单元格验证 这个问题真是奇怪! 在线等…… (急)我用TcpServer和TcpClient写了一个服务器端和一个客户端,请问为什么不能在单机上连接呢? C#中反射用法一问 大家来讨论一下构造不规则窗体的方法 重在参与 由于Oracle的存储过程不能处理大于32K的图片带来的问题 c#的开发平台除了.net还有哪些?? C# 校正扫描仪扫描的倾斜的tif图片 wpf datagrid 行虚拟化后, 滚动条很卡。求解决方案~~
1.所有设置了BackgroundImage的控件背景图 BackgroundImageLayout 属性设置为Stretch
2.窗体打开后获取DPI系数private void Form1_Load(object sender, EventArgs e)
{
//获取系统DPI
try
{
SetProcessDPIAware(); //重要
IntPtr screenDC = GetDC(IntPtr.Zero);
int dpi_x = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSX);
int dpi_y = GetDeviceCaps(screenDC, /*DeviceCap.*/LOGPIXELSY);
CommonInfo.scaleUIX = dpi_x / 96.0;//横向系数
CommonInfo.scaleUIY = dpi_y / 96.0;//纵向系数
ReleaseDC(IntPtr.Zero, screenDC);
}
catch (Exception)
{
//throw;
//没有管理员权限就获取不到了
}
}
/*----------------------------------------获取系统DPI-----------------------------------------------------*/
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr ptr); [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); [DllImport("gdi32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver, // driver name
string lpszDevice, // device name
string lpszOutput, // not used; should be NULL
Int64 lpInitData // optional printer data
); [DllImport("gdi32.dll")]
public static extern int GetDeviceCaps(
IntPtr hdc, // handle to DC
int nIndex // index of capability
); [DllImport("user32.dll")]
internal static extern bool SetProcessDPIAware(); const int DRIVERVERSION = 0;
const int TECHNOLOGY = 2;
const int HORZSIZE = 4;
const int VERTSIZE = 6;
const int HORZRES = 8;
const int VERTRES = 10;
const int BITSPIXEL = 12;
const int PLANES = 14;
const int NUMBRUSHES = 16;
const int NUMPENS = 18;
const int NUMMARKERS = 20;
const int NUMFONTS = 22;
const int NUMCOLORS = 24;
const int PDEVICESIZE = 26;
const int CURVECAPS = 28;
const int LINECAPS = 30;
const int POLYGONALCAPS = 32;
const int TEXTCAPS = 34;
const int CLIPCAPS = 36;
const int RASTERCAPS = 38;
const int ASPECTX = 40;
const int ASPECTY = 42;
const int ASPECTXY = 44;
const int SHADEBLENDCAPS = 45;
const int LOGPIXELSX = 88;
const int LOGPIXELSY = 90;
const int SIZEPALETTE = 104;
const int NUMRESERVED = 106;
const int COLORRES = 108;
const int PHYSICALWIDTH = 110;
const int PHYSICALHEIGHT = 111;
const int PHYSICALOFFSETX = 112;
const int PHYSICALOFFSETY = 113;
const int SCALINGFACTORX = 114;
const int SCALINGFACTORY = 115;
const int VREFRESH = 116;
const int DESKTOPVERTRES = 117;
const int DESKTOPHORZRES = 118;
const int BLTALIGNMENT = 119;所有后台生成的控件,比如Label lbl = new Label();这样生成的,控件的Location、Size属性,全部乘以得到的系数CommonInfo.scaleUIX、CommonInfo.scaleUIY
Control.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
抱歉不理解您所谓这行代码:Control.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;好多控件都没有这个属性,比如button,比如pictrue控件都没有,怎么设置?
这样设置固然是可以的,布局不会变,但是字体会放大。这样的话,如果是按钮、链接等控件,不会看起来不协调吗?有一种方式控制同时字体不会变大,控制界面不变比较好。
所以还是要窗体布局随着DPI的变化而变。否则的话,用户设置DPI不是就没有意义了吗?
有一些用户的屏幕尺寸小而显示器的分辨率大,他们就是希望放大DPI,调整显示效果。否则我们的窗体在屏幕中只显示一点点,也不好看。