乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#實現鼠標鍵盤的監(jiān)控和屏蔽 - 技術應用 - 豆豆網

       冰山上的薰衣草 2010-11-21

      C#實現鼠標鍵盤的監(jiān)控和屏蔽

      http://tech.   2008年11月04日    社區(qū)交流

      C#實現鼠標鍵盤的監(jiān)控和屏蔽。

        下載源程序和運行程序

        http://file./2008_11/1225788644_ddvip_7698.rar

        運行界面如下:

      c#實現鼠標鍵盤的監(jiān)控和屏蔽

        二說明:

        在C++中實現該功能十分簡單,也有很多經典例子可以實現,在C#中確有很多問題會出現。

        對于鉤子知識不太熟悉可以參考我轉載的另一篇文章:http://www.cnblogs.com/hocylan/articles/1033895.html[微軟HOOK技術專題]

        三大概步驟

        其實主要就是調用windows API

        第一步:安裝鉤子:SetWindowsHookEx(WH_Codes idHook, HookProc lpfn,IntPtr pInstance, int threadId);

        第二步:回調和處理CallNextHookEx(IntPtr pHookHandle, int nCode,Int32 wParam, IntPtr lParam);

        第三步:完成普通的業(yè)務處理其它流程

        將封裝的鉤子應用到系統中…….

          private void start_Click(object sender, EventArgs e)
          {hook_Main.InstallHook("1");}
          private void stop_Click(object sender, EventArgs e)
          {this.hook_Main.UnInstallHook();}
          private void stopkeyboard_Click(object sender, EventArgs e)
          { hook_Main.InstallHook("2"); }

        第四步:拆卸鉤子UnhookWindowsHookEx(IntPtr pHookHandle);

        四 重要代碼和解釋:

        *封裝的hook類:

      using System;
      using System.Windows.Forms;
      using System.Runtime.InteropServices;//必須引用
      using System.Reflection; //必須引用
      namespace KeyboardHook
      {
        class Hocy_Hook
        {
          #region私有常量
           ///<summary>
           ///按鍵狀態(tài)數組
           ///</summary>
           private readonly byte[] m_KeyState = new byte[ 256 ];
          private string flags;
          //flag=0 正常 flag=1 監(jiān)控狀態(tài) flag=2 屏蔽鍵盤//
           #endregion私有常量
           #region私有變量
           ///<summary>
           ///鼠標鉤子句柄
           ///</summary>
           private IntPtr m_pMouseHook = IntPtr.Zero;
           ///<summary>
           ///鍵盤鉤子句柄
           ///</summary>
           private IntPtr m_pKeyboardHook = IntPtr.Zero;
           ///<summary>
           ///鼠標鉤子委托實例
           ///</summary>
           ///<remarks>
           ///不要試圖省略此變量,否則將會導致
           ///激活CallbackOnCollectedDelegate 托管調試助手(MDA)。
           ///詳細請參見MSDN中關于CallbackOnCollectedDelegate 的描述
           ///</remarks>
           private HookProc m_MouseHookProcedure;
           ///<summary>
           ///鍵盤鉤子委托實例
           ///</summary>
           ///<remarks>
           ///不要試圖省略此變量,否則將會導致
           ///激活CallbackOnCollectedDelegate 托管調試助手(MDA)。
           ///詳細請參見MSDN中關于CallbackOnCollectedDelegate 的描述
           ///</remarks>
           private HookProc m_KeyboardHookProcedure;
          // 添加
          public event MouseEventHandler OnMouseActivity;
          private const byte VK_SHIFT = 0x10 ;
          private const byte VK_CAPITAL = 0x14;
          private const byte VK_NUMLOCK = 0x90;
           #endregion私有變量
           #region事件定義
           ///<summary>
           ///鼠標更新事件
           ///</summary>
           ///<remarks>當鼠標移動或者滾輪滾動時觸發(fā)</remarks>
           public event MouseUpdateEventHandler OnMouseUpdate;
           ///<summary>
           ///按鍵按下事件
           ///</summary>
           public event KeyEventHandler OnKeyDown;
           ///<summary>
           ///按鍵按下并釋放事件
           ///</summary>
           public event KeyPressEventHandler OnKeyPress;
           ///<summary>
           ///按鍵釋放事件
           ///</summary>
           public event KeyEventHandler OnKeyUp;
           #endregion事件定義
           #region私有方法
           ///<summary>
           ///鼠標鉤子處理函數
           ///</summary>
           ///<param name="nCode"></param>
           ///<param name="wParam"></param>
           ///<param name="lParam"></param>
           ///<returns>鼠標鉤子處理函數</returns>
           private int MouseHookProc( int nCode, Int32 wParam, IntPtr lParam )
           {
      if ((nCode >= 0) && (OnMouseActivity != null))
            {
              //Marshall the data from callback.
              MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));
              //detect button clicked
              MouseButtons button = MouseButtons.None;
              short mouseDelta = 0;
              switch (wParam)
              {
                case (int)WM_MOUSE.WM_LBUTTONDOWN:
                  //case WM_LBUTTONUP:
                  //case WM_LBUTTONDBLCLK:
                  button = MouseButtons.Left;
                  break;
                case (int)WM_MOUSE.WM_RBUTTONDOWN:
                  //case WM_RBUTTONUP:
                  //case WM_RBUTTONDBLCLK:
                  button = MouseButtons.Right;
                  break;
                case (int)WM_MOUSE.WM_MOUSEWHEEL:
                  //If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta.
                  //One wheel click is defined as WHEEL_DELTA, which is 120.
                  //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value
                  mouseDelta = (short)((mouseHookStruct.MouseData>> 16) & 0xffff);
                  //TODO: X BUTTONS (I havent them so was unable to test)
                  //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP,
                  //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released,
                  //and the low-order word is reserved. This value can be one or more of the following values.
                  //Otherwise, mouseData is not used.
                  break;
              }
              //double clicks
              int clickCount = 0;
              if (button != MouseButtons.None)
                if (wParam == (int)WM_MOUSE.WM_LBUTTONDBLCLK || wParam == (int)WM_MOUSE.WM_RBUTTONDBLCLK) clickCount = 2;
                else clickCount = 1;
              //generate event
              MouseEventArgs e = new MouseEventArgs(
                                button,
                               clickCount,
                                mouseHookStruct.Point.X,
                                mouseHookStruct.Point.Y,
                                mouseDelta);
              //raise it
              OnMouseActivity(this, e);
            }
            //*
             return Win32API.CallNextHookEx( this.m_pMouseHook, nCode, wParam, lParam );
           }
           ///<summary>
           ///鍵盤鉤子處理函數
           ///</summary>
           ///<param name="nCode"></param>
           ///<param name="wParam"></param>
           ///<param name="lParam"></param>
           ///<returns></returns>
           ///<remarks></remarks>
           private int KeyboardHookProc( int nCode, Int32 wParam, IntPtr lParam )
           {
            switch (flags)
            {
              case "2":
                return 1;
                break;
              case "1":
                break;
            }
            bool handled = false;
            //it was ok and someone listens to events
            if ((nCode >= 0) && (this.OnKeyDown != null || this.OnKeyUp!= null || this.OnKeyPress!= null))
            {
              //read structure KeyboardHookStruct at lParam
              KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct));
              //raise KeyDown
              if (this.OnKeyDown != null && (wParam == (int)WM_KEYBOARD.WM_KEYDOWN || wParam == (int)WM_KEYBOARD.WM_SYSKEYDOWN))
              {
                Keys keyData = (Keys)MyKeyboardHookStruct.VKCode;
                KeyEventArgs e = new KeyEventArgs(keyData);
                this.OnKeyDown(this, e);
                handled = handled || e.Handled;
              }
              // raise KeyPress
                if (this.OnKeyPress != null && wParam == (int)WM_KEYBOARD.WM_KEYDOWN)
                {
                  bool isDownShift, isDownCapslock;
                  try
                  {
                     isDownShift = ((Win32API.GetKeyStates(VK_SHIFT) & 0x80) == 0x80 ? true : false);
                    isDownCapslock = (Win32API.GetKeyStates(VK_CAPITAL) != 0 ? true : false);
                  }
                  catch
                  {
                    isDownCapslock = false;
                    isDownShift= false;
                  }
                  byte[] keyState = new byte[256];
                  Win32API.GetKeyboardState(keyState);
                  byte[] inBuffer = new byte[2];
                  if (Win32API.ToAscii(MyKeyboardHookStruct.VKCode,
                       MyKeyboardHookStruct.ScanCode,
                       keyState,
                       inBuffer,
                       MyKeyboardHookStruct.Flags) == 1)
                  {
                    char key = (char)inBuffer[0];
                    if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) key = Char.ToUpper(key);
                    KeyPressEventArgs e = new KeyPressEventArgs(key);
                    this.OnKeyPress(this, e);
                    handled = handled || e.Handled;
                  }
                }
              // raise KeyUp
              if (this.OnKeyUp != null && (wParam == (int)WM_KEYBOARD.WM_KEYUP || wParam == (int)WM_KEYBOARD.WM_SYSKEYUP))
              {
                Keys keyData = (Keys)MyKeyboardHookStruct.VKCode;
                KeyEventArgs e = new KeyEventArgs(keyData);
                this.OnKeyUp(this, e);
                handled = handled || e.Handled;
              }
            }
            //if event handled in application do not handoff to other listeners
            if (handled)
              return 1;
            else
              return Win32API.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam);
          }
           #endregion私有方法
           #region公共方法
           ///<summary>
           ///安裝鉤子
           ///</summary>
           ///<returns></returns>
           public bool InstallHook(string flagsinfo)
           {
            this.flags = flagsinfo;
      IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule);
      //pInstance = (IntPtr)4194304;
           //注意:很多時候得到的pInstanc無法安裝鉤子,請檢查值是否為4194304,在應用程序中可以直接取得pinstance的
            // IntPtr pInstanc2 = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly());
            // Assembly.GetExecutingAssembly().GetModules()[0]
             //安裝鼠標鉤子
             if ( this.m_pMouseHook == IntPtr.Zero )
             {
                this.m_MouseHookProcedure = new HookProc( this.MouseHookProc );
                this.m_pMouseHook = Win32API.SetWindowsHookEx( WH_Codes.WH_MOUSE_LL,this.m_MouseHookProcedure, pInstance, 0 );
      // WH_Codes.WH_MOUSE_LL為全局鉤子即系統鉤子,否則應該為WH_Codes.WH_KEYBOARD,即普通鉤子
                if ( this.m_pMouseHook == IntPtr.Zero )
                {
                  this.UnInstallHook();
                  return false;
                }
             }
             if ( this.m_pKeyboardHook == IntPtr.Zero ) //安裝鍵盤鉤子
             {
                this.m_KeyboardHookProcedure = new HookProc( this.KeyboardHookProc );
               // WH_Codes.WH_KEYBOARD_LL為全局鉤子即系統鉤子,否則應該為WH_Codes.WH_KEYBOARD,即普通鉤子
                this.m_pKeyboardHook = Win32API.SetWindowsHookEx( WH_Codes.WH_KEYBOARD_LL,this.m_KeyboardHookProcedure, pInstance, 0 );
                if ( this.m_pKeyboardHook == IntPtr.Zero )
                {
                  this.UnInstallHook();
                  return false;
                }
             }
             return true;
           }
           ///<summary>
           ///卸載鉤子
           ///</summary>
           ///<returns></returns>
           public bool UnInstallHook()
           {
             bool result = true;
             if ( this.m_pMouseHook != IntPtr.Zero )
             {
                result = ( Win32API.UnhookWindowsHookEx( this.m_pMouseHook ) && result );
                this.m_pMouseHook = IntPtr.Zero;
             }
             if ( this.m_pKeyboardHook != IntPtr.Zero )
             {
                result = ( Win32API.UnhookWindowsHookEx( this.m_pKeyboardHook ) && result );
                this.m_pKeyboardHook = IntPtr.Zero;
             }
             return result;
           }
           #endregion公共方法
           #region構造函數
           ///<summary>
           ///鉤子類
           ///</summary>
           ///<remarks>本類僅僅簡單實現了WH_KEYBOARD_LL 以及WH_MOUSE_LL </remarks>
           public Hocy_Hook()
           {
             Win32API.GetKeyboardState( this.m_KeyState );
           }
           #endregion構造函數
        }
      }

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多