C# 如何以编程方式获取仿真器';正在使用的s模型
我正在开发一个应用程序,以使用不同分辨率的许多设备和模拟器,因此我有代码知道是真实的设备还是模拟器,但我想知道正在执行哪个模拟器(型号、类型),以正确的分辨率显示应用程序 实际上,我正在使用代码方法GetOemInfo(),它返回一个值为“Emulator”的字符串或设备的模型,我只知道这是一个仿真器,而不是什么C# 如何以编程方式获取仿真器';正在使用的s模型,c#,visual-studio-2008,mobile,windows-mobile,compact-framework,C#,Visual Studio 2008,Mobile,Windows Mobile,Compact Framework,我正在开发一个应用程序,以使用不同分辨率的许多设备和模拟器,因此我有代码知道是真实的设备还是模拟器,但我想知道正在执行哪个模拟器(型号、类型),以正确的分辨率显示应用程序 实际上,我正在使用代码方法GetOemInfo(),它返回一个值为“Emulator”的字符串或设备的模型,我只知道这是一个仿真器,而不是什么 private static string GetOemInfo() { string oemInfo = new string(' ', 50);
private static string GetOemInfo()
{
string oemInfo = new string(' ', 50);
int result = SystemParametersInfo(SPI_GETOEMINFO, 50, oemInfo, 0);
oemInfo = oemInfo.Substring(0, oemInfo.IndexOf('\0'));
return oemInfo;
}
模拟器没有明确的“OEM信息”来告诉你你想要什么。您必须通过检查操作系统版本、屏幕分辨率,甚至可能是某些API的可用性(如果您正在区分Pro和标准目标的话)来推断它。如果您只想了解屏幕大小,有几种方法可供选择 最完整的方法是使用
SystemMetrics
,它必须从“coredll.dll”进行p/调用
接下来,创建一个枚举类型,如下所示:
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
}
使用此框架all set,您可以简单地获得宽度,例如,如下所示:
int screenWidth = GetSystemMetrics(SYSTEM_METRICS.SM_CXSCREEN);
Console.WriteLine(String.Format("Screen X-res is {0}", screenWidth));
如果你想偷懒,一个简单的解决方案就是在你的项目加载后,简单地读取主窗体的宽度和高度
当然,如果您所做的只是在调整表单之前读取宽度和高度,那么屏幕上显然会出现一些闪烁。如果屏幕大小不是您所追求的全部,那么下面是我用来获取设备信息的方法。它从注册表读取一些数据,从。。。好吧,你会看到的
private static string getName() {
string name = null;
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Ident", true)) {
var name = key.GetValue("Name", [Unnamed]) as string;
}
if (String.IsNullOrEmpty(name)) {
name = getDeviceID();
}
return name;
}
因此,现在您需要我的getDeviceID()
。啊。嗯
private const string COREDLL = "coredll.dll";
[DllImport(COREDLL)]
private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned);
private static string getDeviceID() {
// Reference: http://msdn.microsoft.com/en-us/library/aa446562.aspx
byte[] data = new byte[256];
Int32 OutputBufferSize = data.Length;
Int32 BytesReturned = 0;
// Call KernelIoControl passing the previously defined IOCTL_HAL_GET_DEVICEID parameter
// We don’t need to pass any input buffers to this call
// so InputBuffer and InputBufferSize are set to their null values
bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, data, OutputBufferSize, ref BytesReturned);
// If the request failed, exit the method now
if (retVal) {
// Examine the OutputBuffer byte array to find the start of the
// Preset ID and Platform ID, as well as the size of the PlatformID.
// PresetIDOffset – The number of bytes the preset ID is offset from the beginning of the structure
// PlatformIDOffset - The number of bytes the platform ID is offset from the beginning of the structure
// PlatformIDSize - The number of bytes used to store the platform ID
// Use BitConverter.ToInt32() to convert from byte[] to int
Int32 PresetIDOffset = BitConverter.ToInt32(data, 4);
Int32 PlatformIDOffset = BitConverter.ToInt32(data, 0xc);
Int32 PlatformIDSize = BitConverter.ToInt32(data, 0x10);
// Convert the Preset ID segments into a string so they can be
// displayed easily.
StringBuilder sb = new StringBuilder();
sb.Append(String.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
BitConverter.ToInt32(data, PresetIDOffset),
BitConverter.ToInt16(data, PresetIDOffset + 4),
BitConverter.ToInt16(data, PresetIDOffset + 6),
BitConverter.ToInt16(data, PresetIDOffset + 8)));
// Break the Platform ID down into 2-digit hexadecimal numbers
// and append them to the Preset ID. This will result in a
// string-formatted Device ID
for (int i = PlatformIDOffset; i < PlatformIDOffset + PlatformIDSize; i++) {
sb.Append(String.Format("{0:X2}", data[i]));
}
// return the Device ID string
return sb.ToString();
}
return null;
}
私有常量字符串COREDLL=“COREDLL.dll”;
[DllImport(COREDLL)]
私有静态外部bool KernelIoControl(Int32 IoControlCode、IntPtr InputBuffer、Int32 InputBufferSize、byte[]OutputBuffer、Int32 OutputBufferSize、ref Int32 BytesReturned);
私有静态字符串getDeviceID(){
//参考:http://msdn.microsoft.com/en-us/library/aa446562.aspx
字节[]数据=新字节[256];
Int32 OutputBufferSize=data.Length;
Int32字节返回=0;
//通过前面定义的IOCTL\u HAL\u GET\u DEVICEID参数调用KernelIoControl
//我们不需要向这个调用传递任何输入缓冲区
//因此,InputBuffer和InputBufferSize被设置为空值
bool retVal=KernelIoControl(IOCTL_HAL_GET_DEVICEID,IntPtr.Zero,0,data,OutputBufferSize,ref bytes返回);
//如果请求失败,请立即退出该方法
如果(返回){
//检查OutputBuffer字节数组以查找
//预设ID和平台ID,以及平台ID的大小。
//PresetIDOffset–预设ID从结构开头偏移的字节数
//PlatformIDOffset—平台ID从结构开头偏移的字节数
//PlatformIDSize—用于存储平台ID的字节数
//使用BitConverter.ToInt32()将字节[]转换为int
Int32 PresetIDOffset=位转换器.ToInt32(数据,4);
Int32 PlatformIDOffset=位转换器.ToInt32(数据,0xc);
Int32 PlatformIDSize=位转换器.ToInt32(数据,0x10);
//将预设的ID段转换为字符串,以便
//容易展示。
StringBuilder sb=新的StringBuilder();
sb.Append(String.Format(“{0:X8}-{1:X4}-{2:X4}-{3:X4}-”,
位转换器.ToInt32(数据、预设偏移),
位转换器.ToInt16(数据,预设偏移+4),
位转换器.ToInt16(数据,预设偏移+6),
位转换器.ToInt16(数据,预设偏移+8));
//将平台ID分解为两位十六进制数字
//并将它们附加到预设ID。这将导致
//字符串格式的设备ID
for(inti=PlatformIDOffset;iprivate const string COREDLL = "coredll.dll";
[DllImport(COREDLL)]
private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32 OutputBufferSize, ref Int32 BytesReturned);
private static string getDeviceID() {
// Reference: http://msdn.microsoft.com/en-us/library/aa446562.aspx
byte[] data = new byte[256];
Int32 OutputBufferSize = data.Length;
Int32 BytesReturned = 0;
// Call KernelIoControl passing the previously defined IOCTL_HAL_GET_DEVICEID parameter
// We don’t need to pass any input buffers to this call
// so InputBuffer and InputBufferSize are set to their null values
bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, data, OutputBufferSize, ref BytesReturned);
// If the request failed, exit the method now
if (retVal) {
// Examine the OutputBuffer byte array to find the start of the
// Preset ID and Platform ID, as well as the size of the PlatformID.
// PresetIDOffset – The number of bytes the preset ID is offset from the beginning of the structure
// PlatformIDOffset - The number of bytes the platform ID is offset from the beginning of the structure
// PlatformIDSize - The number of bytes used to store the platform ID
// Use BitConverter.ToInt32() to convert from byte[] to int
Int32 PresetIDOffset = BitConverter.ToInt32(data, 4);
Int32 PlatformIDOffset = BitConverter.ToInt32(data, 0xc);
Int32 PlatformIDSize = BitConverter.ToInt32(data, 0x10);
// Convert the Preset ID segments into a string so they can be
// displayed easily.
StringBuilder sb = new StringBuilder();
sb.Append(String.Format("{0:X8}-{1:X4}-{2:X4}-{3:X4}-",
BitConverter.ToInt32(data, PresetIDOffset),
BitConverter.ToInt16(data, PresetIDOffset + 4),
BitConverter.ToInt16(data, PresetIDOffset + 6),
BitConverter.ToInt16(data, PresetIDOffset + 8)));
// Break the Platform ID down into 2-digit hexadecimal numbers
// and append them to the Preset ID. This will result in a
// string-formatted Device ID
for (int i = PlatformIDOffset; i < PlatformIDOffset + PlatformIDSize; i++) {
sb.Append(String.Format("{0:X2}", data[i]));
}
// return the Device ID string
return sb.ToString();
}
return null;
}