欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 屏蔽快捷鍵的實(shí)現(xiàn)示例

 更新時(shí)間:2021年03月11日 09:35:53   作者:leestar54  
這篇文章主要介紹了c# 屏蔽快捷鍵的實(shí)現(xiàn)示例,幫助大家更好的理解和利用c#進(jìn)行桌面開發(fā),感興趣的朋友可以了解下

前言

有時(shí)候開發(fā)會遇到這樣一個(gè)需求,軟件需要屏蔽用戶的組合快捷鍵或某些按鍵,避免強(qiáng)制退出軟件,防止勿操作等。

原理

1、要實(shí)現(xiàn)組合鍵,按鍵攔截,需要用到user32.dll中的SetWindowsHookEx。

2、要攔截ctrl+alt+del,需要使用ntdll.dll的ZwSuspendProcess函數(shù)掛起winlogon程序,退出之后使用ZwResumeProcess恢復(fù)winlogon程序。

3、軟件需要開啟topMost,以及全屏,否則離開軟件則攔截?zé)o效。

4、如果要實(shí)現(xiàn)熱鍵監(jiān)聽(非焦點(diǎn)攔截),則需要用到user32.dll的RegisterHotKey以及UnregisterHotKey。

實(shí)現(xiàn)

1、Program類

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace LockForm
{
 static class Program
 {
  /// <summary>
  /// 應(yīng)用程序的主入口點(diǎn)。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   SuspendWinLogon();
   Application.Run(new Form1());
   ResumeWinLogon();
  }

  [DllImport("ntdll.dll")]
  public static extern int ZwSuspendProcess(IntPtr ProcessId);
  [DllImport("ntdll.dll")]
  public static extern int ZwResumeProcess(IntPtr ProcessId);

  private static void SuspendWinLogon()
  {
   Process[] pc = Process.GetProcessesByName("winlogon");
   if (pc.Length > 0)
   {
    ZwSuspendProcess(pc[0].Handle);
   }
  }

  private static void ResumeWinLogon()
  {
   Process[] pc = Process.GetProcessesByName("winlogon");
   if (pc.Length > 0)
   {
    ZwResumeProcess(pc[0].Handle);
   }
  }
 }
}

2、Form1類

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace LockForm
{
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }

  private void button1_Click(object sender, EventArgs e)
  {
   if (textBox1.Text == "123")
   {
    Application.ExitThread();
   }
   else
   {
    webBrowser1.Navigate(textBox1.Text);
   }
  }

  private void Form1_Load(object sender, EventArgs e)
  {
   //webBrowser1.Navigate("https://baidu.com");
   HookStart();
   //this.TopMost = false;
   //SuspendWinLogon();
  }

  private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
  {
   e.Cancel = true;
   webBrowser1.Navigate(webBrowser1.Document.ActiveElement.GetAttribute("href"));
  }

  private void button3_Click(object sender, EventArgs e)
  {
   webBrowser1.GoBack();
  }

  private void button2_Click(object sender, EventArgs e)
  {
   webBrowser1.GoForward();
  }

  private void button4_Click(object sender, EventArgs e)
  {
   webBrowser1.GoHome();
  }

  private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  {
   textBox1.Text = webBrowser1.Url.ToString();
  }

  private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
  {

  }

  #region 鍵盤鉤子

  public delegate int HookProc(int nCode, int wParam, IntPtr lParam);//定義全局鉤子過程委托,以防被回收(鉤子函數(shù)原型)
  HookProc KeyBoardProcedure;

  //定義鍵盤鉤子的相關(guān)內(nèi)容,用于截獲鍵盤消息
  static int hHook = 0;//鉤子函數(shù)的句柄
  public const int WH_KEYBOARD = 13;
  //鉤子結(jié)構(gòu)函數(shù)
  public struct KeyBoardHookStruct
  {
   public int vkCode;
   public int scanCode;
   public int flags;
   public int time;
   public int dwExtraInfo;

  }
  //安裝鍵盤鉤子
  public void HookStart()
  {

   if (hHook == 0)
   {
    //實(shí)例化一個(gè)HookProc對象
    KeyBoardProcedure = new HookProc(Form1.KeyBoardHookProc);

    //創(chuàng)建線程鉤子
    hHook = Win32API.SetWindowsHookEx(WH_KEYBOARD, KeyBoardProcedure, Win32API.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);

    //如果設(shè)置線程鉤子失敗
    if (hHook == 0)
    {
     HookClear();
    }
   }
  }

  //取消鉤子
  public void HookClear()
  {
   bool rsetKeyboard = true;
   if (hHook != 0)
   {
    rsetKeyboard = Win32API.UnhookWindowsHookEx(hHook);
    hHook = 0;
   }
   if (!rsetKeyboard)
   {
    throw new Exception("取消鉤子失敗!");
   }
  }
  //對截獲的鍵盤操作的處理
  public static int KeyBoardHookProc(int nCode, int wParam, IntPtr lParam)
  {
   if (nCode >= 0)
   {
    KeyBoardHookStruct kbh = (KeyBoardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyBoardHookStruct));

    if (kbh.vkCode == 91)//截獲左邊WIN鍵
    {
     return 1;
    }
    if (kbh.vkCode == 92)//截獲右邊WIN鍵
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Control)//截獲Ctrl+ESC鍵
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Escape && (int)Control.ModifierKeys == (int)Keys.Alt)
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.F4 && (int)Control.ModifierKeys == (int)Keys.Alt)//截獲ALT+F4
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Tab && (int)Control.ModifierKeys == (int)Keys.Alt)//截獲ALT+TAB
    {
     return 1;
    }
    if (kbh.vkCode == (int)Keys.Delete&&(int)Control.ModifierKeys == (int)Keys.Control + (int)Keys.Alt)
    {
     return 1;
    }
    if ( kbh.vkCode == (int) Keys.Escape && (int) Control.ModifierKeys == (int) Keys.Control + (int) Keys.Alt ) /* 截獲Ctrl+Shift+Esc */
    {
     return 1;
    }

   }

   return Win32API.CallNextHookEx(hHook, nCode, wParam, lParam);
  }
  #endregion
 }
}

