C# 禁用ctrl+alt+del

來源:互聯網
上載者:User

using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace LockWindows
{

 /// <summary>
 /// NativeWIN32 的摘要說明。
 /// </summary>
 public class NativeWIN32
 {
  public NativeWIN32()
  {
   //
   // TODO: 在此處添加建構函式邏輯
   //
  }
  
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern IntPtr FindWindowEx(IntPtr parent /*HWND*/,
   IntPtr next /*HWND*/,
   IntPtr sClassName, 
   string sWindowTitle);
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern void SetForegroundWindow(int hwnd);

  [DllImport("user32.dll")]
  public static extern bool ShowWindow(int hWnd, nCmdShow nCmdShow);

  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern int CreateMutex(IntPtr hWnd, int msg, int wParam, int lParam);

  [DllImport("HK_KeyBoardCtr.dll", CharSet=CharSet.Auto)]
  private static extern void hideHotKey();

  [DllImport("HK_KeyBoardCtr.dll", CharSet=CharSet.Auto)]
  private static extern void showHotKey();

  public static bool hideSysKey(bool bHide)
  {
   try
   {
    if(bHide)
    {
     hideHotKey();
    }
    else
    {
     showHotKey();
    }
    return true;
   }
   catch
   {
    return false;
   }
  }
  //SendMessage(Handle2, WM_GETTEXT, 1024, Integer(@Buf));

  /// <summary>
  /// 找視窗
  /// </summary>
  /// <param name="windowTittle">視窗標題</param>
  /// <returns>返回真表示找到了,返回為假表示未找到</returns>
  public static bool FindThisWindow(string windowTittle)
  {
   IntPtr hParent = IntPtr.Zero;
   IntPtr hNext=IntPtr.Zero;
   hNext=NativeWIN32.FindWindowEx(hParent,hNext,IntPtr.Zero,windowTittle);
   if(hNext.ToInt32()>0)
   {
    NativeWIN32.SetForegroundWindow(hNext.ToInt32());    
    return true;  
   }
   else
   {
    return false;
   }
  }
  public static bool isExists(string strMutex)
  {

   bool createdNew;
   System.Threading.Mutex mutex = new System.Threading.Mutex(true,strMutex,out createdNew);
   return !createdNew;  //createNew為真,則表示mutex建立成功,沒有衝突,這樣就認為本程式沒有啟動
  }

  public static bool CloseTheWindows(string strWindowName)
  {
   try
   {
    IntPtr hParent = IntPtr.Zero;
    IntPtr hNext=IntPtr.Zero;
    hNext=NativeWIN32.FindWindowEx(hParent,hNext,IntPtr.Zero,strWindowName);
    if(hNext.ToInt32()>0)
    {
     //關閉視窗
     NativeWIN32.SendMessage(hNext,16,0,0);
     return true;  
    }
    else
    {
     return false;
    }
   }
   catch(Exception err)
   {
    return false;
   }
  }

  public enum nCmdShow:uint
  {
   SW_FORCEMINIMIZE=0x0,
   SW_HIDE=0x1,
   SW_MAXIMIZE=0x2,
   SW_MINIMIZE=0x3,
   SW_RESTORE=0x4,
   SW_SHOW=0x5,
   SW_SHOWDEFAULT=0x6,
   SW_SHOWMAXIMIZED=0x7,
   SW_SHOWMINIMIZED=0x8,
   SW_SHOWMINNOACTIVE=0x9,
   SW_SHOWNA=0xA,
   SW_SHOWNOACTIVATE=0xB,
   SW_SHOWNORMAL=0xC,
   WM_CLOSE=0x10,
   //   WM_GETTEXT=0x11,
  }
 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.