Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Windows.Forms.Screen.DeviceName在Windows XP上提供垃圾_C#_.net_Winforms_Windows Xp_Screen - Fatal编程技术网

C# System.Windows.Forms.Screen.DeviceName在Windows XP上提供垃圾

C# System.Windows.Forms.Screen.DeviceName在Windows XP上提供垃圾,c#,.net,winforms,windows-xp,screen,C#,.net,Winforms,Windows Xp,Screen,我的3.5.NET Framework程序处理多个监视器,因此我使用它来区分不同的监视器(不能只比较对屏幕对象实例的引用--不同的实例可以引用同一屏幕) 该程序在Windows 7上运行没有问题,但在安装了所有.NET框架的Windows XP SP3上,它会随机做一些奇怪的事情,好像它没有意识到两个给定屏幕实际上是同一个屏幕,它应该能够识别,因为它们应该具有相同的DeviceNames 问题是什么?我该如何解决它?框架或Windows XP中似乎有一个bug 如果在Windows 7下转储Sc

我的3.5.NET Framework程序处理多个监视器,因此我使用它来区分不同的监视器(不能只比较对
屏幕
对象实例的引用--不同的实例可以引用同一屏幕)

该程序在Windows 7上运行没有问题,但在安装了所有.NET框架的Windows XP SP3上,它会随机做一些奇怪的事情,好像它没有意识到两个给定屏幕实际上是同一个屏幕,它应该能够识别,因为它们应该具有相同的
DeviceName
s


问题是什么?我该如何解决它?

框架或Windows XP中似乎有一个bug

如果在Windows 7下转储
Screen.DeviceName
,您将得到如下结果

如果在Windows XP上运行此代码段,您将得到如下结果:

请注意,此处至少有三个版本的
DeviceName

相反,在Windows7上,垃圾部分将被剥离

这就是代码无法识别屏幕的原因——设备名称每次都不同

要解决此问题,请在第一个
'\0'
字符后裁剪
DeviceName
字符串

\\.\DISPLAY1 \\.\DISPLAY2 \\.\DISPLAY1 ????A??M?↕?☺ ? \\.\DISPLAY2 ????☺ ? ☺ ?????
System.Windows.Forms.Screen s = null;
System.Drawing.Point p = new System.Drawing.Point(0,0);

Console.WriteLine("Using same instance of Screen:");
s = System.Windows.Forms.Screen.FromPoint(p);
for (int i = 0; i < 5; ++i)
{
    Console.WriteLine(s.DeviceName);
}

Console.WriteLine();

Console.WriteLine("Using new instance of Screen:");
for (int i = 0; i < 5; ++i)
{
    Console.WriteLine(System.Windows.Forms.Screen.FromPoint(p).DeviceName);
}