動機:
對於Delphi6的TEdit文字方塊,
若要用來顯示數字時,
又剛好使用者要求要將數字靠右,
這時按下F11進入Object Inspector卻找不到Alignment屬性時,
要如何解決呢?!
說明:
利用Delphi6物件導向的特性,
再新增相關的class,
就可以讓你使用Alignment來靠右了!!
程式碼如下:
uses StdCtrls,...; ...... { 加 Alignment property } type TEdit = class(StdCtrls.TEdit) private FAlignment : TAlignment; procedure SetAlignment(Value: TAlignment); protected procedure CreateParams(var Params: TCreateParams); override; public property Alignment: TAlignment read FAlignment write SetAlignment; end; ...... {$R *.dfm} { 加 Alignment Property -- Begin } procedure TEdit.CreateParams(var Params: TCreateParams); const Alignments: array[TAlignment] of LongWord = (ES_Left, ES_Right, ES_Center); begin inherited CreateParams(Params); Params.Style := Params.Style or Alignments[FAlignment]; end; procedure TEdit.SetAlignment(Value: TAlignment); begin if FAlignment <> Value then begin FAlignment := Value; RecreateWnd; end; end; { 加 Alignment Property -- End }
使用方式很簡單...
只要宣告屬性 edName.Alignment := taRightJustify; 即可!!
另外,
若是TMaskEdit可不可以使用呢?!
答案是可以的!!
程式碼如下:
uses Mask,...; ...... { 加 Alignment property } type TMaskEdit = class(Mask.TMaskEdit) private FAlignment : TAlignment; procedure SetAlignment(Value: TAlignment); protected procedure CreateParams(var Params: TCreateParams); override; public property Alignment: TAlignment read FAlignment write SetAlignment; end; ...... {$R *.dfm} { 加 Alignment Property -- Begin } procedure TMaskEdit.CreateParams(var Params: TCreateParams); const Alignments: array[TAlignment] of LongWord = (ES_Left, ES_Right, ES_Center); begin inherited CreateParams(Params); Params.Style := Params.Style or Alignments[FAlignment]; end; procedure TMaskEdit.SetAlignment(Value: TAlignment); begin if FAlignment <> Value then begin FAlignment := Value; RecreateWnd; end; end; { 加 Alignment Property -- End }
留言