用我的代码试试
var
  ClassName: PChar;
  ptCursor: TPoint;
  hWndOver: HWND;
  Text: PChar;
begin
  GetCursorPos(ptCursor);
  hWndOver := WindowFromPoint(ptCursor);
  GetMem(ClassName, 100);
  GetMem(Text, 255);
  try
    GetClassName(hWndOver, ClassName, 100);
    SendMessage(hWndOver, WM_GETTEXT, 255, LongInt(Text));
    Canvas.FillRect(Rect(5, 20 , PreClassLength + 20, 40));
    Canvas.FillRect(Rect(5, 40 , PreTextLength + 20, 60));
    PreClassLength := Canvas.TextWidth(ClassName);
    PreTextLength := Canvas.TextWidth(Text);
    if PreClassLength > PreTextLength then
      Width := Canvas.TextWidth(ClassName) + 20
    else
      Width := Canvas.TextWidth(Text) + 20;
    Canvas.TextOut(5, 20, string(ClassName));
    Canvas.TextOut(5, 35, string(Text));
  finally
    FreeMem(ClassName);
    FreeMem(Text);
  end;
end;