c++ builder XE4 > Form > 4つのFormをモニタいっぱいに並べる


動作確認
C++ Builder XE4

4つのTFormがあるアプリ。

それを以下のような配置にしたい。

  • Form1: 左上に配置. サイズ H1 x W2
  • Form2: 左中に配置. サイズ H1 x W2
  • Form3: 左下に配置. サイズ H1 x W2
  • Form4: 右側に配置. サイズ H3 x W4
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
#include <algorithm> // for std::max()
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    static const int kNumForms = 4;

    // Form
    int xpos[kNumForms] = {0,0,0,2};
    int xwid[kNumForms] = {2,2,2,4};
    int ypos[kNumForms] = {0,1,2,0};
    int yhei[kNumForms] = {1,1,1,3};

    // Form width and height
    int frmWid = 0;
    int frmHei = 0;

    // 1. Find total width and height
    for(int idx=0; idx<kNumForms; idx++) {
        int wid = xpos[idx] + xwid[idx];
        int hei = ypos[idx] + yhei[idx];
        frmWid = std::max(frmWid, wid);
        frmHei = std::max(frmHei, hei);
    }

    // 2. calculate divisions for (x,y)
    int xdiv = Screen->Monitors[0]->Width / frmWid;
    int ydiv = Screen->Monitors[0]->Height / frmHei;

// for debug
//  xdiv = 1280 / frmWid;
//  ydiv = 768 / frmHei;

    TForm *frmPtrs[] = {Form1, Form2, Form3, Form4};

    for(int idx=0; idx<kNumForms; idx++) {
        frmPtrs[idx]->Left = xpos[idx] * xdiv;
        frmPtrs[idx]->Top  = ypos[idx] * ydiv;
        frmPtrs[idx]->Width = xwid[idx] * xdiv;
        frmPtrs[idx]->Height = yhei[idx] * ydiv;
    }

    // 3. show (Form1 is already shown)
    Form2->Show();
    Form3->Show();
    Form4->Show();
}
//---------------------------------------------------------------------------

v0.2 > Taskbarの高さを考慮

Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
#include "Unit4.h"
#include <algorithm> // for std::max()
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    static const int kNumForms = 4;
    static const int kHeightTaskBar = 40;

    // Form
    int xpos[kNumForms] = {0,0,0,2};
    int xwid[kNumForms] = {2,2,2,4};
    int ypos[kNumForms] = {0,1,2,0};
    int yhei[kNumForms] = {1,1,1,3};

    // Form width and height
    int frmWid = 0;
    int frmHei = 0;

    // 1. Find total width and height
    for(int idx=0; idx<kNumForms; idx++) {
        int wid = xpos[idx] + xwid[idx];
        int hei = ypos[idx] + yhei[idx];
        frmWid = std::max(frmWid, wid);
        frmHei = std::max(frmHei, hei);
    }

    // 2. calculate divisions for (x,y)
    int xdiv = Screen->Monitors[0]->Width / frmWid;
    int ydiv = (Screen->Monitors[0]->Height - kHeightTaskBar) / frmHei;

// for debug
//  xdiv = 1280 / frmWid;
//  ydiv = 768 / frmHei;

    TForm *frmPtrs[] = {Form1, Form2, Form3, Form4};

    for(int idx=0; idx<kNumForms; idx++) {
        frmPtrs[idx]->Left = xpos[idx] * xdiv;
        frmPtrs[idx]->Top  = ypos[idx] * ydiv;
        frmPtrs[idx]->Width = xwid[idx] * xdiv;
        frmPtrs[idx]->Height = yhei[idx] * ydiv;
    }

    // 3. show (Form1 is already shown)
    Form2->Show();
    Form3->Show();
    Form4->Show();
}
//---------------------------------------------------------------------------