我if条件建立一个模态窗口,不过关掉时它又弹出一个,如何在if条件成立只建立一个窗口? (100分)

  • 主题发起人 主题发起人 路远
  • 开始时间 开始时间

路远

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我的代码,<br>function EnumChildProc (Childhwnd: THandle; Param: Pointer): Boolean; stdcall;<br>var<br>dlghwnd:THandle;<br>path : string;<br>Modal :TWinSortDialog;<br>begin<br>&nbsp; Result := True;<br>&nbsp; Setlength(childwindowtext,100);<br>&nbsp; getwindowtext (Childhwnd,pchar(childwindowtext),100);<br>&nbsp; childwindowtext := pchar(childwindowtext);<br>&nbsp; Reg:=tregistry.create;<br>&nbsp; &nbsp; if &nbsp;AnsiStartsText(Path,childwindowtext)=true &nbsp; then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modal:=TWinSortDialog.Create (Application);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modal.Edit1.Text :=ChildWindowText;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal.ShowModal;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; Result := False;<br>end;
 
各位帮一下呀
 
var<br>&nbsp; modal:TWinSortDialog;<br>&nbsp; /////声明为全局变量<br>..........<br><br>function EnumChildProc (Childhwnd: THandle; Param: Pointer): Boolean; stdcall;<br>var<br>&nbsp; dlghwnd:THandle;<br>&nbsp; path : string;<br>begin<br>&nbsp; Result := True;<br>&nbsp; Setlength(childwindowtext,100);<br>&nbsp; getwindowtext (Childhwnd,pchar(childwindowtext),100);<br>&nbsp; childwindowtext := pchar(childwindowtext);<br>&nbsp; Reg:=tregistry.create;<br>&nbsp; &nbsp; if &nbsp;AnsiStartsText(Path,childwindowtext) and not Assigned(modal) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;////未被创建过<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modal:=TWinSortDialog.Create (Application);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modal.Edit1.Text :=ChildWindowText;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal.ShowModal;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; Result := False;<br>end;<br><br>//调用枚举子窗口过程语句<br>&nbsp; EnumChildWindows(AHWnd,@EnumChildProc,1);<br>&nbsp; Modal := nil;////////下次调用就为空<br><br>或者用一全局的标志来控制
 
创建之前看是否有此实例存存,存在了就不用创建成了啥
 
轻舞肥羊, 我用你的代码不过刚一关掉就又弹出了
 
我一开始用了这个条件是否有此实例存在,<br>dlghwnd:=Findwindow ('TWinSortDialog',nil);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if dlghwnd = 0 then<br>问题是关过了就他又马上创建一个窗口
 
你有没有把Modal声明为全局变量?<br>Modal.Free并没有把Modal置为nil,所以,下一次调用Assigned(Modal)时会返回真的,<br>你可以设断点看看<br><br>不过设置一个全局变量标志更容易理解
 
<br>&nbsp; &nbsp; if &nbsp;AnsiStartsText(Path,childwindowtext) and not Assigned(modal) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;////未被创建过<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modal:=TWinSortDialog.Create (Application);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;modal.Edit1.Text :=ChildWindowText;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal.ShowModal;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal.Free;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Modal := Nil; &nbsp;//***8<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;
 
我把Modal声明为全局变量啦<br>关TWinSortDialog 是在TWinSortDialog 用了个Button<br>procedure TWinSortDialog.BitBtn1Click(Sender: TObject);<br>begin<br>&nbsp;Close;<br>end;<br>procedure TWinSortDialog.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp;Action := caFree;<br>end;
 
创建时的条件如果没有则创建,<br>如果我一关WinSortDialog那么条件是不是又成立了?如此不就成了一个循环。<br>所以是我关过了就他又马上创建一个窗口?这个问题能不能为某一个条件成立时创建一个窗口。
 
多人接受答案了。
 
后退
顶部