3、聲明windows api

//設(shè)置鉤子
  [DllImport("user32.dll")]
  public static extern int SetWindowsHookEx(int idHook, LockForm.Form1.HookProc lpfn, IntPtr hInstance, int threadID);

  //卸載鉤子
  [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
  public static extern bool UnhookWindowsHookEx(int idHook);

  //調(diào)用下一個(gè)鉤子
  [DllImport("user32.dll")]
  public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);

PS:

windows api查詢

http://www.pinvoke.net/index.aspx

demo下載

鏈接:http://pan.baidu.com/s/1jGpOvsE 密碼:dbj2

以上就是c# 屏蔽快捷鍵的實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于c# 屏蔽快捷鍵的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#解析char型指針?biāo)赶虻膬?nèi)容(實(shí)例解析)

    C#解析char型指針?biāo)赶虻膬?nèi)容(實(shí)例解析)

    在c++代碼中定義了一個(gè)功能函數(shù),這個(gè)功能函數(shù)會將計(jì)算的結(jié)果寫入一個(gè)字符串型的數(shù)組中output,然后c#會調(diào)用c++導(dǎo)出的dll中的接口函數(shù),然后獲取這個(gè)output并解析成string類型,本文通過實(shí)例解析C#?char型指針?biāo)赶虻膬?nèi)容,感興趣的朋友一起看看吧
    2024-03-03
  • C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線壓縮和解壓縮

    C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線壓縮和解壓縮

    本文主要主要介紹了利用ICSharpCode.SharpZipLib第三方的DLL庫實(shí)現(xiàn)在線壓縮和解壓縮的功能,并做了相關(guān)的代碼演示。
    2016-04-04
  • C#字符串的常用操作工具類代碼分享

    C#字符串的常用操作工具類代碼分享

    這篇文章主要介紹了C#字符串的常用操作工具類代碼分享,需要的朋友可以參考下
    2014-04-04
  • .NET的深復(fù)制方法(以C#語言為例)

    .NET的深復(fù)制方法(以C#語言為例)

    深復(fù)制需要將對象實(shí)例中字段引用的對象也進(jìn)行復(fù)制,在平時(shí)的編程工作中經(jīng)常要用到這種復(fù)制方式,下面以c#為例來演示一下方法。
    2016-10-10
  • C# 實(shí)現(xiàn)ADSL自動斷網(wǎng)和撥號的方法(適用于撥號用戶)

    C# 實(shí)現(xiàn)ADSL自動斷網(wǎng)和撥號的方法(適用于撥號用戶)

    下面小編就為大家?guī)硪黄狢# 實(shí)現(xiàn)ADSL自動斷網(wǎng)和撥號的方法(適用于撥號用戶)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • c#之OpenFileDialog解讀(打開文件對話框)

    c#之OpenFileDialog解讀(打開文件對話框)

    這篇文章主要介紹了c#之OpenFileDialog(打開文件對話框),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 淺談C#六大設(shè)計(jì)原則

    淺談C#六大設(shè)計(jì)原則

    這篇文章主要介紹了C#六大設(shè)計(jì)原則的相關(guān)內(nèi)容,文中代碼非常細(xì)致,供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C#校驗(yàn)時(shí)間格式的場景分析

    C#校驗(yàn)時(shí)間格式的場景分析

    本文通過場景分析給大家講解C#里如何簡單的校驗(yàn)時(shí)間格式,本次的場景屬于比較常見的收單API,對第三方的訂單進(jìn)行簽名驗(yàn)證,然后持久化到數(shù)據(jù)庫,需要的朋友跟隨小編一起看看吧
    2022-08-08
  • 詳解C# ConcurrentBag的實(shí)現(xiàn)原理

    詳解C# ConcurrentBag的實(shí)現(xiàn)原理

    ConcurrentBag<T>實(shí)現(xiàn)了IProducerConsumerCollection<T>接口,該接口主要用于生產(chǎn)者消費(fèi)者模式下,可見該類基本就是為生產(chǎn)消費(fèi)者模式定制的。然后還實(shí)現(xiàn)了常規(guī)的IReadOnlyCollection<T>類,實(shí)現(xiàn)了該類就需要實(shí)現(xiàn)IEnumerable<T>、IEnumerable、 ICollection類
    2021-06-06
  • C# WinForm編程獲取文件物理路徑的方法

    C# WinForm編程獲取文件物理路徑的方法

    這篇文章主要介紹了C# inForm編程獲取文件物理路徑的方法,獲取的物理路徑是軟件即軟件安裝所在目錄,需要的朋友可以參考下
    2014-08-08

最新評論