| フォームのサイズに連動して、StringGridのサイズを自動調整 | BCB Tips! Last modified : 2006/07/13 |
| 1. | メインフォームのプロパティを変更 水平スクロールバーを非表示に --- HorzScrollBar -> Visible を "false" に 垂直スクロールバーを非表示に --- VertScrollBar -> Visible を "false" に |
| 2. | TMainMenu コンポーネントをフォームに貼り付け、メニューデザイナーで適当に追加 |
| 3. | TPanel コンポーネントをフォームに貼り付け、プロパティを変更 説明文をクリア --- Caption をクリア TPanel コンポーネントを上部に --- Align を "alNone" → "alTop" に ベベルをなしに --- BevelOuter を "bvRaised" → "bvNone" に |
| 4. | TBevel コンポーネントを TPanel コンポーネント上に貼り付け、プロパティを変更 TBevel コンポーネントを上部に --- Align を "alNone" → "alTop" に 形状の変更 --- Shape を "bsBox" → "bsTopLine" に 高さの変更 --- Height を "50" → "10" に |
| 5. | TStatusBar コンポーネントをフォームに貼り付け |
| 6. | TScrollBox コンポーネントをフォームに貼り付け、プロパティを変更 残りの領域いっぱいのサイズに --- Align を "alNone" → "alClient" に 背景色を変更 --- Color を "clBtnFace" → "clWindow" |
| 7. | TStringGrid コンポーネントを TScrollBox コンポーネント上に貼り付け、プロパティを変更 外枠を無しに --- BorderStyle を "bsSingle" → "bsNone" に 左側の固定列(灰色部)を消す --- FixedCols を "1" → "0" に 行(セル)の高さを変更 --- DefaultRowHeight を "24" → "15" に スクロールバーを非表示に --- ScrollBar を "ssBoth" → "ssNone" に 列を分ける垂直線を非表示に --- Options -> goVertLine を "false" に 行を分ける水平線を非表示に --- Options -> goHorzLine を "false" に 列のサイズを変更可能に --- Options -> goColSizing を "true" に |
| 8. | TSpeedButton コンポーネントを TPanel コンポーネント上に貼り付け、プロパティを変更 ボタンの傾斜を無しに --- Flat を "true" に 画像の設定 --- Glyph から |
// --- OnCanResize イベントは,コントロールのサイズを変更前に発生する
void __fastcall TMainForm::FormCanResize(TObject *Sender, int &NewWidth,
int &NewHeight, bool &Resize)
{
// --- 水平スクロールバーを非表示に
if(StringGrid1->ColCount == 5){ // 調整用セルが存在している場合
ScrollBox1->HorzScrollBar->Visible = false;
}
// --- 描写するイメージをメモリ内ビットマップしてから表示させる(ちらつき防止)
StringGrid1->DoubleBuffered = true;
}
// --- OnResize イベントは,コントロールのサイズが変更された直後に発生する
void __fastcall TMainForm::FormResize(TObject *Sender)
{
// --- 表の高さをセット
int i, useHeight=0;
for(i=0; i < StringGrid1->RowCount; i++){
useHeight = useHeight + StringGrid1->RowHeights[i] + 1;
}
StringGrid1->Height = useHeight;
// --- 垂直スクロールバーが出現する場合
int sbWidth = 0;
if(ScrollBox1->Height < (StringGrid1->Height + StringGrid1->RowCount - 1)){
sbWidth = 16;
}
// --- 調整用セルのサイズを算出
int useWidth, adjustWidth;
useWidth = StringGrid1->ColWidths[0] + 1
+ StringGrid1->ColWidths[1] + 1
+ StringGrid1->ColWidths[2] + 1
+ StringGrid1->ColWidths[3] + 1;
adjustWidth = MainForm->Width - (useWidth + sbWidth + 12);
// --- 調整用セルをセット
if(adjustWidth <= 0){
StringGrid1->ColCount = 4; // 調整用セルを無しに
}else{
if(StringGrid1->ColCount == 4){
StringGrid1->ColCount = 5; // 調整用セルを復活
}
StringGrid1->ColWidths[4] = adjustWidth;
}
// --- StringGrid の外枠サイズを変更
if(StringGrid1->ColCount == 4){
StringGrid1->Width = useWidth;
}else{
StringGrid1->Width = MainForm->Width - (12 + sbWidth);
}
// --- 水平スクロールバーが出現する場合
// if(ScrollBox1->Width < (StringGrid1->Width + StringGrid1->ColCount - 1)){
// }
// --- 描写するイメージをウィンドウに直接レンダリングさせる
// (DoubleBuffered を有効にしているとメモリ消費量が大きいため、Resize後は無効に)
StringGrid1->DoubleBuffered = false;
// --- 水平スクロールバーを表示に
if(StringGrid1->ColCount == 4){ // 調整用セルが無い場合
ScrollBox1->HorzScrollBar->Visible = true;
}
}