我在做联众的自动走棋程序,在同一机子上登多个号,出现的下棋窗口名称都一样,请问我该怎么得到他们的句柄,并轮流的向他们发送走棋的消息?

解决方案 »

  1.   

    可以用EnumWindows API函数获取
      

  2.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  EnumWindowsProc = function (Hwnd: THandle;Param: Pointer): Boolean; stdcall;
    var
      Form1: TForm1;implementation{$R *.dfm}
    function GetTitle (Hwnd: THandle;Param: Pointer): Boolean; stdcall; //传入窗口句柄
    var
      MyCaption: string;
    begin
      SetLength (MyCaption, 100);
      GetWindowText (Hwnd, PChar (MyCaption), 100); //每列举一个窗口,就利用获得的句柄去读标题
       setlength(MyCaption,strlen(PChar (MyCaption)));
       if  MyCaption = '联众象棋'  then  // 你要寻找的标题
          begin
             showmessage('找到了,^_^');// 插入要处理的过程 ,这句是示例
          end;
          //form1.ListBox1.Items.Add (MyCaption);  Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      EWProc: EnumWindowsProc;
    begin
      EWProc := GetTitle;
      EnumWindows (@EWProc, 0);    //回调函数 ,能列举系统出所有的窗口
    end;end.
      

  3.   

    思路是先获取系统所有窗口的句柄,然后获取这些窗口的标题,如果窗口标题符合条件,则执行设定语句:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;  EnumWindowsProc = function (Hwnd: THandle;Param: Pointer): Boolean; stdcall;
    var
      Form1: TForm1;implementation{$R *.dfm}
    function GetTitle (Hwnd: THandle;Param: Pointer): Boolean; stdcall; //传入窗口句柄
    var
      MyCaption: string;
    begin
      SetLength (MyCaption, 100);
      GetWindowText (Hwnd, PChar (MyCaption), 100); //每列举一个窗口,就利用获得的句柄去读标题
       setlength(MyCaption,strlen(PChar (MyCaption)));
       if  MyCaption = '我的电脑'  then  // 你要寻找的标题,这里是'我的电脑 ',建议按Ctrl+E打开一个Explorer试试看结果
          begin
             showmessage('找到了,改你的标题^_^');// 插入要处理的过程 ,这句是示例
              SetWindowText (Hwnd, PChar ('怎么不是我的电脑了呢?'));    // 插入要处理的过程 ,这句是示例,改变标题
          end;
          、、form1.ListBox1.Items.Add (MyCaption);  Result := True;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      EWProc: EnumWindowsProc;
    begin
      EWProc := GetTitle;
      EnumWindows (@EWProc, 0);    //回调函数 ,能列举系统出所有的窗口
    end;end.
      

  4.   

    注意把 、、form1.ListBox1.Items.Add (MyCaption);删掉,这是我测试的语句