var
SendText: string;
SendDataStruct: CopyDataStruct;
begin
SendText := 'Send';
SendDataStruct.cbData := length(Sendtext);
SendDataStruct.lpData := @SendText[1];
SendMessage(FindWindow(PCHAR('TForm1'), PCHAR('form1')), WM_CopyData, Application.Handle, lparam(@SendDataStruct));
end;
接收者:procedure DataIn(var DataMessage: TWMCopyData); message wm_copydata;
procedure DataIn(var DataMessage: TWMCopyData);
var
ReceieveData : CopyDataStruct;
temp: pchar;
ReceieveStr : string;
begin
ReceieveData := DataMessage.CopyDataStruct^;
temp := ReceieveData.lpdata;
ReceieveStr := string(temp);
end;
回复人:xiaohuilau(2000-8-13 11:17:00) 得0分library hk;
uses
SysUtils,
Classes,
hkproc in 'hkproc.pas';
exports EnableHotKeyHook, DisableHotKeyHook;
begin
hNextHookProc := 0;
procSaveExit := ExitProc;
end. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
unit hkproc;
interface
uses Windows, Messages, Dialogs, SysUtils, ShellAPI;
var
intMainHWND: Longint; // 主程序句柄,用于发送消息时用
Shell_TrayWnd: Longint;
CtrlDown: Boolean;
hNextHookProc: HHook;
procSaveExit: Pointer;
function KHH(iCode: integer; wParam: wParam; lParam: lParam): LRESULT; stdcall; export;
function EnableHotKeyHook(MainProgramHWND: Longint): Bool; export;
function DisableHotKeyHook: Bool; Export;
implementation
function KHH(iCode: integer; wParam: wParam; lParam: lParam): LRESULT; stdcall; export;
const
_Keypressmask = $80000000;
var
lpPoint: TPoint;
hwnd: Longint;
strClassName, strTrayClassName: String;
SendText: string; // 向主程序发送的消息串
SendDataStruct: CopyDataStruct; // 消息类型
begin
result := 0;
if iCode < 0 then
begin
result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
exit;
end;
// 判断CLASSNAME 与 WND 所用的 LPPOINT 是不,同的。
GetCursorPos(lpPoint);
hwnd := WindowFromPoint(lpPoint); // 读取鼠标指向的控制句柄
SetLength(strClassName, 255 + 2);
GetClassName(hwnd, PChar(strClassName), 255); // 读取句柄的名字
strClassName := UpperCase(strClassName);
lpPoint.x := LOWORD(lParam); // 读取鼠标的X标
lpPoint.y := HIWORD(lParam); // 读取鼠标的Y标
hwnd := WindowFromPoint(lpPoint); // 读取鼠标指向的控制句柄
strTrayClassName := 'SHELL_TRAYWND'; // 任务栏
if Copy(strClassName, 1, length(strTrayClassName)) = strTrayClassName then
if hwnd = Shell_TrayWnd then
begin
if (wParam = WM_NCRBUTTONUP) or (wParam = WM_NCLBUTTONDOWN) then
begin
result := 1;
exit;
end;
end; { if WND }
strTrayClassName := 'SYSTABCONTROL'; // 任务栏
if Copy(strClassName, 1, length(strTrayClassName)) = strTrayClassName then
exit;
strTrayClassName := 'TRAYNOTIFYWND'; // 页
if Copy(strClassName, 1, length(strTrayClassName)) = strTrayClassName then
begin
if (wParam = WM_RBUTTONUP) then
begin
hwnd := GetParent(GetParent(GetParent(hwnd)));
if hwnd = Shell_TrayWnd then
result := 1;
end;
end;
strTrayClassName := 'BUTTON'; // 开始按钮
if Copy(strClassName, 1, length(strTrayClassName)) = strTrayClassName then
begin
hwnd := GetParent(hwnd);
if hwnd = Shell_TrayWnd then
begin
if (wParam = WM_RBUTTONUP) or (wParam = WM_LBUTTONDOWN) then
result := 1;
if wParam = WM_LBUTTONUP then
begin
SendText := 'Send';
SendDataStruct.lpData := @SendText[1];
SendDataStruct.cbData := length(SendText);
// ***************** * * * * 当按下开始按钮时发送信息。
SendMessage(intMainHWND, WM_CopyData, 0, lParam(@SendDataStruct));
// ***************** * * * *
end;
exit;
end; { if hwnd }
end;
strTrayClassName := 'REBARWINDOW'; // 页
if Copy(strClassName, 1, length(strTrayClassName)) = strTrayClassName then
begin
if (wParam = WM_RBUTTONUP) or (wParam = WM_LBUTTONDOWN) then
begin
hwnd := GetParent(hwnd);
if hwnd = Shell_TrayWnd then
result := 1;
end;
end;
strTrayClassName := 'TOOLBARWINDOW'; // 工具栏
if Copy(strClassName, 1, length(strTrayClassName)) = strTrayClassName then
begin
if (wParam = WM_LBUTTONDOWN) or (wParam = WM_RBUTTONUP) or (wParam = WM_RBUTTONDOWN) then
begin
hwnd := GetParent(GetParent(hwnd));
if hwnd = Shell_TrayWnd then
result := 1;
end;
end;
end;
function EnableHotKeyHook(MainProgramHWND: Longint): Bool; export;
begin
intMainHWND := MainProgramHWND;
Shell_TrayWnd := FindWindow('Shell_TrayWnd', nil);
result := False;
if hNextHookProc <> 0 then
exit;
hNextHookProc := SetWindowsHookEx(WH_MOUSE, KHH, Hinstance, 0);
result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: Bool; export;
begin
if hNextHookProc <> 0 then
begin
UnhookWindowsHookEx(hNextHookProc);
hNextHookProc := 0;
MessageBeep(0);
end;
result := hNextHookProc = 0;
end;
procedure HotKeyHookExit;
begin
if hNextHookProc <> 0 then
DisableHotKeyHook;
end;
end.