有几种方法可以获得屏幕尺寸,如果这就是你所追求的。
最完整的方法是使用SystemMetrics
,它必须从“coredll.dll”中P/Invoked。
[DllImport("coredll.dll")]
public static extern int GetSystemMetrics(SYSTEM_METRICS nIndex);
接下来,创建一个枚举类型,如下所示:
public enum SYSTEM_METRICS : int
{
SM_CXSCREEN = 0,
SM_CYSCREEN = 1,
SM_CXVSCROLL = 2,
SM_CYHSCROLL = 3,
SM_CYCAPTION = 4,
SM_CXBORDER = 5,
SM_CYBORDER = 6,
SM_CXDLGFRAME = 7,
SM_CYDLGFRAME = 8,
SM_CXICON = 11,
SM_CYICON = 12,
SM_CYMENU = 15,
SM_CXFULLSCREEN = 16,
SM_CYFULLSCREEN = 17,
SM_MOUSEPRESENT = 19,
SM_CYVSCROLL = 20,
SM_CXHSCROLL = 21,
SM_DEBUG = 22,
SM_CXDOUBLECLK = 36,
SM_CYDOUBLECLK = 37,
SM_CXICONSPACING = 38,
SM_CYICONSPACING = 39,
SM_CXEDGE = 45,
SM_CYEDGE = 46,
SM_CXSMICON = 49,
SM_CYSMICON = 50,
SM_XVIRTUALSCREEN = 76,
SM_YVIRTUALSCREEN = 77,
SM_CXVIRTUALSCREEN = 78,
SM_CYVIRTUALSCREEN = 79,
SM_CMONITORS = 80,
SM_SAMEDISPLAYFORMAT = 81,
SM_CXFIXEDFRAME = 7,
SM_CYFIXEDFRAME = 8
}
有了这个框架,你可以简单地得到宽度,例如,如下:
int screenWidth = GetSystemMetrics(SYSTEM_METRICS.SM_CXSCREEN);
Console.WriteLine(String.Format("Screen X-res is {0}", screenWidth));
如果您想偷懒,简单的解决方案是在项目加载时简单地读取主窗体的宽度和高度。
当然,如果你所做的只是在调整表单之前读取宽度和高度,那么屏幕上显然会有一些闪烁。