up 谢谢上面的几位大!! to 回复人: Cipherliu(孤鹰) ( ) 信誉:100 如果你不知现在有哪些窗口,可以用EnumWindows找到所有的窗口 那怎么做呢?有没有代码?谢谢了,如果分不够,,我在用别的号给分,,谢谢了!!!!!!!
用这个法行不行。不过要求你创建的窗体要依主窗体为parent。可以用这个思路扩展 var i:integer; begin i:=0; while (ComponentCount>0) and (i<ComponentCount) do if Components[i] is TForm then begin TForm(Components[i]).Close;//or free i:=0 end else Inc(i); end;
不是这样的你那就是Application.ComponentCount拉。道理一样 var i:integer; begin i:=0; while (Application.ComponentCount>0) and (i<Application.ComponentCount) do if (Application.Components[i] is TForm)and not (Application.Components[i] is TMainForm) then //TMainForm is your MainForm ,Replace begin TForm(Application.Components[i]).Close;//or free i:=0 end else Inc(i); end;
这样试一下: var form1:Tform;for i := 0 to Screen.FormCount -1 do if Screen.Forms[i].ClassType = FormClass then begin form1:=Screen.Forms[i]; form1.close; end;
function TMainForm.existform(formname:string):boolean; var i:integer; begin result:=false; with mainform do for i:=mdichildcount-1 downto 0 do if pos(uppercase(formname),uppercase(mdichildren[i].Name ))>0 then begin result:=true; break; end; end;///////// 运用:假如你打开了A,现在要找开B,那就在B的菜单写:if mainform.existform('A')then begin A.Close ; A.Destroy ; end; /////// if not mainform.existform('B')then begin application.CreateForm(TB,B); B.Show ; end;
谢谢上面的几位大!!
to 回复人: Cipherliu(孤鹰) ( ) 信誉:100 如果你不知现在有哪些窗口,可以用EnumWindows找到所有的窗口
那怎么做呢?有没有代码?谢谢了,如果分不够,,我在用别的号给分,,谢谢了!!!!!!!
var i:integer;
begin
i:=0;
while (ComponentCount>0) and (i<ComponentCount) do
if Components[i] is TForm then
begin
TForm(Components[i]).Close;//or free
i:=0
end else Inc(i);
end;
var i:integer;
begin
i:=0;
while (Application.ComponentCount>0) and (i<Application.ComponentCount) do
if (Application.Components[i] is TForm)and not (Application.Components[i] is TMainForm) then //TMainForm is your MainForm ,Replace
begin
TForm(Application.Components[i]).Close;//or free
i:=0
end else Inc(i);
end;
窗体开始就设计好谢谢上面的!!!
var form1:Tform;for i := 0 to Screen.FormCount -1 do
if Screen.Forms[i].ClassType = FormClass then begin
form1:=Screen.Forms[i];
form1.close;
end;
function TMainForm.existform(formname:string):boolean;
var
i:integer;
begin
result:=false;
with mainform do
for i:=mdichildcount-1 downto 0 do
if pos(uppercase(formname),uppercase(mdichildren[i].Name ))>0 then
begin
result:=true;
break;
end;
end;/////////
运用:假如你打开了A,现在要找开B,那就在B的菜单写:if mainform.existform('A')then
begin
A.Close ;
A.Destroy ;
end; ///////
if not mainform.existform('B')then
begin
application.CreateForm(TB,B);
B.Show ;
end;
WNDENUMPROC lpEnumFunc, // address of callback function
LPARAM lParam // application-defined value
);
当然,这是个C语言的定义。我们可以查看Windows 单元,从中找到相应的Pascal 语言定义:function EnumWindows (
lpEnumFunc: TFNWndEnumProc;
lParam: LPARAM): BOOL; stdcall;
查阅帮助文件,我们发现作为参数传递的函数应该属于下面的类型(也是在C中):BOOL CALLBACK EnumWindowsProc (
HWND hwnd, // handle of parent window
LPARAM lParam // application-defined value
);
这与下面的Delphi 过程类型定义一致:type
EnumWindowsProc = function (Hwnd: THandle;
Param: Pointer): Boolean; stdcall;
其中第一个参数是各主窗体的句柄,第二个参数则是调用EnumWindows 函数时所传递的值。实际上,Pascal 中没有相应的TFNWndEnumProc类型定义 ,它只是个指针。这意味着我们需要传递一个带有合适参数的函数,将它用作一个指针,也就是取函数的地址而不是调用它。不幸的是,这也意味着如果某个参数类型出现错误时,编译器不会给予提示。每当调用Windows API函数或传递一个回调函数给系统时,Windows 要求程序员遵循stdcall 调用协定。缺省情况下,Delphi使用另一种更高效的调用协定,其关键字为register。下面是一个与定义兼容的回调函数,此函数把窗口的标题读到字符串中,然后添加到给定窗体的一个列表框中:function GetTitle (Hwnd: THandle; Param: Pointer): Boolean; stdcall;
var
Text: string;
begin
SetLength (Text, 100);
GetWindowText (Hwnd, PChar (Text), 100);
FormCallBack.ListBox1.Items.Add (
IntToStr (Hwnd) + ': ' + Text);
Result := True;
end;
窗体有一个几乎覆盖整个窗体的列表框,窗体顶部有一个小面板,面板上有一个按钮。当按下按钮时,EnumWindows API函数被调用,并且GetTitle 函数作为参数传递给它:procedure TFormCallback.BtnTitlesClick(Sender: TObject);
var
EWProc: EnumWindowsProc;
begin
ListBox1.Items.Clear;
EWProc := GetTitle;
EnumWindows (@EWProc, 0);
end;
你可以直接调用GetTitle函数,不必先把值保存到过程类型临时变量中,上例这么做是为了使回调过程更清楚。程序运行结果确实很有意思,正如你在图9.2中看到的那样,结果显示了系统中正在运行的所有主窗口,其中大部分是隐藏的,你通常看不到,许多实际上没有标题。你改写GetTitle 函数,就可以关闭其它的窗口了.
注意,别把自己也关闭了,检查一下句柄是不是自己的句柄.