Recent

Author Topic: Atribuir evento no onClick em tempo de execução  (Read 5467 times)

alessandrodidi

  • New member
  • *
  • Posts: 9
Atribuir evento no onClick em tempo de execução
« on: July 22, 2021, 01:43:33 am »
Saudações developers!!!

Esta é a minha primeira postagem aqui no fórum.

A minha questão é a seguinte: estou desenvolvendo um sistema com modelagem TDI utilizando PageControl e para facilitar a reutilização de código desenvolvi uma procedure que fica em uma unit exclusiva para esta finalidade. Nesta unit eu criei uma classe para as TabSheets onde eu chamarei os forms. Até aí está funcionando bem, porém esbarrei em um problema, o botão fechar na aba. Como o Lazarus não permite utilizar o OwnerDraw no PageControl eu não consegui desenhar o botão fechar na aba, mas para contornar isso coloquei um Panel que fica na parte superior e da TabSheet e nele eu criei o botão fechar. Novamente, até aí funcionando bem.
O meu problema está na hora de atribuir a procedure para fechar a TabSheet no evento onClick do botão. Li algumas referências sobre o assunto, entretanto ainda não consegui uma forma de fazer funcionar.

Para facilitar estou postando o código

Code: Pascal  [Select][+][-]
  1. unit ExpertTabSheet;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Classes, ExtCtrls, Dialogs, ComCtrls, Forms, Buttons,
  7.   Controls, SysUtils, Themes, UxTheme, Graphics;
  8.  
  9. type ETabSheet = Class (TTabSheet)
  10.   private
  11.  
  12.   protected
  13.  
  14.   public
  15.     constructor Create(AOwner: TComponent); override;
  16.     destructor Destroy; override;
  17. end;
  18.  
  19.   procedure OpenWindowTab(PageControl: TPageControl; FormClass: TFormClass; MultiTab: Boolean);
  20.   procedure ClickEvent(Sender: TObject);
  21.  
  22. implementation
  23.  
  24. constructor ETabSheet.Create(AOwner:TComponent);
  25. begin
  26.   inherited Create(AOwner);
  27. end;
  28.  
  29. destructor ETabSheet.Destroy;
  30. begin
  31.   inherited Destroy;
  32. end;
  33.  
  34. procedure OpenWindowTab(PageControl: TPageControl; FormClass: TFormClass; MultiTab: Boolean);
  35. var
  36.   i: Integer;
  37.   Form: TForm;
  38.   TabSheet: ETabSheet;
  39.   ShiftOn: TShiftState;
  40.   PPointer: TPoint;
  41.   btnClose: TSpeedButton;
  42.   TitleBar, ButtonsSet: TPanel;
  43. begin
  44.   try
  45.     //Cria a TabSheet
  46.     TabSheet := ETabSheet.Create(PageControl);
  47.  
  48.     //Verifica se o form já foi criado
  49.     Form := FormClass.Create(TabSheet);
  50.     for i := 0 to PageControl.ControlCount -1 do
  51.     if (PageControl.Pages[i].Caption = Form.Caption) and (MultiTab = False) then
  52.       begin
  53.         PageControl.ActivePageIndex := i;
  54.         exit;
  55.       end;
  56.  
  57.     //Cria o Form
  58.     TabSheet.PageControl := PageControl;
  59.     Form := FormClass.Create(TabSheet);
  60.     Form.Parent := TabSheet;
  61.  
  62.     //Cria uma barra de títulos para a TabSheet
  63.     TitleBar := TPanel.Create(TabSheet);
  64.     TitleBar.Parent := TabSheet;
  65.     TitleBar.Align := alTop;
  66.     TitleBar.Height := 22;
  67.     TitleBar.Caption := Form.Caption;
  68.     TitleBar.Color := clWindow;
  69.     TitleBar.Visible := True;
  70.  
  71.     //Propriedade do Form
  72.     Form.Align := alClient;
  73.     Form.BorderStyle := bsNone;
  74.     Form.Visible := True;
  75.     TabSheet.Caption := Form.Caption;
  76.  
  77.     //Cria um container para os botões
  78.     ButtonsSet := TPanel.Create(TitleBar);
  79.     ButtonsSet.Parent := TitleBar;
  80.     ButtonsSet.Align := alRight;
  81.     ButtonsSet.Caption := '';
  82.     ButtonsSet.Width := 80;
  83.     ButtonsSet.BorderSpacing.Right := 1;
  84.     ButtonsSet.BiDiMode := bdRightToLeft;
  85.     ButtonsSet.BorderStyle := bsNone;
  86.     ButtonsSet.BevelInner := bvNone;
  87.     ButtonsSet.BevelOuter := bvNone;
  88.     ButtonsSet.Anchors := [akTop,akRight];
  89.     ButtonsSet.Color := clNone;
  90.     ButtonsSet.Visible := True;
  91.  
  92.     //Cria o botão para fechar a TabSheet
  93.     btnClose := TSpeedButton.Create(ButtonsSet);
  94.     btnClose.Parent := ButtonsSet;
  95.     btnClose.Width := 19;
  96.     btnClose.Height := 19;
  97.     btnClose.Left := 60;
  98.     btnClose.BorderSpacing.Around := 1;
  99.     btnClose.BorderSpacing.InnerBorder := 0;
  100.     btnClose.BorderSpacing.CellAlignHorizontal := ccaCenter;
  101.     btnClose.BorderSpacing.CellAlignVertical := ccaCenter;
  102.     btnClose.Align := alNone;
  103.     btnClose.Anchors := [akTop,akRight];
  104.     btnClose.Caption := 'X';
  105.     btnClose.Hint := 'Fechar aba';
  106.     btnClose.Transparent := True;
  107.     btnClose.ShowHint := True;
  108.     btnClose.Visible := True;
  109.     btnClose.OnClick := @ClickEvent;
  110.  
  111.     //Coloca o foco na TabSheet aberta
  112.     PageControl.ActivePage := TabSheet;
  113.     Form.SetFocus;
  114.   except
  115.     Form.Free;
  116.   end;
  117. end;
  118.  
  119. procedure ClickEvent(Sender: TObject);
  120. begin
  121.   ShowMessage('Aqui fica a rotina para destruir a TabSheet e o Form');
  122. end;
  123.  
  124.  
  125. initialization
  126.   ETabSheet.ClassName;
  127.  
  128. end.
  129.  
