TGroupBox から派生したコンポーネントを作成します。
この中にボタンを配置し、ボタンを押せるようにする
コンポーネントを作成したいのですが、
このコンポーネントをフォームに貼り付け、
IDE上でこのボタンを移動したり、サイズを変更
できるようにするにはどうすればよいのでしょうか?
付属している
コンポーネント開発ガイドくらい 読みましょう
付属の開発者ガイド を一読する
+ 付属ヘルプで ComponentState を読む
以上。
> この中にボタンを配置し、ボタンを押せるようにする
> コンポーネントを作成したいのですが、
これだけでは、「TGroupBoxを置いて、その上TButtonを置けば、
サイズ変更も好きなようにできます。」なんですけど?
TGroupBox自体、パネルの上にボタンとか配置できますが、
そういうことではないのですか?
それとも、TGroupBoxの中にボタンが配置済みのコンポーネントで、
その中のボタンを設計時に移動とかサイズ変更したい、ということですか?
後者っぽいですけど・・・。
>それとも、TGroupBoxの中にボタンが配置済みのコンポーネントで、
>その中のボタンを設計時に移動とかサイズ変更したい、ということですか?
すみません。
そうです。
TGroupBoxから派生したクラスTGroupBoxEXを作成し、
そのクラスの中にボタンを配置します。
ボタンのプロパティのTopとかLeftをTGroupBoxEXで
ButtonTop, ButtonLeft として数値入力で表示位置を変更
するところまではできるのですが、
コンポーネントを貼り付けている状態で、
個別にこれらのボタンを移動する方法がわからないのです。
「コンポーネント開発ガイド」見ていますが、
関連する部分すらまだ、見つけていない状態です。
難しいと思います。
設計時に移動・サイズ変更するということは、切り取り(Ctrl+X)することも可能になり、
それを防止する必要もあるでしょう。
コンポーネント化ではなく、TFrameで設計し代用するのはどうでしょう?
Frameは設計がフォームのように行うことができますし、Frameを貼り付けした
フォーム上から、コンポーネントの移動・サイズ変更も可能。
また、Frame上のコンポーネントは削除不可
という、だいたい希望通りの動作になるかと思いますが。
ただ、TFrameが、どのバージョンから使えるようになったかは、知りません(^^ゞ
ちょっと面白そう(?)だったのでやってみました.
ご希望の仕様かどうか,また実用性があるのかは不明です.
MyButtonPanel.pasで保存し,MyButtonPanelをコンポーネントとして登録
して下さい.
ただし,Delphi6以上専用です.質問された方はDelphiのバージョンが
書かれていないので,Delphi5以前であっても文句はなしということで...
unit MyButtonPanel;
//====================================================================
// サブコンポーネント
// TPanel派生のTMyButtonPanelの上にButtonが配置されたもの
// TPanelに設計時にTButtonを配置する場合との違いは,TMyButtonPanelの
// オブジェクトインスペクタにButtonというプロパティがあり,そのプロ
// パティ(イベントも含む)が設定可能なこと
// Delphi6以降専用
//====================================================================
interface
uses
SysUtils, Classes, Controls, ExtCtrls,StdCtrls,Forms;
type
TMyInternalButton = class;
TMyButtonPanel = class(TPanel)
private
{ Private 宣言 }
protected
{ Protected 宣言 }
FButton : TMyInternalButton;
procedure Notification(AComponent: TComponent; Operation: TOperation);override;
procedure SetParent(AParent: TWinControl); override;
public
{ Public 宣言 }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published 宣言 }
property Button : TMyInternalButton read FButton;
end;
TMyInternalButton = class(TButton)
private
{ Private 宣言 }
protected
{ Protected 宣言 }
public
{ Public 宣言 }
constructor Create(AOwner: TComponent); override;
published
{ Published 宣言 }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples2', [TMyButtonPanel]);
end;
{ TMyButtonPanel }
//====================================================================
// Button(サブコンポーネント)を乗せるPanelの生成
//====================================================================
constructor TMyButtonPanel.Create(AOwner: TComponent);
begin
inherited;
FButton:=TMyInternalButton.Create(AOwner);
FButton.Parent:=Self;
end;
//====================================================================
// Button(サブコンポーネント)を乗せるPanelの破棄
//====================================================================
destructor TMyButtonPanel.Destroy;
begin
FButton.Free;
inherited;
end;
//====================================================================
// Button(サブコンポーネント)が削除された場合を考慮
//====================================================================
procedure TMyButtonPanel.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if csDestroying in ComponentState then exit;
if (Operation=opRemove) then begin
if (AComponent=TComponent(FButton)) then begin
FButton:=nil;
end;
end;
end;
//====================================================================
// Button(サブコンポーネント)に名前を付ける
// 名前がないとイベント等が作成できない
//====================================================================
procedure TMyButtonPanel.SetParent(AParent: TWinControl);
var
AForm : TCustomForm;
begin
inherited;
if Parent=nil then exit;
if csDesigning in ComponentState then begin
AForm:=GetParentForm(Self);
FButton.Name:=AForm.Designer.UniqueName('Button');
end;
end;
{ TMyInternalButton }
//====================================================================
// Button(サブコンポーネントでTMybuttonPanelのプロパティ)の生成
// SetSubComponentでサブコンポーネントとして設定
//====================================================================
constructor TMyInternalButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetSubComponent(True);
end;
end.
Mr.XRAY さん、お疲れ様です。
横やり失礼します。
自分は、D5しかないので、D6以降と聞いて、試す気はまったくありません(^^ゞ
TMyButtonPanel の 読み取り専用プロパティ Button は、オブジェクトインスペクタに出すためのもの?
それと、フォームに貼り付けて保存し、閉じてから開き直し(テキスト表示して戻すのも可)
で、エラーなく表示できます?
SetSubComponent(True);
これが、くせものなんだろうか?
どうもD5にはみつからないような感じなんですが。
D5で同じようにやってみたら、SetSubComponentがないせいか、エラーになるんですよ。
スミマセン.先程のコードですが,保存したプロジェクトを開いたら
TMyInternalButtonがありません.のエラーで正常動作しませんでした.
残念!! これはやはりFrameがいいのかも知れません.
ツイート | ![]() |