目录
- 实践过程
- 效果
- 代码
实践过程
效果
代码
public partial class Form1 : Form { public Form1() { InitializeComponent(); } string MaxValue;//显示器支持的最大刷新率 string MinValue;//显示器支持的最小刷新率 string NowValue;//当前刷新率 bool flag = true; public const uint WM_SYSCOMMAND 编程= 0x0112; public const uint SC_MONITORPOWER = 0xF170; [DllImport("user32")] public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, uint wparam, int lParam); public enum DMDO { DEFAULT = 0, D90 = 1, D180 = 2, D270 = 3 } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct DEVMODE { public const int DM_DISPLAYFREQUENCY = 0x400000; public const int DM_PELSWIDTH = 0x80000; public const int DM_PELSHEIGHT = 0x100000; public const int DM_BITSPERPEL = 262144; privphpate const int CCHDEVICENAME = 32; private const int CCHFORMNAME = 32; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)] public string dmDeviceName; public short dmSpecVersion; public short dmDriverVersion; public short dmSize; public short dmDriverExtra; public int dmFields; public int dmPositionX; public int dmPositionY; public DMDO dmDisplayOrientation; public int dmDisplayFixedOutput; public short dmColor; public short dmDuplex; public short dmYResolution; public short dmTTOption; public short dmCollate; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)] public string dmFormName; public short dmLogPixels; public int dmBitsPerPel; public int dmPelsWidth; public int dmPelsHeight; public int dmDisplayFlags; public int dmDisplayFrequency; public int dmICMMethod; public int dmICMIntent; public int dmMediaType; public int dmDitherType; public int dmReserved1; public int dmReserved2; public int dmPanningWidth; public int dmPanningHeight; } [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags); private void Form1_Load(object sender, EventArgs e) { groupBox5.Text = "当前时间:" + DateTime.Now.ToString(); ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_VideoController"); foreach(ManagementObject myobject in searcher.Get() ) { string Vname = myobject["Name"].ToString(); if (Vname.Length >40) { string a = Vname.Substring(0,45); string b = Vname.Substring(46); lblInfo.Text = a + "\n" + b; } else { lblInfo.Text = Vname; } string colorValue = myobject["CurrentNumberOfColors"].ToString(); if (colorValue == "65536") { comboBox1.SelectedIndex = 0; } else { comboBox1.SelectedIndex = 1; } MaxValue=myobject["MaxRefreshRate"].ToString(); if (Convert.ToInt32(MaxValue) > 85) MaxValue = "85"; MinValue = myobject["MinRefreshRate"].ToString(); NowValue=myobject["CurrentRefreshRate"].ToString(); AddHZ(); GetDis(); ChangeDis(1); } } int dWidth = 0; int dHeight = 0; private void GetDis()//获取分辨率 { Size s = SystemInformation.PrimaryMonitorSize; dWidth = s.Width; dHeight = s.Height; lblDisInfo.Text = dWidth.ToString() + " " + dHeight.ToString() + " 像素"; } private void ChangeDis(int i)//变换分辨率 { int dValue; dValue = trackBar1.Value; if (i == 0) { switch (dValue) { case 0: dWidth = 800; dHeight = 600; break; case 1: dWidth = 1024; dHeight = 768; break; case 2: dWidth = 1152; dHeight = 864; break; case 3: dWidth = 1280; dHeight = 600; break; case 4: dWidth = 1280; dHeight = 720; break; case 5: dWidth = 1280; dHeight = 768; break; case 6: dWidth = 1280; dHeight = 760; break; case 7: dWidth = 1280; dHeight = 1024; break; case 8: dWidth = 1400; dHeight = 1050; break; case 9: dWidth = 1600; dHeight = 900; break; case 10: dWidth = 1600; dHeight = 1200; break; } lblDisInfo.Text = dWidth.ToString() + " " + dHeight.ToString() + " 像素"; } else { int dSum=dWidth+dHeight; switch (dSum) { case 1400: trackBar1.Value = 0; break; case 1792: trackBar1.Value = 1; break; case 2016: trackBar1.Value = 2; break; case 1880: trackBar1.Value = 3; break; case 2000: trackBar1.Value = 4; break; case 2048: trackBar1.Value = 5; break; case 2240: trackBar1.Value = 6; break; case 2304: trackBar1.Value = 7; break; case 2450: trackBar1.Value = 8; break; case 2500: trackBar1.Value = 9; break; case 2800: trackBar1.Value = 10; break; } } } private void AddHZ() { int[] hz = new int[] { 60, 70, 75, 85, 100, 120 }; comboBox2.Items.Clear(); if (checkBox1.Checked) { for (int i = 0; i < hz.Length; i++) { if (hz[i] <= Convert.ToInt32(MaxValue)) { comboBox2.Items.Add(hz[i].ToString() + "赫兹"); } } comboBox2.Text = NowValue + "赫兹"; } else { for (int j = 0; j < hz.Length; j++) { comboBox2.Items.Add(hz[j].ToString() + "赫兹"); } comboBox2.Text = NowValue + "赫兹"; } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { AddHZ(); } private void button2_Click(object sender, EventArgs e) { Application.Exit(); } private void trackBar1_Scroll(object sender, EventArgs e) { ChangeDis(0); } private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { groupBox1.Enabled = true; groupBox2.Enabled = true; groupBox3.Enabled = true; groupBox4.Enabled = true; } else { groupBox1.Enabled = false; groupBox2.Enabled = false; groupBox3.Enabled = false; groupBox4.Enabled = false; } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { groupBox5.Enabled = true; } else { groupBox5.Enabled = false; } } private void button1_Click(object sender, EventArgs e) { if (radioButton1.Checked)//设置分辨率、颜色质量、刷新率 { long RetVal = 0; DEVMODE dm = new DEVMODE(); dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE)); dm.dmPelsWidth = dWidth;//宽 dm.dmPelsHeight = dHeight;//高 int f =Convert.ToInt32(comboBox2.Text.Trim().Remove(comboBox2.Text.Trim().Length-2)); dm.dmDisplayFrequency = f;//刷新率 if (comboBox1.SelectedIndex == 0) dm.dmBitsPerPel = 16; else dm.dmBitsPerPel = 32; dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY | DEVMODE.DM_BITSPERPEL; RetVal = ChangeDisplaySettings(ref dm, 0); } if (radioButton2.Checked)//关闭显示器 { timer1.Start(); this.Close(); } } private void timer1_Tick(object sender, EventArgs e) { if (dateTimePicker1.Text == DateTime.Now.ToLongTimeString()) { SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2); timer1.Stop(); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { this.Hide(); if (flag) { e.Cancel = true; } } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { flag = false; Application.Exit(); } private void 显示ToolStripMenuItem_Click(object sender, EventArgs e) { this.Show(); } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { this.Show(); } private void timer2_Tick(object sender, EventArgs e) { groupBox5.Text = "当前时间:" + DateTime.Now.ToString(); } }
partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.lblDisInfo = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.trackBar1 = new System.Windows.Forms.TrackBar(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.lblInfo = new System.Windows.Forms.Label(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.groupBox4 = new System.Windows.Forms.GroupBox(); this.checkBox1 = new System.Windows.Forms.CheckBox(); this.comboBox2 = new System.Windows.Forms.ComboBox(); this.groupBox5 = new System.Windows.Forms.GroupBox(); this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker(); this.label4 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.radioButton1 = new System.Windows.Forms.RadioButton(); this.radioButton2 = new System.Windows.Forms.RadioButton(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.显示ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.timer2 = new System.Windows.Forms.Timer(this.components); this.groupBox1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.groupBox4.SuspendLayout(); this.groupBox5.SuspendLayout(); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.lblDisInfo); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.trackBar1); this.groupBox1.Location = new System.Drawing.Point(11, 96); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(161, 81); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "屏幕分辨率"; // // lblDisInfo // this.lblDisInfo.AutoSize = true; this.lblDisInfo.Location = new System.Drawing.Point(36, 57); this.lblDisInfo.Name = "lblDisInfo"; this.lblDisInfo.Size = new System.Drawing.Size(41, 12); this.lblDisInfo.TabIndex = 3; this.lblDisInfo.Text = "label3"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(134, 29); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(17, 12); this.label2.TabIndex = 2; this.label2.Text = "多"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(15, 29); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(17, 12); this.label1.TabIndex = 1; this.label1.Text = "少"; // // trackBar1 // this.trackBar1.AutoSize = false; this.trackBar1.Location = new System.Drawing.Point(33, 26); this.trackBar1.Name = "trackBar1"; this.trackBar1.Size = new System.Drawing.Size(95, 23); this.trackBar1.TabIndex = 0; this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll); // // groupBox2 // this.groupBox2.Controls.Add(this.lblInfo); this.groupBox2.Location = new System.Drawing.Point(12, 36); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(328, 53); this.groupBox2.TabIndex = 2; this.groupBox2.TabStop = false; this.groupBox2.Text = "显示设备"; // // lblInfo // this.lblInfo.AutoSize = true; this.lblInfo.Location = new System.Drawing.Point(10, 20); this.lblInfo.Name = "lblInfo"; this.lblInfo.Size = new System.Drawing.Size(41, 12); this.lblInfo.TabIndex = 0; this.lblInfo.Text = "label3"; // // groupBox3 // this.groupBox3.Controls.Add(this.comboBox1); this.groupBox3.Location = new System.Drawing.Point(178, 96); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(161, 81); this.groupBox3.TabIndex = 3; this.groupBox3.TabStop = false; this.groupBox3.Text = "颜色质量"; // // comboBox1 // this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox1.FormattingEnabled = true; this.comboBox1.Items.AddRange(new object[] { "中(16位)", "最高(32位)"}); this.comboBox1.Location = new System.Drawing.Point(7, 31); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(148, 20); this.comboBox1.TabIndex = 0; // // groupBox4 // this.groupBox4.Controls.Add(this.checkBox1); this.groupBox4.Controls.Add(this.comboBox2); this.groupBox4.Location = new System.Drawing.Point(12, 184); this.groupBox4.Name = "groupBox4"; this.groupBox4.Size = new System.Drawing.Size(327, 85); this.groupBox4.TabIndex = 4; this.groupBox4.TabStop = false; this.groupBox4.Text = "设置刷新频率"; // // checkBox1 // this.checkBox1.AutoSize = true; this.checkBox1.Checked = true; this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBox1.Location = new System.Drawing.Point(7, 56); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(180, 16); this.checkBox1.TabIndex = 1; this.checkBox1.Text = "隐藏该显示器无法显示的模式"; this.checkBox1.UseVisualStyleBackColor = true; this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); // // comboBox2 // this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBox2.FormattingEnabled = true; this.comboBox2.Items.AddRange(new object[] { "60赫兹", "70赫兹", "75赫兹", "85赫兹", "100赫兹", "120赫兹"}); this.comboBox2.Location = new System.Drawing.Point(6, 20); this.comboBox2.Name = "comboBox2"; this.comboBox2.Size = new System.Drawing.Size(315, 20); this.comboBox2.TabIndex = 0; // // groupBox5 // this.groupBox5.Controls.Add(this.dateTimePicker1); this.groupBox5.Controls.Add(this.label4); this.groupBox5.Enabled = false; this.groupBox5.Location = new System.Drawing.Point(12, 296); this.groupBox5.Name = "groupBox5"; this.groupBox5.Size = new System.Drawing.Size(328, 65); this.groupBox5.TabIndex = 5; this.groupBox5.TabStop = false; // // dateTimePicker1 // this.dateTimePicker1.Checked = false; this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Time; this.dateTimePicker1.Location = new System.Drawing.Point(94, 24); this.dateTimePicker1.Name = "dateTimePicker1"; this.dateTimePicker1.ShowCheckBox = true; this.dateTimePicker1.ShowUpDown = true; this.dateTimePicker1.Size = new System.Drawing.Size(118, 21); this.dateTimePicker1.TabIndex = 1; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(11, 28); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(77, 12); this.label4.TabIndex = 0; this.label4.Text = "关闭显示器:"; // // button1 // this.button1.Location = new System.Drawing.Point(84, 372); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 6; this.button1.Text = "确定"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(177, 372); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 7; this.button2.Text = "取消"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // radioButton1 // this.radioButton1.AutoSize = true; this.radioButton1.Checked = true; this.radioButton1.Location = new System.Drawing.Point(11, 12); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(71, 16); this.radioButton1.TabIndex = 8; this.radioButton1.TabStop = true; this.radioButton1.Text = "基本设置"; this.radioButtopythonn1.UseVisualStyleBackColor = true; this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); // // radioButton2 // this.radioButton2.AutoSize = true; this.radioButton2.Location = new System.Drawing.Point(11, 278); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(71, 16); this.radioButton2.TabIndex = 9; this.radioButton2.Text = "电源设置"; this.radioButton2.UseVisualStyleBackColor = true; this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged); // // timer1 // this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // notifyIcon1 // this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); this.notifyIcon1.Text = "显示器控制"; this.notifyIcon1.Visible = true; this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.Too编程客栈lStripItem[] { this.显示ToolStripMenuItem, this.退出ToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(95, 48); // // 显示ToolStripMenuItem // this.显示ToolStripMenuItem.Name = "显示ToolStripMenuItem"; this.显示ToolStripMenuItem.Size = new System.Drawing.Size(94, 22); this.显示ToolStripMenuItem.Text = "显示"; this.显示ToolStripMenuItem.Click += new System.EventHandler(this.显示TojsolStripMenuItem_Click); // // 退出ToolStripMenuItem // this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem"; this.退出ToolStripMenuItem.Size = new System.Drawing.Size(94, 22); this.退出ToolStripMenuItem.Text = "退出"; this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click); // // timer2 // this.timer2.Enabled = true; this.timer2.Interval = 1000; this.timer2.Tick += new System.EventHandler(this.timer2_Tick); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(351, 403); this.Controls.Add(this.radioButton2); this.Controls.Add(this.radioButton1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.groupBox5); this.Controls.Add(this.groupBox4); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixe开发者_开发入门dSingle; this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "显示器控制"; this.Load += new System.EventHandler(this.Form1_Load); this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.groupBox3.ResumeLayout(false); this.groupBox4.ResumeLayout(false); this.groupBox4.PerformLayout(); this.groupBox5.ResumeLayout(false); this.groupBox5.PerformLayout(); this.contextMenuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; private System.Windows.Forms.TrackBar trackBar1; private System.Windows.Forms.Label lblDisInfo; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.GroupBox groupBox4; private System.Windows.Forms.ComboBox comboBox2; private System.Windows.Forms.CheckBox checkBox1; private System.Windows.Forms.GroupBox groupBox5; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label lblInfo; private System.Windows.Forms.RadioButton radioButton1; private System.Windows.Forms.RadioButton radioButton2; private System.Windows.Forms.DateTimePicker dateTimePicker1; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem 显示ToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem; private System.Windows.Forms.Timer timer2; }
到此这篇关于C#实现设置电脑显示器参数的文章就介绍到这了,更多相关C#设置电脑显示器参数内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论