Mega Code Archive

 
Categories / Delphi / Examples
 

Minimize-maximize-close components at runtime

{ This program try to show something at least interesting about those components on the form. You'll see that by the fact they're descendant of TWinControl they can react like a Window. I mean they can be maximized, minimized and so on. If you see a control that not respond to our commands it's because it doesn't descend from TWinControl. I didn't try to deal with them. Well. I believe this program is an open door to great ideas. There's a lot of components that could take advantages of this to manipulate controls on the form. You know a long journey starts on the first step. Here it is. Show me anything great you could do out of these ideas. Good work. Babak Sateli babak_sateli@yahoo.com } unit MessComps; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Menus, ExtCtrls; type TMessing = class(TForm) Menu: TMainMenu; Options: TMenuItem; Minimize: TMenuItem; Restore: TMenuItem; Hide: TMenuItem; Show: TMenuItem; N2: TMenuItem; Close: TMenuItem; Exit_: TMenuItem; CloseProgram: TMenuItem; Maximize: TMenuItem; Memo: TMemo; Move: TMenuItem; ChangeSize: TMenuItem; N1: TMenuItem; N3: TMenuItem; Target: TMenuItem; procedure Procede(Sender: TObject); procedure TargetClick(Sender: TObject); end; var Messing: TMessing; n: Integer; AllOfThem: Boolean = True; implementation {$R *.DFM} procedure TMessing.Procede(Sender: TObject); var n, Tag: Integer; T: TComponent; ParentClass: TClass; OkToProcede: boolean; Comando: Word; HasTheFocus: Boolean; begin // As the menu itens do pratically the same thing we directed them all to here. // Their tag properties are different so we could know who called us. // Save this. We'll need it Tag := (Sender as TMenuItem).Tag; case (Sender as TMenuItem).Tag of // Oh! You just click options but you're already here. You didn't choose one // yet so exit. 0: Exit; // Now we have the options. Change comando accordingly to reflect our option. 1: COMANDO := SW_MINIMIZE; 2: COMANDO := SW_MAXIMIZE; 3: COMANDO := SW_RESTORE; 4: COMANDO := SW_HIDE; 5: COMANDO := SW_SHOW; 6: COMANDO := SC_SIZE; 7: COMANDO := SC_MOVE; 8: COMANDO := SC_CLOSE; 9: Application.Terminate; end; // Now we know the command we are ready to procede.. // So we start looking for the components this form has. for n := 1 to ComponentCount do // Well componentCount starts with 1 begin // but ComponentsList starts with 0 (zero) so we make some adjustment T := Components[n - 1]; // Now we have the component in our T variable and can ask a few questions ParentClass := T.ClassParent; // We want to know its ClassParent. OkToProcede := ParentClass = TWinControl; // if it is descendant of TWinControl then it's ok to procede. while (ParentClass nil) and (OkToProcede = False) do begin // Oh no! Not yet. Maybe it descends of a lower class that is // descendant of TWinControl. So we keep going back in its "genealogical" // tree to see if we can find TWinControl back there somewhere or get to nil. OkToProcede := ParentClass = TWinControl; ParentClass := ParentClass.Classparent; end; // if we got to nil and didn't find TWinControl then we have nothing to do // this component doen't accept our kinda of commands. if OkToProcede then begin // Does it has the focus? HasTheFocus := (T as TWinControl).Focused; // If not AllOfThem and not has the focus so we break to the next one. if (not AllOfThem) and (not HasTheFocus) then Continue; if Tag then // There no SC_SIZE or SC_MOVE or SC_CLOSE command in here so we have to apart. ShowWindow((T as TWinControl).Handle, COMANDO) else // CloseWindow((T as TWinControl).Handle ); Doesn't work. It minimize them. // But this one bellow works. It's possible to show them after closed but // the controls loose some of their properties. Methinks it's dangerous (T as TWinControl).Perform(WM_SYSCOMMAND, COMANDO, 0); end; end; end; // Changing our target procedure TMessing.TargetClick(Sender: TObject); begin if (Sender as TMenuItem).Tag = 40 then begin (Sender as TMenuItem).Caption := 'Target: the focused one'; (Sender as TMenuItem).Tag := 41; AllofThem := False; end else begin (Sender as TMenuItem).Caption := 'Target: all components'; (Sender as TMenuItem).Tag := 40; AllofThem := True; end; end; end.