首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

跨進程獲取 ListBox 與 ComboBox 內容:Win32 API 實戰

     編輯:Delphi

在自動化測試、螢幕閱讀器、資料擷取等場景中,常需從其他進程的視窗控制項讀取列表內容。本文提供基於 SendMessage 的完整實作,支援 ListBox 與 ComboBox,經 Windows 2000 + Delphi 6 測試通過。

核心函數:GetListBoxText

function GetListBoxText(mHandle: THandle; mStrings: TStrings): Boolean;
var vItemCount, I: Integer; S: string;
begin
  Result := False;
  if not Assigned(mStrings) then Exit;
  mStrings.BeginUpdate;
  try
    mStrings.Clear;
    vItemCount := SendMessage(mHandle, LB_GETCOUNT, 0, 0);
    for I := 0 to vItemCount - 1 do begin
      SetLength(S, SendMessage(mHandle, LB_GETTEXTLEN, I, 0));
      SendMessage(mHandle, LB_GETTEXT, I, Integer(@S[1]));
      mStrings.Add(S);
    end;
    SetLength(S, 0);
  finally mStrings.EndUpdate; end;
  Result := True;
end;

核心函數:GetComboBoxText

function GetComboBoxText(mHandle: THandle; mStrings: TStrings): Boolean;
var vItemCount, I: Integer; S: string;
begin
  Result := False;
  if not Assigned(mStrings) then Exit;
  mStrings.BeginUpdate;
  try
    mStrings.Clear;
    vItemCount := SendMessage(mHandle, CB_GETCOUNT, 0, 0);
    for I := 0 to vItemCount - 1 do begin
      SetLength(S, SendMessage(mHandle, CB_GETLBTEXTLEN, I, 0));
      SendMessage(mHandle, CB_GETLBTEXT, I, Integer(@S[1]));
      mStrings.Add(S);
    end;
    SetLength(S, 0);
  finally mStrings.EndUpdate; end;
  Result := True;
end;

關鍵 API 對照

功能ListBox 訊息ComboBox 訊息
取得項目數LB_GETCOUNTCB_GETCOUNT
取得項目文字長度LB_GETTEXTLENCB_GETLBTEXTLEN
取得項目文字LB_GETTEXTCB_GETLBTEXT

實戰範例:熱鍵觸發滑鼠下控制項讀取

procedure TForm1.FormCreate(Sender: TObject);
begin
  RegisterHotKey(Handle, 1, MOD_WIN, VK_F2); // Win+F2 讀 ListBox
  RegisterHotKey(Handle, 2, MOD_WIN, VK_F3); // Win+F3 讀 ComboBox
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  UnRegisterHotKey(Handle, 1); UnRegisterHotKey(Handle, 2);
end;

procedure TForm1.WMHOTKEY(var Msg: TWMHOTKEY);
var hWnd: THandle;
begin
  hWnd := WindowFromPoint(Point(Mouse.CursorPos.X, Mouse.CursorPos.Y));
  case Msg.HotKey of
    1: GetListBoxText(hWnd, MemoText.Lines);
    2: GetComboBoxText(hWnd, MemoText.Lines);
  end;
end;

注意事項與限制

  • ✅ 適用於標準 Win32 ListBox/ComboBox 控制項
  • ❌ **不適用**於:Owner-draw 且無文字資料、虛擬模式、WPF/WinForms/Qt 等非原生控制項
  • ❌ **權限限制**:目標進程若為高權限(系統管理員),低權限進程無法發送訊息(UIPI)
  • ⚠️ **64 位元相容**:Delphi 6 為 32 位元,若目標為 64 位元進程需 64 位元編譯器
  • 🔧 **編碼問題**:舊版 Delphi 字串為 ANSI,Unicode 內容需 WideString + LB_GETTEXT 配合 WM_GETTEXT 變體

進階:支援 Unicode 的版本

function GetListBoxTextW(mHandle: THandle; mStrings: TStrings): Boolean;
var cnt, i, len: Integer; ws: WideString;
begin
  Result := False; if not Assigned(mStrings) then Exit;
  mStrings.BeginUpdate;
  try
    mStrings.Clear;
    cnt := SendMessage(mHandle, LB_GETCOUNT, 0, 0);
    for i := 0 to cnt - 1 do begin
      len := SendMessage(mHandle, LB_GETTEXTLEN, i, 0);
      SetLength(ws, len);
      SendMessage(mHandle, LB_GETTEXT, i, LPARAM(@ws[1]));
      mStrings.Add(ws);
    end;
  finally mStrings.EndUpdate; end;
  Result := True;
end;
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved