打开记事本。 点一下按钮, 然后到那个记事本里随便输什么。。跳出一个窗体 ,为什么不行,哪有问题?代码如下:
在 dpr文件中的代码如下
library Hook32;uses
   SysUtils,
   Forms,
   Classes,
   myDLl in 'myDLl.pas' {Form1};
{$R *.res} exports 
HookOn,HookOff;begin 
{Application.Initialize; 
Application.Run; } 
end. 
 unit myDLl;interface uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
Dialogs, StdCtrls; type 
TForm1 = class(TForm) 
Button1: TButton; 
Edit1: TEdit; 
Memo1: TMemo; 
procedure Button1Click(Sender: TObject); 
procedure FormClose(Sender: TObject; var Action: TCloseAction); 
procedure FormDestroy(Sender: TObject); 
procedure FormActivate(Sender: TObject); 
private 
{ Private declarations } 
public 
{ Public declarations } 
end; var 
Form1: TForm1; 
function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall; 
function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;export; 
function HookOff:Boolean;stdcall;export; implementation 
{type KeyboardBytes=record 
kbArray:array[0..255] of byte; 
end;} var 
hHk: HHOOK=0; 
hMOUSEHk: HHOOK=0; 
mhwnd:HWND=0; 
bShow:Boolean=False; 
myKey:Byte=VK_F7; 
kbArray:TKeyboardState; 
hThread: Cardinal; 
hmod: Pointer; //Hinstance 
hProcessId: Cardinal; // KeyHookStruct:^THardwareHookStruct; 
mMode:Integer;  {$R *.dfm} function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall; 
begin 
Result :=0; if nCode<0 then 
Result := CallNextHookEx(hHk,nCode,WParam,LParam) 
else 
begin 
GetKeyboardState(kbArray); if (bShow=False) And (kbArray[myKey]=1) then 
begin 
bShow:=True; 
Form1:=TForm1.Create(Application); 
ShowCursor(true); 
try 
// Form1.Caption :=’我的DLL中的窗体!’; 
// LockWindowUpdate(mhwnd); 
/// SetParent(Form1.Handle,mhwnd); 
// MoveWindow(Form1.Handle,1,1,2,2,True); 
// UpdateWindow(Form1.Handle); 
// UpdateWindow(mhwnd); 
SetWindowPos(Form1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE); 
// UpdateWindow(mhwnd); 
// mMode:=GetMapMode(GetDC(mhwnd)); 
// SetMapMode(GetDC(Form1.Handle),mMode); 
// UpdateWindow(Form1.Handle); 
// SetWindowLong(Form1.Handle,GWL_STYLE,GetWindowLong(mhwnd, GWL_STYLE)); 
Result :=1; 
SuspendThread(hThread); 
Form1.ShowModal; 
ShowCursor(true); 
ResumeThread(hThread); 
kbArray[myKey] := 0; 
SetKeyboardState(kbArray); finally 
Form1.Free; 
end; 
end 
else 
begin 
Result := CallNextHookEx(hHk,nCode,WParam,LParam); 
end; 
end; 
end; function HookOn(lpHwnd:HWND;lpType:Longint): Longint;stdcall; export; 
begin 
mhwnd:=lpHwnd; 
if hHk<>0 then UnHookWindowsHookEx(hHk); 
hThread :=GetWindowThreadProcessId(mhwnd,hmod);// hHk :=SetWindowsHookEx(lpType,@HookProc,hInstance,hThread); // WH_KEYBOARD
Result :=hHk 
end; function HookOff:Boolean;stdcall; export; 
begin 
if hHk<>0 then 
begin 
UnHookWindowsHookEx(hHk); 
hHk :=0; 
Result :=true; 
end 
else 
Result :=false; 
end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
bShow:=False; 
end; procedure TForm1.FormDestroy(Sender: TObject); 
begin 
bShow:=False; 
end; procedure TForm1.Button1Click(Sender: TObject); 
begin 
Form1.close; 
end; procedure TForm1.FormActivate(Sender: TObject); 
begin 
ShowCursor(true); 
end; end. //工程unit Unit1;interface uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
Dialogs, StdCtrls, ExtCtrls; type 
TForm1 = class(TForm)
     Button1: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
     procedure Button1Click(Sender: TObject);private 
{ Private declarations } 
public 
{ Public declarations } 
end; var 
Form1: TForm1; 
function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;external 'Hook32.dll' name 'HookOn';
function HookOff:Boolean;stdcall;external 'Hook32.dll' name 'HookOff'; 
implementation {$R *.dfm} procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
hookoff; 
end; procedure TForm1.Button1Click(Sender: TObject);
var h1:HWND;
beginh1:=FindWindow(NIL,'a.txt - 记事本');//这是窗口的句柄。
HookOn(h1,WH_KEYBOARD);end;end.

