0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

c++ builder XE4 > Forms > 8個の子フォームを親フォームの下に4行 x 2列で配置する

Last updated at Posted at 2015-12-14
動作確認
C++ Builder XE4

以下のフォームがあるとする。

  1. 親フォーム (H140 x W1000) : 1個
  2. 子フォーム (H150 x W400) : 8個

整列処理を実行した時に、子フォームを 4行 x 2列にして親フォームの直下に並べたい。

前準備

  1. Form1を作成
  2. Form2を作成 > 「自動生成フォーム」をやめて「使用可能フォーム」にする。

Form2側のコードはなし。

コード

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>

#include "Unit2.h"
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE で管理されるコンポーネント
	TButton *B_createChild;
	TButton *B_matrix;
	void __fastcall B_createChildClick(TObject *Sender);
	void __fastcall B_matrixClick(TObject *Sender);
private:	// ユーザー宣言
	static const int kNumChild = 8;
	TForm2 *m_childForms[kNumChild];
	void __fastcall putChildrenInMatrix();
public:		// ユーザー宣言
	__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{

}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_createChildClick(TObject *Sender)
{
	for(int idx=0; idx < kNumChild; idx++) {
		m_childForms[idx] = new TForm2(this);
		m_childForms[idx]->Show();
	}
}
//---------------------------------------------------------------------------

static void calcLeftTopOfChildren(int parentTop, int parentHeight, int childIdx, int childWidth, int childHeight, int *dstTop, int *dstLeft)
{
	int topSize, leftSize;

	if (childIdx < 4) {
		topSize = childIdx * childHeight;
		leftSize = 0;
	} else {
		topSize = (childIdx - 4) * childHeight;
		leftSize = childWidth;
	}

	int marginTop = parentTop + parentHeight;

	*dstTop = marginTop + topSize;
	*dstLeft = leftSize;
}

void __fastcall TForm1::putChildrenInMatrix()
{
	TForm2 *formPtr;
	int topPos, leftPos;

	for(int chlIdx=0; chlIdx < kNumChild; chlIdx++) {
		formPtr = m_childForms[chlIdx];

		calcLeftTopOfChildren(this->Top, this->Height, chlIdx, formPtr->Width, formPtr->Height, &topPos, &leftPos);
		formPtr->Top = topPos;
		formPtr->Left = leftPos;
	}
}
void __fastcall TForm1::B_matrixClick(TObject *Sender)
{
	putChildrenInMatrix();
}
//---------------------------------------------------------------------------

動作

B_createボタンで子フォームを作成後、B_matrixボタンで子フォームが親フォームの下に整列される。

整列の用語をmatrixとしたのはいまいち。他の名前含めてリファクタリングが必要。

引数が多いので横長のコードになった。省略名や構造体を使えばもう少し見通しはよくなる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?