一、问题描述
采用C#开发在WINCE环境下运行的PDA程序,不同的RF手持设备有不同的分辨率,现在根据RF设备的分辨率自动调整界面大小,做到自适应分辨率。主要调用C# API的GetSystemMetrics(int nIndex)方法。
二、解决思路
引用系统再带的库,并根据需要设定控件大小。
三、代码
步骤:
1、引用命名空间
using System.Runtime.InteropServices;
2、在public partial class LoginForm : BaseForm {}中引用外部方法
[DllImport("user32")]
public static extern int GetSystemMetrics(int nIndex);
3、在控件大小位置引用GetSystemMetrics()方法
his.ClientSize = new System.Drawing.Size(GetSystemMetrics(0)-2, GetSystemMetrics(1)-5);
其中,参数0表示屏幕宽度,1表示屏幕高度,其他数字的含义请见下表:
SM_CXSCREEN = 0屏幕宽度
SM_CYSCREEN = 1屏幕高度
SM_CXVSCROLL = 2垂直滚动条的宽度
SM_CYHSCROLL = 3水平滚动条的宽度
SM_CYCAPTION = 4Height of windows caption 实际标题高度加上SM_CYBORDER
SM_CXBORDER = 5Width of no-sizable borders 无法测量的窗口框架宽度
SM_CYBORDER = 6Height of non-sizable borders 无法测量的窗口框架高度
SM_CXDLGFRAME = 7Width of dialog box borders
SM_CYDLGFRAME = 8Height of dialog box borders
SM_CYHTHUMB = 9Height of scroll box on horizontal scroll bar 水平滚动条上滑块的高度
SM_CXHTHUMB = 10Width of scroll box on horizontal scroll bar 水平滚动条上滑块的宽度
SM_CXICON = 11Width of standard icon 图标宽度
SM_CYICON = 12Height of standard icon 图标高度
SM_CXCURSOR = 13Width of standard cursor 光标宽度
SM_CYCURSOR = 14Height of standard cursor 光标高度
SM_CYMENU = 15Height of menu 以像素计算的单个菜单条的高度
SM_CXFULLSCREEN = 16Width of client area of maximized window
SM_CYFULLSCREEN = 17Height of client area of maximized window
SM_CYKANJIWINDOW = 18Height of Kanji window
SM_MOUSEPRESENT = 19True is a mouse is present 如果为TRUE或不为0的值则安装了鼠标,否则没有安装。
SM_CYVSCROLL = 20Height of arrow in vertical scroll bar
SM_CXHSCROLL = 21Width of arrow in vertical scroll bar
SM_DEBUG = 22True if deugging version of windows is running
SM_SWAPBUTTON = 23True if left and right buttons are swapped.
SM_CXMIN = 28Minimum width of window
SM_CYMIN = 29Minimum height of window
SM_CXSIZE = 30Width of title bar bitmaps
SM_CYSIZE = 31height of title bar bitmaps
SM_CXMINTRACK = 34Minimum tracking width of window
SM_CYMINTRACK = 35Minimum tracking height of window
SM_CXDOUBLECLK = 36double click width
SM_CYDOUBLECLK = 37double click height
SM_CXICONSPACING = 38width between desktop icons
SM_CYICONSPACING = 39height between desktop icons
SM_MENUDROPALIGNMENT = 40Zero if popup menus are aligned to the left of the memu bar item. True if it is aligned to the right.
SM_PENWINDOWS = 41The handle of the pen windows DLL if loaded.
SM_DBCSENABLED = 42True if double byte characteds are enabled
SM_CMOUSEBUTTONS = 43Number of mouse buttons.
SM_CMETRICS = 44Number of system metrics
SM_CLEANBOOT = 67Windows 95 boot mode. 0 = normal, 1 = safe, 2 = safe with network
SM_CXMAXIMIZED = 61default width of win95 maximised window
SM_CXMAXTRACK = 59maximum width when resizing win95 windows
SM_CXMENUCHECK = 71width of menu checkmark bitmap
SM_CXMENUSIZE = 54width of button on menu bar
SM_CXMINIMIZED = 57width of rectangle into which minimised windows must fit.
SM_CYMAXIMIZED = 62default height of win95 maximised window
SM_CYMAXTRACK = 60maximum width when resizing win95 windows
SM_CYMENUCHECK = 72height of menu checkmark bitmap
SM_CYMENUSIZE = 55height of button on menu bar
SM_CYMINIMIZED = 58height of rectangle into which minimised windows must fit.
SM_CYSMCAPTION = 51height of windows 95 small caption
SM_MIDEASTENABLED = 74Hebrw and Arabic enabled for windows 95
SM_NETWORK = 63bit o is set if a network is present.
SM_SECURE = 44True if security is present on windows 95 system
SM_SLOWMACHINE = 73true if machine is too slow to run win95.