解决方案 »

  1.   

    library Hook32; uses 
      SysUtils, 
      Forms, 
      Classes, 
      myDLl in 'myDLl.pas' {Form1}; 
    {$R *.res} exports 
    HookOn,HookOff; begin 
    {Application.Initialize; 
    Application.Run; } 
    end. 
    unit myDLl; interface uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls; type 
    TForm1 = class(TForm) 
    Button1: TButton; 
    Edit1: TEdit; 
    Memo1: TMemo; 
    procedure Button1Click(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure FormDestroy(Sender: TObject); 
    procedure FormActivate(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; var 
    Form1: TForm1; 
    function HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall; 
    function HookOn(lpHwnd:HWND;lpType:Longint):Longint;stdcall;export; 
    function HookOff:Boolean;stdcall;export; implementation 
    {type KeyboardBytes=record 
    kbArray:array[0..255] of byte; 
    end;} var 
    hHk: HHOOK=0; 
    hMOUSEHk: HHOOK=0; 
    mhwnd:HWND=0; 
    bShow:Boolean=False; 
    myKey:Byte=VK_F7; 
    kbArray:TKeyboardState; 
    hThread: Cardinal; 
    hmod: Pointer; //Hinstance 
    hProcessId: Cardinal; // KeyHookStruct:^THardwareHookStruct; 
    mMode:Integer;  {$R *.dfm} procedure GetDLLForm;
    begin
      with TForm1.create(nil) do 
      begin
        try
          showModal;
        finally
          Free;
        end;
      end;
    end;
    export GetDLLForm;  //OKfunction HookProc(nCode:Integer;WParam: WPARAM;LParam:LPARAM):LRESULT;stdcall; 
    begin 
    Result :=0; if nCode <0 then 
    Result := CallNextHookEx(hHk,nCode,WParam,LParam) 
    else 
    begin 
    GetKeyboardState(kbArray); if (bShow=False) And (kbArray[myKey]=1) then 
    begin 
    bShow:=True; 
    Form1:=TForm1.Create(Application); 
    ShowCursor(true); 
    try 
    // Form1.Caption :=’我的DLL中的窗体!’; 
    // LockWindowUpdate(mhwnd); 
    /// SetParent(Form1.Handle,mhwnd); 
    // MoveWindow(Form1.Handle,1,1,2,2,True); 
    // UpdateWindow(Form1.Handle); 
    // UpdateWindow(mhwnd); 
    SetWindowPos(Form1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE); 
    // UpdateWindow(mhwnd); 
    // mMode:=GetMapMode(GetDC(mhwnd)); 
    // SetMapMode(GetDC(Form1.Handle),mMode); 
    // UpdateWindow(Form1.Handle); 
    // SetWindowLong(Form1.Handle,GWL_STYLE,GetWindowLong(mhwnd, GWL_STYLE)); 
    Result :=1; 
    SuspendThread(hThread); 
    Form1.ShowModal; 
    ShowCursor(true); 
    ResumeThread(hThread); 
    kbArray[myKey] := 0; 
    SetKeyboardState(kbArray); finally 
    Form1.Free; 
    end; 
    end 
    else 
    begin 
    Result := CallNextHookEx(hHk,nCode,WParam,LParam); 
    end; 
    end; 
    end; function HookOn(lpHwnd:HWND;lpType:Longint): Longint;stdcall; export; 
    begin 
    mhwnd:=lpHwnd; 
    if hHk <>0 then UnHookWindowsHookEx(hHk); 
    hThread :=GetWindowThreadProcessId(mhwnd,hmod); // hHk :=SetWindowsHookEx(lpType,@HookProc,hInstance,hThread); // WH_KEYBOARD 
    Result :=hHk 
    end; function HookOff:Boolean;stdcall; export; 
    begin 
    if hHk <>0 then 
    begin 
    UnHookWindowsHookEx(hHk); 
    hHk :=0; 
    Result :=true; 
    end 
    else 
    Result :=false; 
    end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); 
    begin 
    bShow:=False; 
    end; procedure TForm1.FormDestroy(Sender: TObject); 
    begin 
    bShow:=False; 
    end; procedure TForm1.Button1Click(Sender: TObject); 
    begin 
    Form1.close; 
    end; procedure TForm1.FormActivate(Sender: TObject); 
    begin 
    ShowCursor(true); 
    end; end.