俺正在做一个QQ类似的工具,如果发送给A的发送消息窗口已经打开,获得焦点,否则创建一个实例;发送给B时情况如A,就像QQ发送消息一样,给不同的好友发消息可以成功创建实例,若是同一好友,则调出原先打开的窗口.发送窗口均为同一窗体,点击时再创建它的实例.请问各位大虾能不能在创建实例时给它一个标识,往后再按这个标识取得它?????求助中.........

解决方案 »

  1.   

    支持ing,我也在考虑这个问题,
      

  2.   

    最好用窗体名称判断吧:
    比如发消息给A的时候:
    if (Form1<>nil) and (Form1.Caption='与A对话') then 
      begin
        Form1.show;  Form1.BringTOFront;
      end
    else
      begin
       Form1:=TForm1.Create(nil);
       Form1.Caption:='与A对话';
       Form1.Show;
      end;
      

  3.   

    但是当我打开多个窗口时(同一个窗体的实例,如给B对话、给C对话、给A对话),怎么判定选择它们。楼上的感觉好像只是一个Form1在running....
      

  4.   

    我记得有一个api函数可以实现判断是否运行了 好像是什么findwindow之类的吧
    记不清楚了
      

  5.   

    1.声明函数如下: 
    FUNCTION  ShowWindow(winhandle:integer; wincommand:integer):integer;stdcall;external'User32.dll';
    FUNCTION  BringWindowToTop(HWND:integer):integer;stdcall;external'User32.dll';
    FUNCTION  FindWindowA(Winhandle:integer; wintitle:string) :integer;stdcall;external'User32.dll';
    2.
    procedure TForm1.Button1Click(Sender: TObject);
    Var handle:integer;
    begin
    handle:= FindWindowA( 0, 'Form2' );
    If handle > 0 Then
      begin
      BringWindowToTop(handle);
      ShowWindow(handle ,5 );
      end
    else
      begin
      Application.CreateForm(TForm2, Form2);
      Form2.showmodal;
      end;end;
      

  6.   

    大家写的都好像是针对'窗口再次运行'的情况呀,我的情况是:跟QQ的发好在好友消息一样,同窗口可以重建,但同一个好友的发送窗口只有一个。有没有给实例加一个标识的东西??还有如何获得一个窗体的所有实例???
    eg.
    已生成的窗口实例:
       Form1.=好友A
       Form1.=好友B
      

  7.   

    (1)项目文件 Project1.dpr
    program Project1;
    uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};
    {$R *.res}
    begin
      if CreateMutex then begin
        Application.Initialize;
        Application.CreateForm(TForm1, Form1);
        Application.Run;
      end else begin
        DestroyMutex;
      end;
    end.(2)==============
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      function CreateMutex: Boolean;
      procedure DestroyMutex;implementationvar Mutex: hWnd;{$R *.dfm}procedure DestroyMutex;
    begin
      if Mutex <> 0 then
        CloseHandle(Mutex);
    end;function CreateMutex: Boolean;
    var
      PrevInstHandle: THandle;
      AppTitle: PChar;
    begin
      AppTitle := StrAlloc(100);
      StrPCopy(AppTitle, Application.Title);
      Result := True;
      Mutex := Windows.CreateMutex(nil, False, AppTitle);
      if (GetLastError = ERROR_ALREADY_EXISTS) or (Mutex = 0) then begin
        Result := False;
        SetWindowText(Application.Handle, '');
        PrevInstHandle := FindWindow(nil, AppTitle);
        if PrevInstHandle <> 0 then begin
          if IsIconic(PrevInstHandle) then
            ShowWindow(PrevInstHandle, SW_RESTORE)
          else
            BringWindowToTop(PrevInstHandle);
          SetForegroundWindow(PrevInstHandle);
        end;
        if Mutex <> 0 then
          Mutex := 0;
      end;
      StrDispose(AppTitle);
    end;end.