Resizing the Cells
Go Up to Customizing a grid Index
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:
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;
//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) };
//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:
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;
//header file class PACKAGE TSampleCalendar : public TCustomGrid { . . . protected: void __fastcall BoundsChanged(void); };
//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 }