Como abrir uma única vez um formulário MDIChild ou mostra-lo se ele estiver oculto?
Desta forma você vai abrir somente uma vez o form, não importando o número
de vezes em que o usuário clique no botão, por exemplo.
Se a janela já esta aberta, o procedimento seta o foco para a mesma.
Segue o código Fonte:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| procedure ShowChild(MainForm : TForm; InstanceClass: TFormClass; var Reference);
var
Instance: TForm;
begin
Screen.Cursor:= crHourglass;
LockWindowUpdate(MainForm.Handle);
if not IsChildFormExist(InstanceClass) then
try
Instance:= TForm(InstanceClass.NewInstance);
TForm(Reference):= Instance;
try
Instance.Create(MainForm);
if (Instance as TForm).FormStyle = fsNormal then
begin
(Instance as TForm).FormStyle := fsMdiChild;
(Instance as TForm).Visible := True;
end;
except
TForm(Reference):= nil;
Instance.Free;
raise;
end;
finally
Screen.Cursor:= crDefault;
end
else
with TForm(Reference) do begin
if WindowState = wsMinimized then WindowState:= wsNormal;
BringToFront;
Screen.Cursor:= crDefault;
SetFocus;
end;
LockWindowUpdate(0);
end; |
procedure ShowChild(MainForm : TForm; InstanceClass: TFormClass; var Reference);
var
Instance: TForm;
begin
Screen.Cursor:= crHourglass;
LockWindowUpdate(MainForm.Handle);
if not IsChildFormExist(InstanceClass) then
try
Instance:= TForm(InstanceClass.NewInstance);
TForm(Reference):= Instance;
try
Instance.Create(MainForm);
if (Instance as TForm).FormStyle = fsNormal then
begin
(Instance as TForm).FormStyle := fsMdiChild;
(Instance as TForm).Visible := True;
end;
except
TForm(Reference):= nil;
Instance.Free;
raise;
end;
finally
Screen.Cursor:= crDefault;
end
else
with TForm(Reference) do begin
if WindowState = wsMinimized then WindowState:= wsNormal;
BringToFront;
Screen.Cursor:= crDefault;
SetFocus;
end;
LockWindowUpdate(0);
end;
1
2
3
4
| procedure TfrmPrincipal.btnServicoClick(Sender: TObject);
begin
ShowChild(Self, TFrmCLiente, FrmCLiente);
end; |
procedure TfrmPrincipal.btnServicoClick(Sender: TObject);
begin
ShowChild(Self, TFrmCLiente, FrmCLiente);
end;
Esta dica faz uso da dica abaixo:
http://showdelphi.com.br/dica-como-identificar-se-um-formulario-do-tipo-mdichild-ja-esta-criado-delphi/
-
InfusTec
-
6.256 views
- 0 comentários
- 16 de abril de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo