LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > geometry > 比率固定でTPanel, TChartのサイズをScaleBy()で調整する

Last updated at Posted at 2018-10-19
動作環境
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;
}
//---------------------------------------------------------------------------

結果

実行直後 (ウィンドウサイズ変更なし)

2018-10-19_14h53_15.png

サイズ変更後
2018-10-19_14h54_05.png

Fitボタン押下後

2018-10-19_14h54_49.png

備考

Fitボタンを何回も押下すると、微妙にサイズが変わる。
この機能をきちんと使う場合には要調査。

関連

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1