« Last Edit: July 22, 2021, 01:57:30 am by alessandrodidi »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Atribuir evento no onClick em tempo de execução
« Reply #1 on: July 22, 2021, 02:03:45 am »
Excluir uma TTabSheet é tão simples quanto isto:
Code: Pascal  [Select][+][-]
  1. procedure CloseTab(const ATab: TTabSheet);
  2. begin
  3.   if Assigned(ATab) then begin
  4.     {Se houver controles cujo proprietário (Owner)
  5.      não seja o TabSheet, faça Free aqui}
  6.     ATab.Free;
  7.   end;
  8. end;


Traduzido pelo Google ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

alessandrodidi

  • New member
  • *
  • Posts: 9
Re: Atribuir evento no onClick em tempo de execução
« Reply #2 on: July 22, 2021, 02:34:30 am »
Ótimo lucamar, obrigado pela contribuição.
Mais uma questão: para eu atribuir essa procedure no evento onclick do botão fechar, basta eu atribui-la, tipo
Code: Pascal  [Select][+][-]
  1. ...
  2. BotaoFechar.onClick := @CloseTab(minhaTabSheet);
  3. ...

Seria somente isso ou há mais algum cuidado que devo tomar para poder chamar essa procedure no botão fechar?



Excluir uma TTabSheet é tão simples quanto isto:
Code: Pascal  [Select][+][-]
  1. procedure CloseTab(const ATab: TTabSheet);
  2. begin
  3.   if Assigned(ATab) then begin
  4.     {Se houver controles cujo proprietário (Owner)
  5.      não seja o TabSheet, faça Free aqui}
  6.     ATab.Free;
  7.   end;
  8. end;


Traduzido pelo Google ;)
« Last Edit: July 22, 2021, 02:45:25 am by alessandrodidi »

pauloh0

  • Newbie
  • Posts: 3
Re: Atribuir evento no onClick em tempo de execução
« Reply #3 on: September 21, 2021, 10:03:05 pm »
Olá se entendi bem é um clique em Runtime que voce precisa:

BitBtn2Click(self);
ou
BitBtn2Click(nil);

 

TinyPortal © 2005-2018