c#控制显示器和光驱的开与关

来源:互联网 发布:hl线切割编程使用方法 编辑:程序博客网 时间:2024/04/30 01:26

转载自:http://topic.csdn.net/u/20080920/10/473931cd-e28d-4dcf-afa0-3eb36e35ceb0.html

因为要在vs2005下运行,所以我已经做了修改

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ControlHardWare
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        #region SendMessage
        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);
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            CloseLCD(sender, e);
        }

        void CloseLCD(object sender, EventArgs e)
        {
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);    // 2 为关闭显示器, -1则打开显示器
        }

// 以下是操作光驱

        [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
        private static extern long CDdoor(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
        private bool CDOpen = true;

        private void button2_Click(object sender, EventArgs e)
        {
            if (CDOpen == false)
            {
                CDdoor("set CDAudio door open", "0", 0, 0);
                CDOpen = true;
                this.button2.Text = "点击关闭光驱";
            }
            else
            {
                CDdoor("set CDAudio door closed", "0", 0, 0);
                CDOpen = false;
                this.button2.Text = "点击打开光驱";
            }

        }
    }
}

原创粉丝点击