51编程网站点导航
您现在的位置: 51编程网 >> 文档中心 >> winform截屏、关机、重启、注销、关闭显示器、打开关闭光驱
官方QQ群:12672376
winform截屏、关机、重启、注销、关闭显示器、打开关闭光驱
来源:51编程网发布时间:2009-09-20Hits:1454

#region C#截屏
        /// <summary>
        /// 名称:GetImage
        /// 功能:截图函数
        /// 编写时间:2008-12-11
        /// </summary>
        private void GetImage()//打印屏幕
        {
            try
            {
                string tempImagePath = Application.StartupPath;
                string temp = tempImagePath + "\\CurrentScreenImage";
                Directory.CreateDirectory(@temp);
                Image i = new Bitmap(this.Width, this.Height);
                Graphics g = Graphics.FromImage(i);
                g.CopyFromScreen(new Point(this.Location.X, this.Location.Y), new Point(0, 0), new Size(this.Width, this.Height));
                i.Save(@temp + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg");
                g.Dispose();
                MessageBox.Show("截图成功!");
            }
            catch
            {
                MessageBox.Show("截图失败!");
            }
        }
        #endregion

 

        ///命名空间
        ///using System.Runtime.InteropServices;
        ///using System.Diagnostics;
        #region 注销、关闭、重启计算机
        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
        //ExitWindowsEx 函数
        private static extern int ExitWindowsEx(int uFlags, int dwReserved);
        public void zhuxiao() //注销
        {
            ExitWindowsEx(0, 0);
        }

        public void guanji()//关机
        {
            try
            {
                Process.Start("Shutdown.exe", " -s -t 0");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       
        public void chongqi()//重启
        {
            try
            {
                Process.Start("shutdown.exe", " -r -t 0");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

 

        #region 关闭显示器
        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);

        private void button1_Click(object sender, EventArgs e)
        {
            CloseLCD(sender, e);//关闭显示器
        }

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

 

        #region 打开关闭光驱
        /// <summary>
        /// 类构造方法中加//mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
        /// </summary>
        private bool CDOpen = true;
        [System.Runtime.InteropServices.DllImport("winmm.dll", EntryPoint = "mciSendStringA")]
        protected static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback);

        private void button1_Click(object sender, EventArgs e)
        {
            mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
            if (CDOpen == false)
            {
                mciSendString("set cdaudio door open", null, 0, IntPtr.Zero);
                CDOpen = true;
                this.button1.Text = "关闭光驱";
            }
            else
            {
                mciSendString("set cdaudio door closed", null, 0, IntPtr.Zero);
                CDOpen = false;
                this.button1.Text = "打开光驱";
            }
        }
        #endregion

>> 以下文档您可能感兴趣
>> 关注国内外最新新闻
| 热门文档
| 热门源码 ->
| 热门教程 ->
| 热门工具
Copyright© 2008-2010 51bcw.com All Rights Reserved 版权所有©51编程网
FocusNT!技术支持 Email:   浙ICP备09040643号
LOGO