LoginSignup
0
0

More than 5 years have passed since last update.

c++ builder XE4, 10.2 Tokyo > form > フォームサイズ自動変更

Last updated at Posted at 2015-10-08
動作確認
C++ Builder XE4 on Windows 7pro
Rad Studio 10.2 Tokyo Update 2 on Windows 10 Pro(追記: 2017/12/26)

ノートPC (1367 x 768)向けに作っていたソフトを大画面モニタで使用するため、フォームサイズ自動変更する。

try1

以下の関数を FormShow()にてコールしている。

void __fastcall TForm1::Screen_autoSizeSetting()
{
    // 基準となるノートPCの解像度
    int noteW = 1367;
    int noteH = 768;

    int myW = Screen->Monitors[0]->Width;
    int myH = Screen->Monitors[0]->Height;

    if (myW <= noteW && myH <= noteH) {
        return;
    }

    float ratioH = (float)myH / (float)noteH;
    float ratioW = (float)myW / (float)noteW;

    int setRatio;
    if (ratioH < ratioW) {
        setRatio = ratioH * 100; // 少数第二位まで扱う
    } else {
        setRatio = ratioW * 100;
    }

//  String msg = IntToStr(setRatio);
//  OutputDebugString(msg.c_str());

    this->ScaleBy(setRatio, 100);

}

try2 (複数フォーム対応版)

任意のフォームに適用するバージョン。

void __fastcall TFormMain_graph::XXX_autoSizeSetting(TForm *formsPtr[], int num)
{
    // 基準となるノートPCの解像度
    int noteW = 1367;
    int noteH = 768;

    int myW = Screen->Monitors[0]->Width;
    int myH = Screen->Monitors[0]->Height;

    float ratioH = (float)myH / (float)noteH;
    float ratioW = (float)myW / (float)noteW;

    int setRatio;
    if (ratioH < ratioW) {
        setRatio = ratioH * 100; // 少数第二位まで扱う
    } else {
        setRatio = ratioW * 100;
    }

    for(int idx=0; idx < num; idx++) {
        formsPtr[idx]->ScaleBy(setRatio, 100);
    }

}

以下のような使い方にする

void __fastcall TFormMain_graph::Screen_autoSizeSetting()
{

    TForm *forms[] = {
        this,
        Form2,
        FormXXX,
    };
    int numForms = sizeof(forms) / sizeof(TForm *);

    XXX_autoSizeSetting(forms, numForms);

    return;
}

内部処理を共通化することで、変更時の対応を楽にする。

注意点

(追記 2017/12/26)

Monitors[]使用時は、下記の点に注意するとよい。

C++ Builder XE4, 10.2 Tokyo > モニタ解像度 > Bug > Screen->Monitors[0]のWidthとHeightがおかしくなる

0
0
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
0