Ignore:
Timestamp:
Feb 29, 2016, 5:23:37 PM (8 years ago)
Author:
chronos
Message:
  • Modified: TForm title bar implemented using controls.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • os/trunk/Xvcl/Xvcl.Forms.pas

    r13 r14  
    88type
    99  TBorderStyle = (bsNormal, bsNone);
     10  TPanel = class;
     11  TForm = class;
     12  TApplication = class;
     13
     14  TFormTitleBar = class
     15  private
     16    function GetForm: TForm;
     17    procedure SetForm(const Value: TForm);
     18    procedure DoMaximize(Sender: TObject);
     19    procedure DoClose(Sender: TObject);
     20    procedure DoMouseDown(Sender: TObject; Position: TPoint; Buttons: TMouseButtonSet);
     21  public
     22  const
     23    TitleBarHeight = 24;
     24  var
     25    MainLabel: TLabel;
     26    Panel: TPanel;
     27    MaximizeButton: TButton;
     28    MinimizeButton: TButton;
     29    CloseButton: TButton;
     30    procedure Paint;
     31    constructor Create;
     32    destructor Destroy; override;
     33    property Form: TForm read GetForm write SetForm;
     34  end;
    1035
    1136  TForm = class(TWinControl)
     
    1843    function GetVideoDevice: TVideoDevice; override;
    1944  public
    20   const
    21     TitleBarHeight = 24;
    2245  var
    2346    Screen: TObject; // TScreen;
     47    Application: TApplication;
    2448    Caption: string;
     49    TitleBar: TFormTitleBar;
    2550    function HandleMessage(Message: TMessage): Boolean; override;
    2651    procedure Paint; override;
     52    procedure Close;
     53    constructor Create; override;
     54    destructor Destroy; override;
    2755    property Focused: Boolean read FFocused write SetFocused;
    2856    property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle;
     
    3058
    3159  TPanel = class(TWinControl)
     60  protected
     61    function HandleMessage(Message: TMessage): Boolean; override;
     62  public
    3263    Caption: string;
    3364    procedure Paint; override;
     
    4172    Forms: TList<TForm>;
    4273    MainForm: TForm;
     74    Terminated: Boolean;
    4375    function HandleMessage(Message: TMessage): Boolean; virtual;
    4476    procedure Run; virtual;
     
    6698procedure TApplication.Terminate;
    6799begin
    68 
     100  Terminated := True;
    69101end;
    70102
    71103{ TForm }
     104
     105procedure TForm.Close;
     106begin
     107  if Application.MainForm = Self then
     108    Application.Terminate;
     109end;
     110
     111constructor TForm.Create;
     112begin
     113  inherited;
     114  TitleBar := TFormTitleBar.Create;
     115end;
     116
     117destructor TForm.Destroy;
     118begin
     119  TitleBar.Free;
     120  inherited;
     121end;
    72122
    73123function TForm.GetVideoDevice: TVideoDevice;
     
    78128
    79129function TForm.HandleMessage(Message: TMessage): Boolean;
    80 var
    81   TitleBarBounds: TRectangle;
    82 begin
    83   Result := False;
    84   if Message is TMessageMouseDown then
    85   with TMessageMouseDown(Message) do begin
    86     TitleBarBounds := TRectangle.Create(0, 0, Bounds.Width, TitleBarHeight);
    87     Focused := True;
    88     if (BorderStyle = bsNormal) and TitleBarBounds.Contains(ScreenToClient(Position)) then begin
    89       Move.StartControlPos := Bounds.TopLeft;
    90       Move.StartMousePos := Position;
    91       Move.Active := True;
    92       Result := True;
    93     end;
    94   end else
    95   if Message is TMessageMouseUp then
    96   with TMessageMouseUp(Message) do begin
    97     Move.Active := False;
    98   end else
    99   if Message is TMessageMouseMove then
    100   with TMessageMouseUp(Message) do begin
    101     if Move.Active then begin
    102       Bounds.TopLeft := Move.StartControlPos + (Position - Move.StartMousePos);
    103       TScreen(Screen).Paint;
    104     end;
    105   end;
    106   if not Result then inherited;
     130begin
     131  inherited;
    107132end;
    108133
    109134procedure TForm.Paint;
    110135begin
     136  if BorderStyle = bsNormal then begin
     137    TitleBar.Form := Self;
     138    TitleBar.Paint;
     139  end;
    111140  inherited;
    112141  with Canvas do begin
    113142    if BorderStyle = bsNormal then begin
    114       if Focused then Brush.Color := clLightBlue else
    115         Brush.Color := clSilver;
    116       FillRect(TRectangle.Create(0, 0, Bounds.Width - 1, TitleBarHeight));
    117143      MoveTo(TPoint.Create(0, 0));
    118144      LineTo(TPoint.Create(Bounds.Width - 1, 0));
     
    120146      LineTo(TPoint.Create(0, Bounds.Height - 1));
    121147      LineTo(TPoint.Create(0, 0));
    122       MoveTo(TPoint.Create(0, TitleBarHeight));
    123       LineTo(TPoint.Create(Bounds.Width - 1, TitleBarHeight));
    124       TextOut(TPoint.Create((Bounds.Width - GetTextSize(Caption).X) div 2,
    125         (TitleBarHeight - GetTextSize(Caption).Y) div 2), Caption);
    126148    end;
    127149  end;
     
    142164
    143165{ TPanel }
     166
     167function TPanel.HandleMessage(Message: TMessage): Boolean;
     168begin
     169  inherited;
     170end;
    144171
    145172procedure TPanel.Paint;
     
    152179    LineTo(TPoint.Create(0, Bounds.Height - 1));
    153180    LineTo(TPoint.Create(0, 0));
    154     TextOut(TPoint.Create((Bounds.Width - GetTextSize(Caption).X) div 2,
    155       (Bounds.Height - GetTextSize(Caption).Y) div 2), Caption);
    156   end;
     181    if Caption <> '' then
     182      TextOut(TPoint.Create((Bounds.Width - GetTextSize(Caption).X) div 2,
     183        (Bounds.Height - GetTextSize(Caption).Y) div 2), Caption);
     184  end;
     185end;
     186
     187{ TFormTitleBar }
     188
     189constructor TFormTitleBar.Create;
     190begin
     191  inherited;
     192  Panel := TPanel.Create;
     193  Panel.Visible := True;
     194  Panel.OnMouseDown := DoMouseDown;
     195
     196  MainLabel := TLabel.Create;
     197  MainLabel.Parent := Panel;
     198  MainLabel.Visible := True;
     199
     200  CloseButton := TButton.Create;
     201  CloseButton.Parent := Panel;
     202  CloseButton.Caption := 'X';
     203  CloseButton.Visible := True;
     204  CloseButton.OnClick := DoClose;
     205
     206  MaximizeButton := TButton.Create;
     207  MaximizeButton.Parent := Panel;
     208  MaximizeButton.Caption := 'M';
     209  MaximizeButton.Visible := True;
     210  MaximizeButton.OnClick := DoMaximize;
     211
     212  MinimizeButton := TButton.Create;
     213  MinimizeButton.Parent := Panel;
     214  MinimizeButton.Caption := 'V';
     215  MinimizeButton.Visible := True;
     216end;
     217
     218destructor TFormTitleBar.Destroy;
     219begin
     220  MainLabel.Free;
     221  MinimizeButton.Free;
     222  MaximizeButton.Free;
     223  CloseButton.Free;
     224  Panel.Free;
     225  inherited;
     226end;
     227
     228procedure TFormTitleBar.DoClose(Sender: TObject);
     229begin
     230  Form.Close;
     231end;
     232
     233procedure TFormTitleBar.DoMaximize(Sender: TObject);
     234begin
     235  Form.Bounds := TRectangle.Create(0, 0, TScreen(Form.Screen).Size.X, TScreen(Form.Screen).Size.Y);
     236  Form.Paint;
     237end;
     238
     239procedure TFormTitleBar.DoMouseDown(Sender: TObject; Position: TPoint; Buttons: TMouseButtonSet);
     240begin
     241  Form.Focused := True;
     242  if (Form.BorderStyle = bsNormal) then begin
     243    TScreen(Form.Screen).FormMove.StartControlPos := Form.Bounds.TopLeft;
     244    TScreen(Form.Screen).FormMove.StartMousePos := Position;
     245    TScreen(Form.Screen).FormMove.Control := Form;
     246    TScreen(Form.Screen).FormMove.Active := True;
     247  end;
     248end;
     249
     250function TFormTitleBar.GetForm: TForm;
     251begin
     252  Result := TForm(Panel.Parent);
     253end;
     254
     255procedure TFormTitleBar.Paint;
     256begin
     257  if Assigned(Form) then begin
     258    Panel.Bounds := TRectangle.Create(0, 0, Form.Bounds.Width, TitleBarHeight);
     259    CloseButton.Bounds := TRectangle.Create(Panel.Bounds.Width - TitleBarHeight,
     260      2, TitleBarHeight - 4, TitleBarHeight - 4);
     261    MaximizeButton.Bounds := TRectangle.Create(Panel.Bounds.Width - 2 * TitleBarHeight,
     262      2, TitleBarHeight - 4, TitleBarHeight - 4);
     263    MinimizeButton.Bounds := TRectangle.Create(Panel.Bounds.Width - 3 * TitleBarHeight,
     264      2, TitleBarHeight - 4, TitleBarHeight - 4);
     265   if Form.Focused then Panel.Color := clLightBlue else
     266      Panel.Color := clSilver;
     267    MainLabel.Caption := Form.Caption;
     268    MainLabel.Bounds := TRectangle.Create(0, 0, Panel.Bounds.Width, Panel.Bounds.Height);
     269  end;
     270end;
     271
     272procedure TFormTitleBar.SetForm(const Value: TForm);
     273begin
     274  Panel.Parent := Value;
    157275end;
    158276
Note: See TracChangeset for help on using the changeset viewer.