RAD Studio |  |

Resizing the Cells

Note: When a user or application changes the size of a window or control, Windows sends a message called WM_SIZE to the affected window or control so it can adjust any settings needed to later paint its image in the new size. Your VCL component can respond to that message by altering the size of the cells so they all fit inside the boundaries of the control. To respond to the WM_SIZE message, you will add a message-handling method to the component.
Creating a message-handling method is described in detail in the section Creating new message handlers.

In this case, the calendar control needs a response to WM_SIZE, so add a protected method called WMSize to the control indexed to the WM_SIZE message, then write the method so that it calculates the proper cell size to allow all cells to be visible in the new size:

Copy Code

    type
      TSampleCalendar = class(TCustomGrid)
      protected
        procedure WMSize(var Message: TWMSize); message WM_SIZE;
      .
      .
      .
      end;
    .
    .
    .
    procedure TSampleCalendar.WMSize(var Message: TWMSize);
    var
      GridLines: Integer;                                         { temporary local variable }
    begin
      GridLines := 6 * GridLineWidth;                 { calculate combined size of all lines }
      DefaultColWidth := (Message.Width - GridLines) div 7;     { set new default cell width }
      DefaultRowHeight := (Message.Height - GridLines) div 7;              { and cell height }
    end;

Copy Code

    //header file
    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    .
    .
    .
    protected:
        void __fastcall WMSize(TWMSize &Message);
    BEGIN_MESSAGE_MAP
      MESSAGE_HANDLER(WM_SIZE, TWMSize, WMSize)
    END_MESSAGE_MAP(TCustomGrid)
    };

Copy Code

    //implementation file
    void __fastcall TSampleCalendar::WMSize(TWMSize &Message)
    {
      int GridLines;                                  // temporary local variable
      GridLines = 6 * GridLineWidth;                  // calculated combined size of all lines
      DefaultColWidth = (Message.Width - GridLines) / 7;    // set new default cell width
      DefaultRowHeight = (Message.Height - GridLines) / 7;  // and cell height
    }

Now when the calendar is resized, it displays all the cells in the largest size that will fit in the control.

In this case, the calendar control needs to override BoundsChanged so that it calculates the proper cell size to allow all cells to be visible in the new size:

Copy Code

    type
      TSampleCalendar = class(TCustomGrid)
      protected
        procedure BoundsChanged; override;
      .
      .
      .
      end;
    .
    .
    .
    procedure TSampleCalendar.BoundsChanged;
    var
      GridLines: Integer;                                         { temporary local variable }
    begin
      GridLines := 6 * GridLineWidth;                 { calculate combined size of all lines }
      DefaultColWidth := (Width - GridLines) div 7;     { set new default cell width }
      DefaultRowHeight := (Height - GridLines) div 7;              { and cell height }
      inherited; {now call the inherited method }
    end;

Copy Code

    //header file
    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    .
    .
    .
    protected:
        void __fastcall BoundsChanged(void);
    };

Copy Code

    //implementation file
    void __fastcall TSampleCalendar::BoundsChanged(void)
    {
      int GridLines;                                  // temporary local variable
      GridLines = 6 * GridLineWidth;                  // calculated combined size of all lines
      DefaultColWidth = (Width - GridLines) / 7;    // set new default cell width
      DefaultRowHeight = (Height - GridLines) / 7;  // and cell height
      TCustomGrid::BoundsChanged(); // now call the inherited method
    }

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!