.NET中调用Windows API函数强制显示器指定分辨率
来源:互联网 发布:sql count 求和 编辑:程序博客网 时间:2024/05/22 18:38
案例:在软件开发过程中有时候需要使显示器的分辨率能够自适应软件设计图片或窗体的大小,因此想在软件启动的过程中重新设定显示的显示分辨率,同时当软件退出时恢复原来的分辨率设置.
分析:由于用到设置显示器分辨率的问题,因此要用到GDI的Windows API:
函数:ChangeDisplaySettings(LPDEVMODE lpDevMode,DWORD dwflags)
结构体:DEVMODE(The DEVMODE data structure contains information about the initialization and environment of a printer or a display device)
知识点:在C#中引用Windows API和结构体的方法.
System.Runtime.InteropServices 命名空间提供各种各样支持 COM interop 及平台调用服务的成员。
StructLayout(LayoutKind.Explicit)通过使用属性可以自定义结构在内存中的布局方式。
MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)指示如何在托管代码和非托管代码之间封送数据。
DllImport extern 修饰符用于声明在外部实现的方法。extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用;在这种情况下,该方法还必须声明为 static.
主要代码:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int ChangeDisplaySettings([In]ref DEVMODE lpDevMode, int dwFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplaySettings(string lpszDeviceName, Int32 iModeNum, ref DEVMODE lpDevMode);
void ChangeRes()
{
DEVMODE devM = new DEVMODE();
devM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool = EnumDisplaySettings(null, 0, ref devM);
devM.dmPelsHeight = 768;
devM.dmPelsWidth = 1024;
devM.dmDisplayFrequency = 60;
devM.dmBitsPerPel = 32;
long result = ChangeDisplaySettings(ref devM, 0);
}
private void button1_Click(object sender, EventArgs e)
{
ChangeRes();
}
- .NET中调用Windows API函数强制显示器指定分辨率
- VB.NET调用Windows API函数(整理中)
- VB.NET编程中调用Windows API
- VB.NET编程中调用Windows API
- vb.net中调用 Windows API
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- 在C#中调用windows API函数
- C# NET调用API枚举系统监视器,显示器
- C# NET调用API枚举系统监视器,显示器
- 在Windows中获取指定目录的API函数SHGetSpecialFolderPath
- 我自己写的一个消息队列服务器模型,欢迎大家指点
- 项目管理实践【五】自动编译和发布网站
- 系统建设或应用提供的目标或愿景性场景
- .NET设计规范(六) 异常
- 再牛逼的伟人,也有苦逼的青春之物理版【转载】
- .NET中调用Windows API函数强制显示器指定分辨率
- 神经网络
- C++对象的拷贝构造函数 赋值函数 调用情况
- 关于Java中Exception类的一些方法
- ubuntu软件管理大全
- zz Google Summer of Code 2011几个感兴趣的项目
- .NET设计规范(七) 使用规范
- Java是什么?
- 为你的团队提供学习的机会