国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現自動鎖屏與關屏的方法

2023-05-18 12:33:07
字體:
來源:轉載
供稿:網友

有時我們編寫的程序需要進行鎖屏和關屏操作,在網上搜索了一下,終于找到了可行的解決方案。全文如下:

最近找到了windows鎖屏API:LockWorkStation,并把之前的關屏API整合了一下,編寫了一個可以選擇自動鎖屏+關屏的程序。程序源代碼片段如下:

public Form1( bool aLock ) {

  if (aLock) {

  //鎖屏+關屏

  LockWorkStation();

  SendMessage( this.Handle, (uint)0x0112, (IntPtr)0xF170, (IntPtr)2 );

  }

  else {

  //禁止鼠標鍵盤動作+關屏

  BlockInput( true );

  System.Threading.Thread.Sleep( 10 );

  SendMessage( this.Handle, (uint)0x0112, (IntPtr)0xF170, (IntPtr)2 );

  BlockInput( false );

  }

  this.Close();

  //Application.Exit();

  Environment.Exit( 0 );

  }

  //關屏

  [DllImport( "user32.dll", CharSet = CharSet.Auto )]

  static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam );

  //禁止鼠標鍵盤動作

  [return: MarshalAs( UnmanagedType.Bool )]

  [DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )]

  public static extern bool BlockInput( [In, MarshalAs( UnmanagedType.Bool )] bool fBlockIt );

  //鎖屏

  [DllImport( "user32.dll" )]

  public static extern bool LockWorkStation();

需要指出的是,在退出程序時要使用Environment.Exit( 0 );而非Application.Exit();否則會出錯而提示類似:“***遇到錯誤,需要關閉”。

最后修改一下Main函數代碼:

static void Main(string[] args) {

  //Application.EnableVisualStyles();

  //Application.SetCompatibleTextRenderingDefault( false );

  if (args == null || args.Length == 0) {

  //禁止鼠標鍵盤動作+關屏

  Application.Run( new Form1( false ) );

  }

  else {

  //鎖屏+關屏

  Application.Run( new Form1( true ) );

  }

  }

       ..

這樣,我們就可以實現鎖屏和關屏了。為了方便,你可以新建個快捷方式,加個參數,即可鎖屏。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 游戏| 武义县| 景洪市| 饶阳县| 南部县| 通江县| 怀来县| 许昌县| 罗江县| 巴林右旗| 霍城县| 温泉县| 库车县| 宕昌县| 高要市| 拉萨市| 类乌齐县| 芷江| 北辰区| 交城县| 武强县| 周口市| 潼南县| 波密县| 玉环县| 民县| 绥阳县| 鄂尔多斯市| 金川县| 正定县| 桓仁| 忻州市| 连山| 明星| 满洲里市| 寿宁县| 卫辉市| 四会市| 东城区| 咸宁市| 鸡泽县|