1
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 3 years have passed since last update.

C++BuilderでTSpinEditを右詰め(中央)に表示出来るようにコンポーネント作成

Last updated at Posted at 2022-01-18

はじめに

C++Builder(XE以降?)には、エディットボックスとスピンボタンを一体化した、TSpinEditが「Sample」パレットにあります。
spin.png

スピンボタンで数値を上下出来るので便利なのですが、左寄せでしか数値の表示が出来ません。TSpinEditを継承した、自作のコンポーネントを作ります。

コンポーネント作成

エディットボックスに右詰め(中央揃え)を実現するには、Windows APIのgetwindowlongptr() (※旧 getwindowlong)と、setwindowlongptr() (※旧 setwindowlong)を使用する方法がありますが、そのようなAPIを使わずとも、もっと簡単に実現できます。

(1)コンポーネント作成
 下図の様にTSpinEditを継承したコンポーネント作成をします。
 クラス名、パレット名などは任意に設定してください。

spin2.png

(2)コンポーネントユニットにヘッダーファイルにAlignment

下のソースコードの様にAlignmentを追加します。

nsSpinEdit.h
//---------------------------------------------------------------------------

# ifndef nsSpinEditH
# define nsSpinEditH
//---------------------------------------------------------------------------
# include <System.SysUtils.hpp>
# include <System.Classes.hpp>
# include <Vcl.Controls.hpp>
# include <Vcl.Samples.Spin.hpp>
# include <Vcl.StdCtrls.hpp>
//---------------------------------------------------------------------------
class PACKAGE TnsSpinEdit : public TSpinEdit
{
private:
protected:
public:
	__fastcall TnsSpinEdit(TComponent* Owner);
__published:
	//↓この1行を追加する↓
	__property Alignment;
};
//---------------------------------------------------------------------------
# endif

(3)出来たコンポーネントユニットを自作のパッケージに追加してメイク
 ユニットをパッケージに追加してメイクします。
 また、「インストール」必要な場合があります。

spin3.png

これで、パレットが更新され、新しく作成したTSpinEditの改良版で「Alignment」プロパティが使えて、右詰め(中央揃え)が可能になります。

spin4.png

終わりに

長々と色々書きましたが、結局の所TSpinEditの親のクラスのTCustomEditのAlignmentがpublicスコープになっていて、プロパティとしてTSpinEditでは見えていない状態だったのを、__publishedに変更して見えるようにしただけなんです。
クラスの継承とかよく見ると、楽に機能強化とかできる場合があると言う事です。

1
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
1
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?