3232:Edit Controls that Align Under NT 4
KEYWORDS: Edit, TEdit, Alignment, ES_LEFT, ES_RIGHT, ES_CENTER AREA: Applicati
Creating an edit control that allows alignment under NT4.
OVERVIEW
This document demonstrates how to create a descendant of
TEdit that allows alignment of the text.
WHO SHOULD USE THIS DOCUMENT
Anyone with a basic familiarity with Delphi programming.
Applies to any version of Delphi.
CREATING JUSTAEDIT
One of the features the TMemo surfaces that a TEdit does
not is the ability to justify the text. This functionality
exists via the Alignment property. Great! So why doesn't
a TEdit have this functionality, or perhaps a more pertinent
question is how do we add it?
This property, unfortunately can not be surfaced from an
ancestor, because the functionality does not exist in any
ancestor. So it needs to be implemented via the window style
flags. ES_LEFT, ES_RIGHT, and ES_CENTER specify the text
alingment for edit controls. These flags do not have
any bearing on single line edit controls...unless running NT 4,
Service pack 3, and that is the reason the functionality
does not exist in the native control. This functionality
in now implemented in NT 4.0 but will have no effect in Win
3.1 and Windows 95.
WORKING CODE
unit JusEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,
StdCtrls;
type
TJustaEdit = class(TEdit)
private
{ Private declarations }
fAlignment : TAlignment;
protected
{ Protected declarations }
procedure SetAlignment(Value: TAlignment);
public
{ Public declarations }
procedure createParams(var Params : TCreateParams); override;
published
{ Published declarations }
property Alignment: TAlignment read FAlignment write SetAlignment
default taLeftJustify;
end;
procedure Register;
implementation
procedure TJustaEdit.CreateParams(var Params : TCreateParams);
var
x : Longint;
begin
inherited CreateParams(Params);
case fAlignment of
tarightjustify: x := es_right;
taleftjustify : x := es_left;
tacenter : x := es_center;
end;
params.style := params.style or x;
end;
procedure TJustaEdit.SetAlignment;
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
procedure Register;
begin
RegisterComponents('Samples', [TJustaEdit]);
end;
end.
TI