Changeset 8 for os/trunk/Xvcl


Ignore:
Timestamp:
Jun 2, 2013, 10:46:40 PM (12 years ago)
Author:
chronos
Message:
  • Added: Implemented simple TEdit control which react to key press messages.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • os/trunk/Xvcl/Xvcl.Controls.pas

    r5 r8  
    2929  TKeyState = (ksShift, ksAlt, ksOS);
    3030  TKeyStateSet = set of TKeyState;
    31   TMessageKeyboard = class(TMessage)
     31  TMessageKeyPress = class(TMessage)
    3232    KeyCode: Integer;
    3333    States: TKeyStateSet;
     
    5959    FOnMouseDown: TNotifyEvent;
    6060    FOnMouseUp: TNotifyEvent;
     61    FOnKeyPress: TNotifyEvent;
    6162    function GetCanvas: TCanvas;
    6263    procedure SetParent(const Value: TWinControl); virtual;
     
    8182    property OnMouseDown: TNotifyEvent read FOnMouseDown write FOnMouseDown;
    8283    property OnMouseUp: TNotifyEvent read FOnMouseUp write FOnMouseUp;
     84    property OnKeyPress: TNotifyEvent read FOnKeyPress write FOnKeyPress;
    8385  end;
    8486
     
    9799    FCaption: string;
    98100    procedure SetCaption(const Value: string);
     101  protected
     102    function HandleMessage(Message: TMessage): Boolean; override;
    99103  public
    100104    procedure Paint; override;
     
    112116  end;
    113117
     118  TEdit = class(TControl)
     119  private
     120    FText: string;
     121    procedure SetText(const Value: string);
     122  protected
     123    function HandleMessage(Message: TMessage): Boolean; override;
     124  public
     125    procedure Paint; override;
     126    constructor Create; override;
     127    property Text: string read FText write SetText;
     128  end;
     129
    114130
    115131implementation
     
    153169
    154170function TControl.HandleMessage(Message: TMessage): Boolean;
     171begin
     172  Result := False;
     173end;
     174
     175procedure TControl.Paint;
     176begin
     177  with Canvas do begin
     178    Brush.Color := Color;
     179    FillRect(TRectangle.Create(0, 0, Bounds.Width, Bounds.Height));
     180  end;
     181end;
     182
     183function TControl.ScreenToClient(Position: TPoint): TPoint;
     184begin
     185  Result := Position.Substract(Bounds.TopLeft);
     186  if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
     187end;
     188
     189procedure TControl.SetColor(const Value: TColor);
     190begin
     191  if FColor <> Value then begin
     192    FColor := Value;
     193    if Visible then Paint;
     194  end;
     195end;
     196
     197procedure TControl.SetParent(const Value: TWinControl);
     198begin
     199  if FParent <> Value then begin
     200    if Assigned(FParent) then Parent.Controls.Remove(Self);
     201    FParent := Value;
     202    if Assigned(FParent) then Parent.Controls.Add(Self);
     203  end;
     204end;
     205
     206{ TButton }
     207
     208function TButton.HandleMessage(Message: TMessage): Boolean;
    155209begin
    156210  Result := False;
     
    170224end;
    171225
    172 procedure TControl.Paint;
    173 begin
    174   with Canvas do begin
    175     Brush.Color := Color;
    176     FillRect(TRectangle.Create(0, 0, Bounds.Width, Bounds.Height));
    177   end;
    178 end;
    179 
    180 function TControl.ScreenToClient(Position: TPoint): TPoint;
    181 begin
    182   Result := Position.Substract(Bounds.TopLeft);
    183   if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
    184 end;
    185 
    186 procedure TControl.SetColor(const Value: TColor);
    187 begin
    188   if FColor <> Value then begin
    189     FColor := Value;
    190     if Visible then Paint;
    191   end;
    192 end;
    193 
    194 procedure TControl.SetParent(const Value: TWinControl);
    195 begin
    196   if FParent <> Value then begin
    197     if Assigned(FParent) then Parent.Controls.Remove(Self);
    198     FParent := Value;
    199     if Assigned(FParent) then Parent.Controls.Add(Self);
    200   end;
    201 end;
    202 
    203 { TButton }
    204 
    205226constructor TButton.Create;
    206227begin
     
    268289    if Message is TMessageMouse then
    269290    with TMessageMouse(Message) do begin
    270     if Control.Bounds.Contains(ScreenToClient(Position)) then begin
     291      if Control.Bounds.Contains(ScreenToClient(Position)) then begin
     292        if TWinControl(Control).HandleMessage(Message) then begin
     293          Result := True;
     294          Break;
     295        end;
     296      end;
     297    end else
     298    if Message is TMessageKeyPress then
    271299      if TWinControl(Control).HandleMessage(Message) then begin
    272300        Result := True;
    273301        Break;
    274302      end;
    275     end;
    276   end;
    277303  end;
    278304end;
     
    305331end;
    306332
     333{ TEdit }
     334
     335constructor TEdit.Create;
     336begin
     337  inherited;
     338  FColor := clWhite;
     339end;
     340
     341function TEdit.HandleMessage(Message: TMessage): Boolean;
     342begin
     343  Result := False;
     344  if Message is TMessageKeyPress then
     345  with TMessageKeyPress(Message) do begin
     346    if Assigned(FOnKeyPress) then FOnKeyPress(Self);
     347    if KeyCode = 8 then Text := Copy(Text, 1, Length(Text) - 1)
     348      else Text := Text + Chr(TMessageKeyPress(Message).KeyCode);
     349    Paint;
     350    Result := True;
     351  end else
     352end;
     353
     354procedure TEdit.Paint;
     355var
     356  TextShort: string;
     357begin
     358  inherited;
     359  with Canvas do begin
     360    MoveTo(TPoint.Create(0, 0));
     361    LineTo(TPoint.Create(Bounds.Width - 1, 0));
     362    LineTo(TPoint.Create(Bounds.Width - 1, Bounds.Height - 1));
     363    LineTo(TPoint.Create(0, Bounds.Height - 1));
     364    LineTo(TPoint.Create(0, 0));
     365    TextShort := Text;
     366    while (GetTextSize(TextShort).X > Bounds.Width) and
     367      (Length(TextShort) > 0) do
     368      Delete(TextShort, 1, 1);
     369
     370    TextOut(TPoint.Create(3,
     371      (Bounds.Height - GetTextSize(TextShort).Y) div 2), TextShort);
     372  end;
     373end;
     374
     375procedure TEdit.SetText(const Value: string);
     376begin
     377  if FText <> Value then begin
     378    FText := Value;
     379    if Visible then Paint;
     380  end;
     381end;
     382
    307383end.
Note: See TracChangeset for help on using the changeset viewer.