C#winform截屏、关机、重启、注销、关闭显示器、打开关闭光驱

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值