// Delphi中给出窗口标题的部分字符串,求窗口的句柄,窗口模糊查询

// Delphi中给出窗口标题的部分字符串,求窗口的句柄,窗口模糊查询

 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetWndText(hWnd: hWnd): string;
var
  Ret: LongInt;
  mText: PChar;
  Buf: Integer;
begin
  Ret := SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1;
  GetMem(mText, Ret);
  try
    SendMessage(hWnd, WM_GETTEXT, Ret, LPARAM(mText));
    Result := mText; // WideCharToString(mText);
  finally
    FreeMem(mText, Ret);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  hCurrentWindow: hWnd;
  WndText: String;
begin
  hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST);
  while hCurrentWindow <> 0 do
  begin
     WndText := GetWndText(hCurrentWindow);
    // if UpperCase(WndText)='窗口的标题' then begin
    // Delphi中给出窗口标题的部分字符串,求窗口的句柄,窗口模糊查询
    if pos('EditPlus', WndText) <> 0 then // Delphi中给出窗口标题的部分字符串,求窗口的句柄,窗口模糊查询
    // Delphi中给出窗口标题的部分字符串,求窗口的句柄,窗口模糊查询
    begin
      {
        PostMessage(  hCurrentWindow, WM_SYSCOMMAND, SC_MAXIMIZE, 0); // 最大化
        SetForegroundWindow(  hCurrentWindow);
        Sleep(100);
        SetWindowPos(  hCurrentWindow, HWND_TOP, 0, 0, Screen.Width,
        Screen.Height, SWP_SHOWWINDOW);
      }
      Edit1.Text := WndText;
      Exit;
    end;
    hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT);
  end;
end;

end.

posted @ 2013-11-02 12:14  delphichm  阅读(996)  评论(0编辑  收藏  举报