Screen.PrimaryScreen.WorkingArea.Width获取的与实际电脑桌面不一致
时间: 2024-02-03 10:02:23 浏览: 17
如果使用Screen.PrimaryScreen.WorkingArea.Width获取的分辨率与实际电脑桌面不一致,可能是因为您的电脑设置了缩放比例,导致屏幕的分辨率与实际分辨率不一致。此外,如果您的电脑连接了多个显示器,那么获取到的分辨率可能是其中一个显示器的分辨率。
您可以尝试使用System.Windows.Forms.Screen类的AllScreens属性来获取所有的屏幕信息,然后分别获取它们的分辨率,示例代码如下:
```
using System.Windows.Forms;
...
// 获取所有屏幕分辨率
foreach (Screen screen in Screen.AllScreens)
{
int screenWidth = screen.Bounds.Width;
int screenHeight = screen.Bounds.Height;
Console.WriteLine("Screen Resolution: {0} x {1}", screenWidth, screenHeight);
}
```
这样可以获取所有显示器的分辨率信息,希望能够帮助到您。
相关问题
Screen.PrimaryScreen.Bounds.Width 获取的与实际电脑桌面不一致
如果使用Screen.PrimaryScreen.Bounds.Width获取的分辨率与实际电脑桌面不一致,可能是因为您的电脑设置了缩放比例,导致屏幕的分辨率与实际分辨率不一致。
在这种情况下,可以使用System.Windows.Forms.Screen类的WorkingArea属性,该属性获取的是除任务栏等系统元素外的工作区域大小,可以避免受到缩放比例的影响,示例代码如下:
```
using System.Windows.Forms;
...
// 获取当前屏幕工作区域分辨率
int screenWidth = Screen.PrimaryScreen.WorkingArea.Width;
int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
// 输出屏幕分辨率
Console.WriteLine("Screen Resolution: {0} x {1}", screenWidth, screenHeight);
```
这样获取的分辨率就与实际电脑桌面一致了。希望能够帮助到您。
怎么将窗体大小设置为 Screen.PrimaryScreen.Bounds
您可以使用以下代码将窗体大小设置为屏幕大小:
```csharp
this.Width = Screen.PrimaryScreen.Bounds.Width;
this.Height = Screen.PrimaryScreen.Bounds.Height;
```
其中,`Screen.PrimaryScreen.Bounds` 是一个 `Rectangle` 类型的对象,它包含了屏幕的位置和大小信息。我们可以使用 `Bounds.Width` 和 `Bounds.Height` 属性获取屏幕的宽度和高度,并将窗体的宽度和高度设置为相应的值。