C#实现程序控制光驱门开/关

其实十分简单调用一个api函数就可以搞定了winmm.dll中的CDdoor

  1 None.gif using  System;
  2 None.gif using  System.Drawing;
  3 None.gif using  System.Collections;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Windows.Forms;
  6 None.gif using  System.Data;
  7 None.gif
  8 None.gif namespace  CDDoor
  9 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 11InBlock.gif    /// Form1 的摘要说明。
 12ExpandedSubBlockEnd.gif    /// </summary>

 13InBlock.gif    public class Form1 : System.Windows.Forms.Form
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 15InBlock.gif        private System.Windows.Forms.Button button1;
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 17InBlock.gif        /// 必需的设计器变量。
 18ExpandedSubBlockEnd.gif        /// </summary>

 19InBlock.gif        private System.ComponentModel.Container components = null;
 20InBlock.gif
 21InBlock.gif        [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint="mciSendStringA")] 
 22InBlock.gif        private static extern long CDdoor(string lpstrCommand, string lpstrReturnString, long uReturnLength, long hwndCallback);
 23InBlock.gif
 24InBlock.gif        private bool CDOpen = true;
 25InBlock.gif
 26InBlock.gif        public Form1()
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            InitializeComponent();
 29InBlock.gif
 30InBlock.gif            this.button1.Text = "点击关闭光驱"
 31InBlock.gif            CDdoor("set CDAudio door open""0"00); 
 32ExpandedSubBlockEnd.gif        }

 33InBlock.gif
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 35InBlock.gif        /// 清理所有正在使用的资源。
 36ExpandedSubBlockEnd.gif        /// </summary>

 37InBlock.gif        protected override void Dispose( bool disposing )
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 39InBlock.gif            if( disposing )
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 41InBlock.gif                if (components != null
 42ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 43InBlock.gif                    components.Dispose();
 44ExpandedSubBlockEnd.gif                }

 45ExpandedSubBlockEnd.gif            }

 46InBlock.gif            base.Dispose( disposing );
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 51InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 52InBlock.gif        /// 此方法的内容。
 53ExpandedSubBlockEnd.gif        /// </summary>

 54InBlock.gif        private void InitializeComponent()
 55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 56InBlock.gif            this.button1 = new System.Windows.Forms.Button();
 57InBlock.gif            this.SuspendLayout();
 58InBlock.gif            // 
 59InBlock.gif            // button1
 60InBlock.gif            // 
 61InBlock.gif            this.button1.Location = new System.Drawing.Point(104128);
 62InBlock.gif            this.button1.Name = "button1";
 63InBlock.gif            this.button1.TabIndex = 0;
 64InBlock.gif            this.button1.Click += new System.EventHandler(this.button1_Click);
 65InBlock.gif            // 
 66InBlock.gif            // Form1
 67InBlock.gif            // 
 68InBlock.gif            this.AutoScaleBaseSize = new System.Drawing.Size(614);
 69InBlock.gif            this.ClientSize = new System.Drawing.Size(292273);
 70InBlock.gif            this.Controls.Add(this.button1);
 71InBlock.gif            this.Name = "Form1";
 72InBlock.gif            this.Text = "Form1";
 73InBlock.gif            this.ResumeLayout(false);
 74InBlock.gif
 75ExpandedSubBlockEnd.gif        }

 76ExpandedSubBlockEnd.gif        #endregion

 77InBlock.gif
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 79InBlock.gif        /// 应用程序的主入口点。
 80ExpandedSubBlockEnd.gif        /// </summary>

 81InBlock.gif        [STAThread]
 82InBlock.gif        static void Main() 
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 84InBlock.gif            Application.Run(new Form1());
 85ExpandedSubBlockEnd.gif        }

 86InBlock.gif
 87InBlock.gif        private void button1_Click(object sender, System.EventArgs e)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            if (CDOpen == false
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
 91InBlock.gif                CDdoor("set CDAudio door open""0"00); 
 92InBlock.gif                CDOpen = true
 93InBlock.gif                this.button1.Text = "点击关闭光驱"
 94ExpandedSubBlockEnd.gif            }
 
 95InBlock.gif            else 
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif
 97InBlock.gif                CDdoor("set CDAudio door closed""0"00); 
 98InBlock.gif                CDOpen = false
 99InBlock.gif                this.button1.Text = "点击打开光驱"
100ExpandedSubBlockEnd.gif            }

101ExpandedSubBlockEnd.gif        }

102ExpandedSubBlockEnd.gif    }

103ExpandedBlockEnd.gif}

104 None.gif

转载于:https://www.cnblogs.com/Hedonister/archive/2005/05/12/153735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值