本实验是使用C#获取电脑显示屏屏幕的尺寸。
使用的API是GetDeviceCaps,
GetDeviceCaps(
UINT Adapter,
D3DDEVTYPE DeviceType
D3DCAPS8*pCaps
)
由于这个函数属于C++的标准函数,其参数较多,这里使用C#已经封装好的Screen对象。
textBox2.Text = Screen.PrimaryScreen.WorkingArea.Height.ToString();
textBox1.Text = Screen.PrimaryScreen.WorkingArea.Width.ToString();
下图是我的电脑运行的结果。
具体实现办法是:
1.创建一个项目,命名为DeskSize,默认窗体为Form1。
2.在窗口上年添加一个按钮和两个文本显示编辑框。
3.添加按钮事件,并在按钮事件中添加如下代码。
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Screen.PrimaryScreen.WorkingArea.Height.ToString();
textBox1.Text = Screen.PrimaryScreen.WorkingArea.Width.ToString();
}
根据显示器的分辨率,可以设置软件的窗体尺寸。