0

さまざまな解像度の多くのデバイスとエミュレーターで動作するアプリケーションを開発しているので、実際のデバイスかエミュレーターかを知るためのコードはありますが、実行されているエミュレーター (モデル、タイプ) を知りたいです。アプリケーションを正しい解像度で表示します。

実際には、値「Emulator」またはデバイスのモデルを含む文字列を返すコード メソッド GetOemInfo() を使用しています。

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;
    }
4

3 に答える 3

0

画面サイズを取得する方法はいくつかあります。

最も完全な方法はSystemMetrics、「c​​oredll.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));

怠惰になりたい場合は、プロジェクトが読み込まれたときにメインフォームの幅と高さを読み取るだけの簡単な解決策があります。

もちろん、フォームを調整する前に幅と高さを読み取るだけの場合は、明らかに画面がちらつきます。

于 2013-04-18T14:04:11.727 に答える
0

エミュレーターには、あなたが求めているものを提供する「OEM情報」は特にありません。OS のバージョン、画面の解像度、および場合によっては一部の API の可用性を確認して、それを推測する必要があります (Pro と Standard のターゲットを区別する場合)。

于 2013-04-17T14:15:01.167 に答える
0

画面サイズがすべてではない場合は、デバイスの情報を取得するために使用する方法を次に示します。レジストリからいくつかのデータを読み取り、いくつかのデータを...まあ、わかります。

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;
}
于 2013-04-18T14:23:47.053 に答える