就是用到了bpl封装了窗体单元,后在主程序里动态调用bpl,可以在主程序里调用到封装的窗体单元,现在的问题是:多次点击时会有多个重复的窗体单元,现在是不知道怎么判断该窗体已打开,已存在的情况下,只要放到最前面就行了。
if (ChildForm[n,0] <> EmptyStr) and (ChildForm[n,0] = 'base') then
begin
TForm(BplForm).BringToFront; //这个bpl是字符串,就是内存地址出错,自己测试用的
Abort;
end;
if (ChildForm[n,0] <> EmptyStr) and (ChildForm[n,0] = 'base') then
begin
TForm(BplForm).BringToFront; //这个bpl是字符串,就是内存地址出错,自己测试用的
Abort;
end;
var
ps: array[0..254] of Char;
hChild:HWND;
begin
hChild:= GetWindow(ParentWnd, GW_CHILD); // 取得第一個子視窗,ParentWnd:主窗口句柄
while hChild<> 0 do
begin
if IsWindowVisible(hChild) then
begin
GetClassName(hChild,ps, 255);
if SameText(StrPas(ps[0]),'你的子窗口类名') then // 如果类名相同就置前
begin
SetForegroundWindow(hChild);
exit;
end;
end;
hChild:=GetWindow(ParentWnd, GW_HWNDNEXT); // 取得下一個 siblings 視窗
end;
//如果没有就直接创建
end;
TForm(BplForm).BringToFront; //这个bpl是字符串,就是内存地址出错,自己测试用的
这句代码不能show出窗体,只有一个内存地址错误,主要是bplForm是字符串而不是对像,那我要怎么样才能BringToFront出来已打开的窗体。
var
Aclass: TFormClass;
ChildForm: array [0..3,0..1] of TFormClass;//全局
///--------------------------------///
Aclass := TFormClass(GetClass(BplForm));
if Aclass <> nil then
begin
for n := 0 to Screen.FormCount - 1 do
begin
if ChildForm[n, 0] = nil then
begin
with TFormClass(Aclass).Create(Application) as TForm do
begin
ChildForm[n , 0] := Aclass;
//ChildForm[n , 1] := Caption;
FormStyle := fsMDIChild;
WindowState := wsMaximized;
Break;
end;
end
else
if (ChildForm[n,0] <> nil) and (TFormClass(Aclass) = ChildForm[n,0]) then
begin
//ShowMessage(toString(TFormClass(Aclass)));
(TFormClass(Aclass) as TForm).BringToFront;
Abort;
end;
end;
case flag of
1: Show;
2: ShowModal;
3: Hide;
end;
我的代码
hChild:= GetWindow(ParentWnd, GW_CHILD); m617105 (押宝)对于这个我是传递一个主窗体句柄进来 ,可是上面这个得到的都是0,无论我是否创建有窗体。不明白,还是要对mdi要创建的窗体设置他的父窗体所有者?
MID窗体的容器为MDIClient,需要先找到MDIClient窗口句柄
var
ParentWnd: THandle;
ps: array[0..254] of Char;
hChild:HWND;
begin
ParentWnd:= Handle;
ParentWnd:= FindWindowEx(ParentWnd, 0, 'MDIClient ', nil);
if ParentWnd=0 then Exit;
hChild:= GetWindow(ParentWnd,GW_CHILD);
while hChild<> 0 do
begin
if IsWindowVisible(hChild) then
begin
GetClassName(hChild,ps, 255);
if SameText(StrPas(ps[0]),'你的子窗口类名') then // 如果类名相同就置前
begin
SetForegroundWindow(hChild);
exit;
end;
end;
hChild:=GetWindow(ParentWnd, GW_HWNDNEXT); // 取得下一個 siblings 視窗
end;
//如果没有就直接创建
end;