1,178
社区成员
发帖
与我相关
我的任务
分享
function FindControl(Handle: HWnd): TWinControl;
var
OwningProcess: DWORD;
begin
Result := nil;
if (Handle <> 0) and (GetWindowThreadProcessID(Handle, OwningProcess) <> 0) and
(OwningProcess = GetCurrentProcessId) then
begin
if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
Result := Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)))
else
Result := Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0));
end;
end;TWinControl FindControl1(HWND Handle)
{
DWORD OwningProcess;
TWinControl Result;
Result = NULL;
if ((Handle != 0) && (GetWindowThreadProcessId(Handle, &OwningProcess)!= 0)&&
(&OwningProcess = GetCurrentProcessId))
{
if (GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom)
Result = Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)));
else
Result = Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0));
return Result;
}procedure InstallHook(MainWnd, DestWnd: HWND); stdcall;
begin
if P^.hkMsg = 0 then
P^.hkMsg := SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance, 0);
P^.HostWnd := MainWnd;
P^.HostPID := GetCurrentProcessId;
P^.DestWnd := DestWnd;
end;
void __stdcall InstallHook(HWND MainWnd, HWND DestWnd)
{
if (P^.hkMsg = 0)
{
P^.hkMsg = SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, HInstance, 0);
P^.HostWnd = MainWnd;
P^.HostPID = GetCurrentProcessId;
P^.DestWnd = DestWnd;
}
}
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
{
ControlAtomString = String().sprintf("ControlOfs%.8X%.8X", GetModuleHandle(NULL), GetCurrentThreadID());
ControlAtom = GlobalAddAtom(ControlAtomString.c_str());
RM_GetObjectInstance = RegisterWindowMessage(ControlAtomString.c_str());
break;
}
case DLL_PROCESS_DETACH:
{
GlobalDeleteAtom(ControlAtom);
ControlAtomString = "";
break;
}
default:
break;
}
return 1;
}initialization
ControlAtomString := Format('ControlOfs%.8X%.8X', [GetModuleHandle(nil), GetCurrentThreadID]);
ControlAtom := GlobalAddAtom(PChar(ControlAtomString));
RM_GetObjectInstance := RegisterWindowMessage(PChar(ControlAtomString));
finalization
GlobalDeleteAtom(ControlAtom);
ControlAtomString := '';
end.String ControlAtomString;
ATOM ControlAtom ;
DWORD RM_GETOBJECTINSTANCE;
extern "C" __declspec(dllexport) TWinControl* __stdcall MyFindControl(HANDLE hWnd);
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
TWinControl* __stdcall MyFindControl(HANDLE hWnd)
{
DWORD dwOwningProcess;
if(hWnd != NULL && GetWindowThreadProcessId(hWnd, &dwOwningProcess) != 0
&& dwOwningProcess == GetCurrentProcessId())
{
if(GlobalFindAtom(ControlAtomString.c_str()) == ControlAtom)
return (TWinControl *)GetProp(hWnd, MAKEINTATOM(ControlAtom));
else
return (TWinControl *)SendMessage(hWnd, RM_GETOBJECTINSTANCE, 0, 0);
}
return NULL;
}