0
0

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, 10.2 Tokyo > TLabel > Caption文字列が指定の枠を超えてしまうかのチェック

Last updated at Posted at 2016-11-16
動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/28)

TLabelのCaptionに任意の文字列を代入するとき、その文字列が指定の枠(TLabelのwidth)を超えてしまうかどうかの確認。

該当のTLabelはAutoSizeプロパティをfalseとしている、という前提。

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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE で管理されるコンポーネント
	TLabel *Label1;
	TButton *Button1;
	TLabel *Label2;
	void __fastcall Button1Click(TObject *Sender);
private:	// ユーザー宣言
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::Button1Click(TObject *Sender)
{
	//String kDummyString = L"START_jfkldsajkfjdklsajfkldjsklfjaksdljfkldsjalkjfsdk_END";
	String kDummyString = L"AAA";

	int orgWdt = Label1->Width;

	Label1->Caption = kDummyString;

	Label1->AutoSize = true;

	int aftWdt = Label1->Width;

	Label1->AutoSize = false;

	if (aftWdt > orgWdt) {
		OutputDebugString(L"too large font size");
	} else {
		OutputDebugString(L"font size is appropriate");
	}

	int nop=1; // for breakpoint
}
//---------------------------------------------------------------------------

メッセージ欄に以下が出ていれば、枠を超えてしまっている。

デバッグ出力: too large font size プロセス Project1.exe (1156)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?