Помогите, кто может!
Ниже приведен код, позволяющий ограничивать количество символов в компоненте Edit. У меня Edit размером 37х37 Размер шрифта 20. Так что входит только один символ (
Что мне и НУЖНО).
Теперь Вопрос!
Как мне спозиционировать вводимый символ строго по центру компонента?
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// Перхватываем событие KeyPress TEdit'а и измеряем ширину уже введенного текста
// и ширину нового символа. Если ширина больше чем клиентская область TEdit'а,
// новый символ отбрасывается.
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
Rect : TRect;
bm : TBitmap;
begin
if ( ( Ord( Key ) <> VK_TAB ) and ( Ord( Key ) <> VK_RETURN ) and
( Ord( Key ) <> VK_LEFT ) and ( Ord( Key ) <> VK_BACK ) ) then
begin
Windows.GetClientRect( Edit1.Handle, Rect );
bm := TBitmap.Create;
bm.Width := Rect.Right;
bm.Height := Rect.Bottom;
bm.Canvas.Font := Edit1.Font;
if bm.Canvas.TextWidth( Edit1.Text + Key + ' ' ) > Rect.Right then
Key := #0;
bm.Free;
end;
end;
end.
Может Кто знает. ПОМОГИТЕ ПОЖАЛУЙСТА!!!
Я только начинаю изучать Delphi.