程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 使用鉤子函數[6] - 數據傳遞

使用鉤子函數[6] - 數據傳遞

編輯:Delphi

上一個例子是從 DLL 中接受數據, 那怎麼給 DLL 傳遞數據呢? 還有, 在接受數據時, 讓 Timer 一直在那掃描也不是個好辦法呀. 本例解決了這些問題(但不知解決了博友 "鵬" 的問題沒有?).

為了方便測試, 提供一個源碼下載吧: http://files.cnblogs.com/del/MouseHook_2.rar

本例效果圖(和上一例是一樣的):

DLL 文件:

library MyHook;

uses
SysUtils,
Windows,
Messages,
Classes;

{$R *.res}

const WM_MyMessage = WM_USER + 1; {自定義消息}

var
hook: HHOOK;
info: string;
h: HWND; {用作外部窗口的句柄}

{獲取外部窗口的句柄}
function SetHWnd(hwnd: HWND): Boolean; stdcall;
begin
h := hwnd;
Result := True;
end;

function MouseHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
case wParam of
WM_MOUSEMOVE  : info := '鼠標位置';
WM_LBUTTONDOWN : info := '按下';
WM_LBUTTONUp  : info := '放開';
end;
info := Format('%s: %d,%d', [info, PMouseHookStruct(lParam)^.pt.X, PMouseHookStruct(lParam)^.pt.Y]);

{通過消息把數據傳遞給指定窗口}
PostMessage(h, WM_MyMessage, 0, Integer(PChar(info)));

Result := CallNextHookEx(hook, nCode, wParam, lParam);
end;

function SetHook: Boolean; stdcall;
const
WH_MOUSE_LL =14;
begin
hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHook, HInstance, 0);
Result := hook <> 0;
end;

function DelHook: Boolean; stdcall;
begin
Result := UnhookWindowsHookEx(hook);
end;

exports SetHook, DelHook, MouseHook, SetHWnd;
begin
end.

測試代碼:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

const WM_MyMessage = WM_USER + 1;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MyMessage(var msg: TMessage); message WM_MyMessage; {定義一個消息方法接受消息}
end;

function SetHook: Boolean; stdcall;
function DelHook: Boolean; stdcall;
function SetHWnd(hwnd: HWND): Boolean; stdcall;

var
Form1: TForm1;

implementation

{$R *.dfm}

function SetHook; external 'MyHook.dll';
function DelHook; external 'MyHook.dll';
function SetHWnd; external 'MyHook.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
SetHook;
SetHWnd(Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
DelHook;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := '安裝鉤子';
Button2.Caption := '載卸鉤子';
FormStyle := fsStayOnTop; {為了測試, 讓窗口一直在前面}
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
DelHook;
end;

{把接受到的內容顯示在窗體}
procedure TForm1.MyMessage(var msg: TMessage);
begin
Text := PChar(msg.LParam);
end;

end.

測試窗體:

object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 78
ClientWidth = 271
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 48
Top = 32
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object Button2: TButton
Left = 144
Top = 32
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 1
OnClick = Button2Click
end
end

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved