動作環境
C++ Builder XE4
仕様
- TPanel, TChartの配置を比率で固定
- フォームのサイズに基づいて、その比率になるように拡大縮小をする
用途: SVGAから4K UHDまでに対応するUIにて。
実装 v0.1
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
static int s_defaultWidth_form; // ScaleBy()処理に使うため、デフォルトサイズを保持するための変数
static int s_defaultHeight_form; // ScaleBy()処理に使うため、デフォルトサイズを保持するための変数
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_fitClick(TObject *Sender)
{
static float curFactor = 1.0;
float factor_width = (float)this->Width / s_defaultWidth_form;
float factor_height = (float)this->Height / s_defaultHeight_form;
// ScaleBy()でサイズ変更するため、一旦元のサイズに戻す
this->ScaleBy(100/curFactor, 100);
this->Width = s_defaultWidth_form;
this->Height = s_defaultHeight_form;
// 小さい比率に合わせてScaleBy()する
if (factor_width > factor_height) {
curFactor = factor_height;
} else {
curFactor = factor_width;
}
this->ScaleBy(100 * curFactor, 100);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
s_defaultWidth_form = this->Width;
s_defaultHeight_form = this->Height;
}
//---------------------------------------------------------------------------
結果
実行直後 (ウィンドウサイズ変更なし)
Fitボタン押下後
備考
Fitボタンを何回も押下すると、微妙にサイズが変わる。
この機能をきちんと使う場合には要調査。