发 帖  
原厂入驻New

C#中改变显示器的分辨率

2008-12-29 11:13:19  1711
分享
<font face="Verdana">C#中改变显示器的分辨率</font><br/>
<p><font face="Verdana">C#中改变显示器的分辨率</font></p>
<p><font face="Verdana">________________________________________<br/>下面的代码实现修改显示器分辨率和刷新频率的功能:<br/>using System;<br/>using System.Drawing;<br/>using System.Collections;<br/>using System.ComponentModel;<br/>using System.Windows.Forms;<br/>using System.Data;<br/>using System.Runtime.InteropServices;</font></p>
<p><font face="Verdana">namespace ScreenResolution<br/>{</font></p>
<p><font face="Verdana">&nbsp; public class Form1 : System.Windows.Forms.Form<br/>&nbsp; {<br/>&nbsp;&nbsp;&nbsp; public enum DMDO<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEFAULT = 0,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; D90 = 1,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; D180 = 2,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; D270 = 3<br/>&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct DEVMODE<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public const int DM_displayFREQUENCY = 0x400000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public const int DM_PELSWIDTH = 0x80000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public const int DM_PELSHEIGHT = 0x100000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private const int CCHDEVICENAME = 32;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private const int CCHFORMNAME = 32;</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string dmDeviceName;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmSpecVersion;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmDriverVersion;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmSize;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmDriverExtra;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmFields;</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmPositionX;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmPositionY;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public DMDO dmDisplayOrientation;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmDisplayFixedOutput;</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmColor;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmDuplex;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmYResolution;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmTTOption;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmCollate;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHFORMNAME)]<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string dmFormName;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public short dmLogPixels;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmBitsPerPel;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmPelsWidth;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmPelsHeight;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmDisplayFlags;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmDisplayFrequency;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmICMMethod;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmICMIntent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmMediaType;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmDitherType;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmReserved1;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmReserved2;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmPanningWidth;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public int dmPanningHeight;<br/>&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; [DllImport("user32.dll", CharSet=CharSet.Auto)]<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //static extern int ChangeDisplaySettings( DEVMODE lpDevMode,&nbsp; int dwFlags);</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; static extern int ChangeDisplaySettings( [In] ref DEVMODE lpDevMode,&nbsp; int dwFlags);<br/>&nbsp;&nbsp;&nbsp; private System.ComponentModel.Container components = null;<br/>&nbsp;&nbsp;&nbsp; public Form1()<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InitializeComponent();<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp; protected override void Dispose( bool disposing )<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF( disposing )<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (components != null)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; components.Dispose();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; base.Dispose( disposing );<br/>&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; #region Windows Form Designer generated code<br/>&nbsp;&nbsp;&nbsp; private void InitializeComponent()<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.ClientSize = new System.Drawing.Size(292, 273);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Text = "改变屏幕分辨率的例子";</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp; #endregion</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; static void Main()<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Form1 r = new Form1();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r.ChangeRes();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Application.Run(new Form1());<br/>&nbsp;&nbsp;&nbsp; }</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp; void ChangeRes()<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Form1 t = new Form1();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long RetVal=0;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEVMODE dm = new DEVMODE();<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dm.dmSize= (short)Marshal.SizeOf(typeof(DEVMODE));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dm.dmPelsWidth = 1024;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dm.dmPelsHeight= 768;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dm.dmDisplayFrequency=85;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RetVal = ChangeDisplaySettings(ref dm, 0);<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp; }<br/>} </font></p>
<p><font face="Verdana"></font>&nbsp;</p>
0
2008-12-29 11:13:19   评论 分享淘帖

只有小组成员才能发言,加入小组>>

135个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /7 下一条

快速回复 返回顶部 返回列表