C#实现程序控制光驱门开/关
其实十分简单调用一个api函数就可以搞定了winmm.dll中的CDdoor
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7
8namespace CDDoor
9{
10 /// <summary>
11 /// Form1 的摘要说明。
12 /// </summary>
13 public class Form1 : System.Windows.Forms.Form
14 {
15 private System.Windows.Forms.Button button1;
16 /// <summary>
17 /// 必需的设计器变量。
18 /// </summary>
19 private System.ComponentModel.Container components = null;
20
21 [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint="mciSendStringA")]
22 private static extern long CDdoor(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);
23
24 private bool CDOpen = true;
25
26 public Form1()
27 {
28 InitializeComponent();
29
30 this.button1.Text = "点击关闭光驱";
31 CDdoor("set CDAudio door open", "0", 0, 0);
32 }
33
34 /// <summary>
35 /// 清理所有正在使用的资源。
36 /// </summary>
37 protected override void Dispose( bool disposing )
38 {
39 if( disposing )
40 {
41 if (components != null)
42 {
43 components.Dispose();
44 }
45 }
46 base.Dispose( disposing );
47 }
48
49 #region Windows 窗体设计器生成的代码
50 /// <summary>
51 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
52 /// 此方法的内容。
53 /// </summary>
54 private void InitializeComponent()
55 {
56 this.button1 = new System.Windows.Forms.Button();
57 this.SuspendLayout();
58 //
59 // button1
60 //
61 this.button1.Location = new System.Drawing.Point(104, 128);
62 this.button1.Name = "button1";
63 this.button1.TabIndex = 0;
64 this.button1.Click += new System.EventHandler(this.button1_Click);
65 //
66 // Form1
67 //
68 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
69 this.ClientSize = new System.Drawing.Size(292, 273);
70 this.Controls.Add(this.button1);
71 this.Name = "Form1";
72 this.Text = "Form1";
73 this.ResumeLayout(false);
74
75 }
76 #endregion
77
78 /// <summary>
79 /// 应用程序的主入口点。
80 /// </summary>
81 [STAThread]
82 static void Main()
83 {
84 Application.Run(new Form1());
85 }
86
87 private void button1_Click(object sender, System.EventArgs e)
88 {
89 if (CDOpen == false)
90 {
91 CDdoor("set CDAudio door open", "0", 0, 0);
92 CDOpen = true;
93 this.button1.Text = "点击关闭光驱";
94 }
95 else
96 {
97 CDdoor("set CDAudio door closed", "0", 0, 0);
98 CDOpen = false;
99 this.button1.Text = "点击打开光驱";
100 }
101 }
102 }
103}
104
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7
8namespace CDDoor
9{
10 /// <summary>
11 /// Form1 的摘要说明。
12 /// </summary>
13 public class Form1 : System.Windows.Forms.Form
14 {
15 private System.Windows.Forms.Button button1;
16 /// <summary>
17 /// 必需的设计器变量。
18 /// </summary>
19 private System.ComponentModel.Container components = null;
20
21 [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint="mciSendStringA")]
22 private static extern long CDdoor(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);
23
24 private bool CDOpen = true;
25
26 public Form1()
27 {
28 InitializeComponent();
29
30 this.button1.Text = "点击关闭光驱";
31 CDdoor("set CDAudio door open", "0", 0, 0);
32 }
33
34 /// <summary>
35 /// 清理所有正在使用的资源。
36 /// </summary>
37 protected override void Dispose( bool disposing )
38 {
39 if( disposing )
40 {
41 if (components != null)
42 {
43 components.Dispose();
44 }
45 }
46 base.Dispose( disposing );
47 }
48
49 #region Windows 窗体设计器生成的代码
50 /// <summary>
51 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
52 /// 此方法的内容。
53 /// </summary>
54 private void InitializeComponent()
55 {
56 this.button1 = new System.Windows.Forms.Button();
57 this.SuspendLayout();
58 //
59 // button1
60 //
61 this.button1.Location = new System.Drawing.Point(104, 128);
62 this.button1.Name = "button1";
63 this.button1.TabIndex = 0;
64 this.button1.Click += new System.EventHandler(this.button1_Click);
65 //
66 // Form1
67 //
68 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
69 this.ClientSize = new System.Drawing.Size(292, 273);
70 this.Controls.Add(this.button1);
71 this.Name = "Form1";
72 this.Text = "Form1";
73 this.ResumeLayout(false);
74
75 }
76 #endregion
77
78 /// <summary>
79 /// 应用程序的主入口点。
80 /// </summary>
81 [STAThread]
82 static void Main()
83 {
84 Application.Run(new Form1());
85 }
86
87 private void button1_Click(object sender, System.EventArgs e)
88 {
89 if (CDOpen == false)
90 {
91 CDdoor("set CDAudio door open", "0", 0, 0);
92 CDOpen = true;
93 this.button1.Text = "点击关闭光驱";
94 }
95 else
96 {
97 CDdoor("set CDAudio door closed", "0", 0, 0);
98 CDOpen = false;
99 this.button1.Text = "点击打开光驱";
100 }
101 }
102 }
103}
104