一、开关机控制

using System.Runtime.InteropServices;

//注销、关机、重启
class shutdown
{
[StructLayout(LayoutKind.Sequential, Pack = )]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
} [DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess(); [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr phtok); [DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name, ref long pluid); [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool ExitWindowsEx(int DoFlag, int rea); internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010; private static void DoExitWin(int DoFlag)
{
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = ;
tp.Luid = ;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
ok = AdjustTokenPrivileges(htok, false, ref tp, , IntPtr.Zero, IntPtr.Zero);
ok = ExitWindowsEx(DoFlag, );
} public void Reboot()//重启
{
DoExitWin(EWX_FORCE | EWX_REBOOT);
} public void PowerOff()//关机
{
DoExitWin(EWX_FORCE | EWX_POWEROFF);
} public void LogOff()//注销
{
DoExitWin(EWX_FORCE | EWX_LOGOFF);
} }

二、音量控制

1.user32

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam); const uint WM_APPCOMMAND = 0x319;
const uint APPCOMMAND_VOLUME_UP = 0x0a;
const uint APPCOMMAND_VOLUME_DOWN = 0x09;
const uint APPCOMMAND_VOLUME_MUTE = 0x08; public void volumeUp() {//音量加 SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
}
public void volumeDown()
{//音量减 SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
}
public void volumeMuet()
{//静音或取消 SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);
}

2.CoreAudioApi

using CoreAudioApi;
using System.Runtime.InteropServices; namespace LHProgectClient
{
class volumeControl
{
//实例化CoreAudioApi里的类
MMDeviceEnumerator devEnum = new MMDeviceEnumerator();
MMDevice defaultDevice = null; //初始化
public volumeControl()
{
defaultDevice = devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
} //是否静音
public bool MUTE
{
get { return defaultDevice.AudioEndpointVolume.Mute; }
set { defaultDevice.AudioEndpointVolume.Mute = value; }
} //音量加
public void volumeUp()
{
defaultDevice.AudioEndpointVolume.VolumeStepUp();
}
//音量减
public void volumeDown()
{
defaultDevice.AudioEndpointVolume.VolumeStepDown();
}
//静音、取消
public void volumeMute()
{
defaultDevice.AudioEndpointVolume.Mute = !defaultDevice.AudioEndpointVolume.Mute;
}
}
}

最新文章

  1. EntityFramework之DetectChanges's Secrets(三)(我为EF正名)
  2. 引用类型-Array类型
  3. html的<!DOCTYPE>标签初窥
  4. Latex 笔记
  5. ASP.NET WEB API的服务托管(Self-HOST)
  6. CodeForces 682A Alyona and Numbers (水题)
  7. 禁用iOS9 App Transport Security(ATS)特性时不起作用
  8. unity3d 使用背景贴图
  9. 5亿投资A站G站:中文在线的二次元野心
  10. Cell.reuseIdentifier 指什么
  11. vue2.0transition过渡的使用介绍
  12. BZOJ_2223_[Coci 2009]PATULJCI_主席树
  13. 【转】Python3—UnicodeEncodeError 'ascii' codec can't encode characters in position 0-1
  14. 数据分组、统计 case when then else end
  15. Linux下线程同步的几种方法
  16. 利用Jquery和fullCalendar制作日程表
  17. ui-router路由控制器(一)
  18. sql 中如何将返回的记录某一条置顶
  19. 浅谈padding
  20. Spring Boot实践——Spring AOP实现之动态代理

热门文章

  1. 初识Continuation
  2. java通过句柄访问对象
  3. 如何修改Github上提交的错误用户地址和姓名
  4. windows中vim以及cmder的使用
  5. Linux-Shell脚本编程-学习-1-Linux基本命令
  6. Markdown常用的几种语法
  7. LeetCode 876——链表的中间结点
  8. edgeboxes proposal 和dpm 连接
  9. C++ 中神奇的头文件,懒人专用
  10. not1,not2,bind1st,bind2nd