不需要DLL的全局钩子完成鼠标截屏功能

       效果是点击截屏以后程序判断鼠标按下和鼠标抬起两个事件得到拉出的矩形,以这个矩形大小抓屏,画到Image上。使用的钩子类型是WH_MOUSE_LL所以不需DLL就可以达到全局钩子的效果。下面是代码,希望对研究这方面的兄弟有帮助。

//---------------------------------------------------------------------------
CutScreenForm.h:
#ifndef CutScreenFormH
#define CutScreenFormH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <AppEvnts.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE-managed Components
        TPanel *Panel1;
        TPanel *Panel2;
        TImage *Image1;
        TButton *Button1;
        TApplicationEvents *ApplicationEvents1;
        void __fastcall ApplicationEvents1Message(tagMSG &Msg,
          bool &Handled);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
        void __fastcall Button1Click(TObject *Sender);

private:    // User declarations

#define CM_ENTIRESCREEN 1
#define CM_ACTIVEWINDOW 2
public:        // User declarations
        __fastcall TForm1(TComponent* Owner);
        void __fastcall CaptureRect(int x,int y);
        void __fastcall DisableHook();
        LONG    width,height;
        HDC     DesktopDC;
        void __fastcall EnableHook();

};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
bool    ButtonDown = true;          //按钮状态
RECT    CapRect;                   //屏幕矩形区域
HHOOK   hook=NULL;                 //     存放新钩子句柄
void   DispXY(int x,int y);
LRESULT   CALLBACK   MouseHook(int   nCode, 
                               WPARAM   wParam,
                               LPARAM   lParam);
//---------------------------------------------------------------------------
#endif


CutScreenForm.cpp:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CutScreenForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Message(tagMSG &Msg,
      bool &Handled)
{
  if   (Msg.message   ==   WM_MOUSEMOVE)
        {
                int   x   =   LOWORD(Msg.lParam);
                int   y   =   HIWORD(Msg.lParam);
                Caption   =   Format("x   =   %d,   y   =   %d",ARRAYOFCONST((x,y))); 
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::EnableHook()
  {
        if   (hook==NULL)           //     安装新钩子
             hook=SetWindowsHookEx(WH_MOUSE_LL,
             (HOOKPROC)MouseHook,
             GetModuleHandle(NULL),
             0);
  }

LRESULT   CALLBACK   MouseHook(int   nCode,
                               WPARAM   wParam,
                               LPARAM   lParam) 
  {
          if   (nCode>=HC_ACTION)
                {
                        if (wParam == WM_LBUTTONDOWN)
                        {
                                ButtonDown = true;
                                MOUSEHOOKSTRUCT   *lPara=(MOUSEHOOKSTRUCT   *)lParam;
                                Form1->CaptureRect(lPara->pt.x,lPara->pt.y);
                        }else if(wParam == WM_LBUTTONUP)
                        {
                                ButtonDown = false;
                                MOUSEHOOKSTRUCT   *lPara=(MOUSEHOOKSTRUCT   *)lParam;
                                Form1->CaptureRect(lPara->pt.x,lPara->pt.y);
                                return(CallNextHookEx(hook,nCode,wParam,lParam));
                        }else if(wParam == WM_MOUSEMOVE){
                                return(CallNextHookEx(hook,nCode,wParam,lParam));
                        }
                     
                }
  }

void __fastcall TForm1::CaptureRect(int x,int y)
  {
       if(ButtonDown)
       {
                CapRect.top = y;
                CapRect.left = x;
       }else
       {
                if(CapRect.top > y)
                {
                        CapRect.bottom = CapRect.top;
                        CapRect.top = y;
                }else
                {
                        CapRect.bottom = y;
                }
                if(CapRect.left > x)
                {
                       CapRect.right = CapRect.left;
                       CapRect.left = x;
                }else
                {
                       CapRect.right = x;
                }
                DesktopDC   =   GetDC(GetDesktopWindow());     //   创建内存设备描述表
                width   =   CapRect.right   -   CapRect.left;
                height   =   CapRect.bottom   -   CapRect.top;
                BitBlt(Image1->Canvas->Handle,0,0,width,height,DesktopDC,
                CapRect.left,CapRect.top,SRCCOPY);
                Image1->Repaint();
                DisableHook();
       }
  }

  void __fastcall TForm1::DisableHook()
  {
        if   (hook!=NULL) 
        {
                UnhookWindowsHookEx(hook);
                hook=NULL;               //   卸掉新钩子
         }
  }
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
        DisableHook();       
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
        EnableHook();       
}
//---------------------------------------------------------------------------
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值