我用这段代码取出来的句柄不对啊var
  HWND: DWORD;
  Pid: Integer;
begin
  Sleep(2000);
  HWND := GetForegroundWindow;
  Pid := GetWindowThreadProcessId(HWND, nil);
  if AttachThreadInput(Pid, GetCurrentThreadId, True) then
  begin
    HWND := GetFocus;
    AttachThreadInput(Pid, GetCurrentThreadId, False);
    ShowMessage(IntToStr(HWND));
  end
  else raise Exception.Create('Couldn''t Find Window!');
end;

解决方案 »

  1.   


       type
        EnumWindowsProc = function (Hwnd: THandle;
          Param: Pointer): Boolean; stdcall;
    var
      Form1: TForm1;implementation{$R *.dfm}
    function GetTitle (Hwnd: THandle; Param: Pointer): Boolean; stdcall;
    var
        Text,text2: string;
    begin
        SetLength (Text, 100);
        setlength(text2,40);
    //    text:='xunsha';
        GetWindowText (Hwnd, PChar (Text), 100);
        getclassname(hwnd,pchar(text2),100);
        form1.ListBox1.Items.Add (
          IntToStr (Hwnd) + ' : '+ text2 +'     : '+ Text);
        Result := True;
    end;function Callback(h:hwnd;param:Lparam):boolean;stdcall;var  buf:array[0..255] of char;
       buff:array[0..255] of char;
      text:array [0..12] of string;
    begin
        result:=true;
        Getclassname(h,@buf,255);
      //  getclassname(h,pchar(text[1]),12);
     showmessage(strpas(buf));
         if strpas(buf)='ThunderRT6CommandButton' then
       begin
      Enablewindow(h,true);
       sendmessage(h,wm_gettext,255,integer(@buff));
    //   if strpas(buff)='' then  sendmessage(h,wm_settext,255,integer(pchar('Password')));
     //  showmessage(inttostr(h));
    //  sendmessage(h,wm_gettext,255,integer(@buff));
     showmessage(strpas(buff));
    // result:=false;
      end;end;
    function GetTitle2 (Hwnd: THandle; Param: Pointer): Boolean; stdcall;
    var
        Text: string;
    begin
        SetLength (Text, 100);
        GetWindowText (Hwnd, PChar (Text), 100);
    //    if copy(text,1,8)='系统登录' then
    form1.Edit4.Text:=text;    form1.Memo1.Lines.Add(IntToStr (Hwnd) + ': ' + Text);
        Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
        EWProc: EnumWindowsProc;
    begin
        ListBox1.Items.Clear;
        EWProc := GetTitle;
     EnumWindows(@ewproc,0);end;procedure TForm1.Button2Click(Sender: TObject);
    var
        EWProc: EnumWindowsProc;
    begin
       memo1.Clear;
        EWProc := GetTitle2;
       EnumChildWindows(p,@CallBack,0);
    end;