delphi获取其他外部程序中TDBGridEh控件中的数据

使用钩子原理+dll注入。首先使用FindwindowEx能获取到表格的句柄,再转化为表格,将表格的内容赋值给你的新表格。
function MsgWndProc(hwnd: HWND; Msg: UINT; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
// SG: TStringGrid;
SG:TDrawgrid;
X, Y: Integer;
begin
case Msg of
CM_QUERYROW:
begin
Result := -1;
if P^.DestWnd <> 0 then
begin
SG := Pointer(FindControl(P^.DestWnd));
if SG <> nil then Result := SG.RowCount;
end;
Exit;
end;
CM_QUERYCOL:
begin
Result := -1;
if P^.DestWnd <> 0 then
begin
SG := Pointer(FindControl(P^.DestWnd));
if SG <> nil then Result := SG.ColCount;
end;
Exit;
end;
CM_HOOKCELL:
begin
Result := -1;
P^.Text[0] := #0;
if P^.DestWnd <> 0 then
begin
SG := Pointer(FindControl(P^.DestWnd));
if SG <> nil then
begin
X := WParam;
Y := LParam;
if (X >= 0) and (X < SG.ColCount) and (Y >= 0) and (Y < SG.RowCount) then
begin
Result := Length(SG.Cells[X, Y]);//就是这里 获取不到它的单元格值?
if Result > 0 then
begin
StrPLCopy(P^.Text, SG.Cells[X, Y], 1024);
end;
end;
end;
end;
Exit;
end;
end;
Result := DefWindowProc(hwnd, Msg, WParam, LParam);
end;
这个我已经测试通过。