Советы по Delphi

рамка для текстовых компонентовОдин из


3D- рамка для текстовых компонентовОдин из примеров создания текстового компонента с трехмерной декоративной контурной рамкой (для создания компонента потребовалось около получаса. Он демонстрирует только принцип получения рамки. Я не стал колдовать над свойствами типа ParentFont..., т.к. это заняло бы еще немало времени и места).
unit IDSLabel;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs,
ExtCtrls;
type
TIDSLabel = class(TBevel)private{ Private declarations }FAlignment : TAlignment;FCaption : String;FFont : TFont;FOffset : Byte;
FOnChange : TNotifyEvent;
procedure SetAlignment( taIn : TAlignment );procedure SetCaption( const strIn : String);procedure SetFont( fntNew : TFont );procedure SetOffset( bOffNew : Byte );protected{ Protected declarations }constructor Create( compOwn : TComponent ); override;destructor Destroy; override;procedure Paint; override;public{ Public declarations }published{ Published declarations }property Alignment : TAlignment read FAlignment write SetAlignment default taLeftJustify;property Caption : String read FCaption write SetCaption;property Font : TFont read FFont write SetFont;property Offset : Byte read FOffset write SetOffset;
property OnChange : TNotifyEvent read FOnChange write FOnChange;end;
implementation

constructor
TIDSLabel.Create;
begin
inherited
Create(compOwn);
FFont := TFont.Create;with compOwn as TForm doFFont.Assign(Font);
Offset := 4;Height := 15;end;

destructor TIDSLabel.Destroy;
begin
FFont.Free;
inherited Destroy;end;

procedure TIDSLabel.Paint;
var
wXPos, wYPos : Word;begin

{Рисуем рамку}inherited Paint;
{Назначаем шрифт}Canvas.Font.Assign(Font);
{Вычисляем вертикальную позицию}wYPos := (Height - Canvas.TextHeight(Caption)) div 2;
{Вычисляем горизонтальную позицию}wXPos := Offset;case Alignment oftaRightJustify: wXPos := Width - Canvas.TextWidth(Caption) - Offset;taCenter: wXPos := (Width - Canvas.TextWidth(Caption)) div 2;end;Canvas.Brush := Parent.Brush;Canvas.TextOut(wXPos,wYPos,Caption);
end;

procedure TIDSLabel.SetAlignment;
begin
FAlignment := taIn;Invalidate;end;

procedure TIDSLabel.SetCaption;
beginFCaption := strIn;
if Assigned(FOnChange) thenFOnChange(Self);
Invalidate;end;

procedure TIDSLabel.SetFont;
begin
FFont.Assign(fntNew);Invalidate;end;

procedure TIDSLabel.SetOffset;
begin
FOffset := bOffNew;Invalidate;end;

end.
[000044]
Комбинация TLabel и TEdit
unit Editlbl1;

interface

uses

SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,Forms, Dialogs, stdctrls;
type
TLabelEdit = class(TWinControl)private{ Private declarations }FEdit: TEdit;FLabel: TLabel;function GetLabelCaption: string;procedure SetLabelCaption(LabelCaption: string);function GetEditText: string;procedure SetEditText(EditText: string);protected{ Protected declarations }public{ Public declarations }constructor Create(AOwner: TComponent); override;publishedproperty LabelCaption: string read GetLabelCaption write SetLabelCaption;property EditText: string read GetEditText write SetEditText;property Left;property Top;property Width;property Height;property Text;property Font;{ Можете опубликовать другие, необходимые вам свойства. }{ Published declarations }end;
procedure Register;

implementation

constructor
TLabelEdit.Create(AOwner: TComponent);
begin
inherited
Create(AOwner);
FEdit := TEdit.Create(self);FLabel := TLabel.Create(self);
with FLabel do beginWidth := FEdit.Width;visible := true;Parent := self;Caption := 'LabelEdit';end;
with FEdit do beginTop := FLabel.Height+2;Parent := self;Visible := true;end;
Top := 0;Left := 0;Width := FEdit.Width;Height := FEdit.Height+FLabel.Height;Visible := true;end;

function TLabelEdit.GetLabelCaption: string;
begin
Result := FLabel.Caption;end;

procedure TLabelEdit.SetLabelCaption(LabelCaption: string);
begin
FLabel.Caption := LabelCaption;end;

function TLabelEdit.GetEditText: string;
begin
Result := FEdit.Text;end;

procedure TLabelEdit.SetEditText(EditText: string);
begin
FEdit.Text := EditText;end;

procedure Register;
begin
RegisterComponents('Test', [TLabelEdit]);end;

end.
[000481]

Содержание раздела