此前问过与我的问题类似的问题。但我认为这是独一无二的。
我有我的PC显示器和扩展显示器,我通过此代码获得了EDID和一些数据:
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\WMI", "SELECT * FROM WmiMonitorID");
foreach (ManagementObject queryObj in searcher.Get())
{
dynamic snid = queryObj["SerialNumberID"];
string serialnum = GetStringFromInt(snid);
Console.WriteLine("SerialNumber: {0}",serialnum);
Console.WriteLine("YearOfManufacture: {0}", queryObj["YearOfManufacture"]);
dynamic code = queryObj["ProductCodeID"];
string pcid = GetStringFromInt(code);
code = queryObj["ManufacturerName"];
string manufactrName = GetStringFromInt(code);
Console.WriteLine("ProductCodeID: " + pcid);
Console.WriteLine("Manufacture: " + manufactrName);
}}
所以我将Manufacture
和ProductCodeID
结合起来,就像ABC3401
和DEF4561
(我有两个监听器)。我需要将我的申请移至DEF4561
。我尝试Move Form onto specified Screen,但名称返回\\DISPLAY1
,\\DISPLAY2
等。我需要ABC3401
strng来识别监视器。我不知道如何结合这些结果。
答案 0 :(得分:0)
要完成答案,我正在填写此内容。首先,通过edokan's answer
获取监视器的详细信息 public struct DisplayInfo
{
public string DisplayID;
public string DisplayName;
public string EDID;
}
[DllImport("user32.dll")]
static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
public static List<DisplayInfo> GetConnectedMonitorInfo()
{
var MonitorCollection = new List<DisplayInfo>();
DISPLAY_DEVICE d = new DISPLAY_DEVICE();
d.cb = Marshal.SizeOf(d);
try
{
for (uint id = 0; EnumDisplayDevices(null, id, ref d, 0); id++)
{
EnumDisplayDevices(d.DeviceName, 0, ref d, 1);
if (string.IsNullOrEmpty(d.DeviceName.ToString())
&& string.IsNullOrEmpty(d.DeviceID.ToString())) continue;
var dispInfo = new DisplayInfo();
dispInfo.DisplayID = id.ToString();
dispInfo.DisplayName = d.DeviceName.ToString();
dispInfo.EDID = d.DeviceID.ToString();
if (d.DeviceID.ToString().Contains("ABC4562"))
{
//Identify Multiple monitor of same series
if (string.IsNullOrEmpty(myMonitor.myMonDevName))
{
myMonitor.myMonDevName = d.DeviceName.ToString();
}
else
{
MultipleMonitorFound = true;
}
}
MonitorCollection.Add(dispInfo);
d.cb = Marshal.SizeOf(d);
}
}
catch (Exception ex)
{
LogError(ex.ToString());
}
return MonitorCollection;
}
现在我们有了监视器列表及其详细信息。现在需要将GUI更改为该监视器,
我们需要一些方法:
//I am Using WPF
//For C# only, we can use GetActiveWindow() API.
WindowInteropHelper winHelp = new WindowInteropHelper(this);
ScreenObject sc = new ScreenObject();
//Compare the name with screens
sc.screen = sc.GetMyMonitor(myMonitor.myMonDevName);
ScreenObject
课程来自Move form onto the specified screen回答。
public static void MoveWindowToMonitor(IntPtr hWnd, int left, int top,double width, double height)
{
if (SetWindowPos(hWnd, (IntPtr)SpecialWindowHandles.HWND_TOP, left, top, (int)width, (int)height, SetWindowPosFlags.SWP_SHOWWINDOW))
LogError( " : Success");
else
LogError(" : Failed");
}
P/Invoke可以帮助你。
C#WPF用户的说明:
如果您使用Windows Presentation Foundation,则需要使用WindowInteropHelper来获取Window句柄。
确保您引用了PresentationFramework程序集。
将其插入使用块
using System.Windows.Interop;
创建WindowInteropHelper的实例
WindowInteropHelper winHelp = new WindowInteropHelper(target);
然后使用GetHctiveWindowHandle()的winHelp.Handle。
答案 1 :(得分:-1)
您必须使用EnumDisplayDevices
枚举附加到它们的显示设备和显示器,然后使用可以匹配它们。
以下是来自pinvoke.net的无耻代码副本
[Flags()]
public enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x10,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct DISPLAY_DEVICE
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
public string DeviceKey;
}
[DllImport("user32.dll")]
static extern bool EnumDisplayDevices(string lpDevice, uint iDevNum, ref DISPLAY_DEVICE lpDisplayDevice, uint dwFlags);
void Main()
{
DISPLAY_DEVICE d=new DISPLAY_DEVICE();
d.cb=Marshal.SizeOf(d);
try {
for (uint id=0; EnumDisplayDevices(null, id, ref d, 0); id++) {
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}, {4}", id, d.DeviceName, d.DeviceString, d.StateFlags, d.DeviceID));
EnumDisplayDevices(d.DeviceName, 0, ref d, 1);
Console.WriteLine(String.Format(" {0}, {1}, {2}, {3}, {4}", id, d.DeviceName, d.DeviceString, d.StateFlags, d.DeviceID));
d.cb=Marshal.SizeOf(d);
}
} catch (Exception ex) {
Console.WriteLine(String.Format("{0}",ex.ToString()));
}
}
并在我的计算机上提供以下输出
0, \\.\DISPLAY1, ATI Radeon HD 5570, 134742017, PCI\VEN_1002&DEV_68D9&SUBSYS_E142174B&REV_00
0, \\.\DISPLAY1\Monitor0, SyncMaster B2230 (Digital), AttachedToDesktop, MultiDriver, \\?\DISPLAY#SAM0635#5&15bb0574&0&UID256#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}
1, \\.\DISPLAY2, ATI Radeon HD 5570, 134742021, PCI\VEN_1002&DEV_68D9&SUBSYS_E142174B&REV_00
1, \\.\DISPLAY2\Monitor0, SyncMaster B2230 (Analog), AttachedToDesktop, MultiDriver, \\?\DISPLAY#SAM0635#5&15bb0574&0&UID257#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}
2, \\.\DISPLAYV1, RDPDD Chained DD, 2097160,
2, , , 0,
3, \\.\DISPLAYV2, RDP Encoder Mirror Driver, 2097160,
3, , , 0,
4, \\.\DISPLAYV3, RDP Reflector Display Driver, 2097160,
4, , , 0,