C++ Builder

La Isla Bonita



Maximize Form to a given Size
Maximize Form to a given Size


Description

This article shows how to maximize a Form to a given size for a given screen resolution.

For example, given a screen resoution of 800x600, we wish to maximize the form (via the Maximize button on the Form's title bar) to the the size of 400x600.

When we press the TButton, the maximized dimension of the form will change to 700x500, thus, the next time the form is maximized, its size will change accordingly.

MaximizeFormToAGivenSize.cpp

#include <vcl.h>
#pragma hdrstop

#include "MaximizeFormToAGivenSize.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TButton* Button1;

int Top_Maximized    = 100;
int Left_Maximized   = 100;
int Width_Maximised  = 600;
int Height_Maximised = 400;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    Width    = 500;
    Height   = 300;
    Color    = (TColor)0xb1ee1;
    Position = poScreenCenter;

    Button1 = new TButton(this);
    Button1->Parent  = this;
    Button1->Top     = Height/2;
    Button1->Width   = 250;
    Button1->Left    = (Width - Button1->Width)/2;
    Button1->Height  = 50;
    Button1->Caption = "Change Maximized Size";
    Button1->OnClick = Button1Click;
    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::MaximizeForm(int top, int left, int width, int height)
{
    //---------------------------------------------------------
    // Maximize to 600x400 when the maximize button is pressed
    // on the Form's title bar
    //---------------------------------------------------------
    Top    = top;
    Left   = left;
    Width  = width;
    Height = height;
}

void __fastcall TForm1::FormResize(TObject *Sender)
{
    MaximizeForm( Top_Maximized, Left_Maximized,
                  Width_Maximised, Height_Maximised );
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   //Change the Form Position for the Form Maximized state
   Top_Maximized  = 50;
   Left_Maximized = 50;

   //Change the Form dimensions for the Form Maximized state
   Width_Maximised  = 700;
   Height_Maximised = 500;
}
//---------------------------------------------------------------------------

MaximizeFormToAGivenSize.h

#ifndef MaximizeFormToAGivenSizeH
#define MaximizeFormToAGivenSizeH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall FormResize(TObject *Sender);
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
        void __fastcall MaximizeForm(int top, int left, int width, int height);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.