onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

测试:

 

函数代码:

function FindWindowXG(strClass, strTitle: string): THandle;
var
  hd: THandle;
  arrClass: array[0..99] of Char;
  arrTitle: array[0..99] of Char;
  wClass, wTitle: string;
begin
  hd := GetWindow(Application.Handle, GW_HWNDFIRST);
  while hd > 0 do
  begin
    GetClassName(hd, @arrClass[0], Length(arrClass));
    GetWindowText(hd, @arrTitle[0], Length(arrTitle));

    wClass := arrClass;
    wTitle := arrTitle;

    strClass := Trim(strClass);
    strTitle := Trim(strTitle);
    if (Length(strClass) > 0) and (Length(strTitle) = 0) then
    begin
      if UpperCase(strClass) = UpperCase(wClass) then
      begin
        Result := hd;
        Exit;
      end;
    end;

    if (Length(strClass) = 0) and (Length(strTitle) > 0) then
    begin
      if Pos(UpperCase(strTitle), UpperCase(wTitle)) > 0 then
      begin
        Result := hd;
        Exit;
      end;
    end;

    if (Length(strClass) > 0) and (Length(strTitle) > 0) then
    begin
      if (UpperCase(strClass) = UpperCase(wClass))
        and (Pos(UpperCase(strTitle), UpperCase(wTitle)) > 0) then
      begin
        Result := hd;
        Exit;
      end;
    end;

    hd := GetNextWindow(hd, GW_HWNDNEXT);
  end;

  Result := 0;
end;

 

 

测试代码:

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

function FindWindowXG(strClass, strTitle: string): THandle;
var
  hd: THandle;
  arrClass: array[0..99] of Char;
  arrTitle: array[0..99] of Char;
  wClass, wTitle: string;
begin
  hd := GetWindow(Application.Handle, GW_HWNDFIRST);
  while hd > 0 do
  begin
    GetClassName(hd, @arrClass[0], Length(arrClass));
    GetWindowText(hd, @arrTitle[0], Length(arrTitle));

    wClass := arrClass;
    wTitle := arrTitle;

    strClass := Trim(strClass);
    strTitle := Trim(strTitle);
    if (Length(strClass) > 0) and (Length(strTitle) = 0) then
    begin
      if UpperCase(strClass) = UpperCase(wClass) then
      begin
        Result := hd;
        Exit;
      end;
    end;

    if (Length(strClass) = 0) and (Length(strTitle) > 0) then
    begin
      if Pos(UpperCase(strTitle), UpperCase(wTitle)) > 0 then
      begin
        Result := hd;
        Exit;
      end;
    end;

    if (Length(strClass) > 0) and (Length(strTitle) > 0) then
    begin
      if (UpperCase(strClass) = UpperCase(wClass))
        and (Pos(UpperCase(strTitle), UpperCase(wTitle)) > 0) then
      begin
        Result := hd;
        Exit;
      end;
    end;

    hd := GetNextWindow(hd, GW_HWNDNEXT);
  end;

  Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit3.Text := IntToStr(FindWindowXG(Edit1.Text,Edit2.Text));
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  Edit3.Clear
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  Edit3.Clear
end;

end.

 

posted on 2019-05-08 10:33  onlyou13  阅读(129)  评论(0编辑  收藏  举报