Ignore:
Timestamp:
May 6, 2013, 12:16:30 AM (12 years ago)
Author:
chronos
Message:
  • Modified: Improved messages passing to controls.
  • Modified: Now TButton change background color during mouse click.
Location:
branches/Xvcl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/Xvcl

    • Property svn:ignore
      •  

        old new  
        22*.local
        33Win32
         4__history
  • branches/Xvcl/Xvcl.Controls.pas

    r19 r20  
    99  TControl = class;
    1010  TWinControl = class;
     11
     12  TMessage = class
     13    Number: Integer;
     14    Handled: Boolean;
     15  end;
     16
     17  TMouseButton = (mbLeft, mbRight, mbMiddle);
     18  TMouseButtonSet = set of TMouseButton;
     19  TMessageMouse = class(TMessage)
     20    Position: TPoint;
     21    Buttons: TMouseButtonSet;
     22  end;
     23  TMessageMouseDown = class(TMessageMouse);
     24  TMessageMouseUp = class(TMessageMouse);
     25
     26  TKeyState = (ksShift, ksAlt, ksOS);
     27  TKeyStateSet = set of TKeyState;
     28  TMessageKeyboard = class(TMessage)
     29    KeyCode: Integer;
     30    States: TKeyStateSet;
     31  end;
    1132
    1233  TControlCanvas = class(TCanvas)
     
    2546    FBounds: TRectangle;
    2647    FOnClick: TNotifyEvent;
     48    FColor: TColor;
     49    FOnMouseDown: TNotifyEvent;
     50    FOnMouseUp: TNotifyEvent;
    2751    function GetCanvas: TCanvas;
    2852    procedure SetParent(const Value: TWinControl); virtual;
     53    procedure SetColor(const Value: TColor);
    2954  protected
    3055    function GetVideoDevice: TVideoDevice; virtual;
    3156  public
    3257    Controls: TList<TControl>;
     58    function HandleMessage(Message: TMessage): Boolean; virtual;
    3359    function ClientToScreen(Position: TPoint): TPoint; virtual;
     60    function ScreenToClient(Position: TPoint): TPoint; virtual;
    3461    procedure Paint; virtual;
    3562    constructor Create; override;
     
    4067    property Parent: TWinControl read FParent write SetParent;
    4168    property VideoDevice: TVideoDevice read GetVideoDevice;
     69    property Color: TColor read FColor write SetColor;
    4270    property OnClick: TNotifyEvent read FOnClick write FOnClick;
     71    property OnMouseDown: TNotifyEvent read FOnMouseDown write FOnMouseDown;
     72    property OnMouseUp: TNotifyEvent read FOnMouseUp write FOnMouseUp;
    4373  end;
    4474
    4575  TWinControl = class(TControl)
     76  protected
     77  public
    4678    Controls: TList<TControl>;
    47     function HandleClick(Position: TPoint): Boolean;
     79    function HandleMessage(Message: TMessage): Boolean; override;
    4880    constructor Create; override;
    4981    destructor Destroy; override;
     
    5688  public
    5789    procedure Paint; override;
     90    constructor Create; override;
    5891    property Caption: string read FCaption write SetCaption;
    5992  end;
     
    75108function TControl.ClientToScreen(Position: TPoint): TPoint;
    76109begin
    77   Result := TPoint.Create(Bounds.Left + Position.X, Bounds.Top + Position.Y);
     110  Result := Position.Add(Bounds.TopLeft);
    78111  if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
    79112end;
     
    107140end;
    108141
     142function TControl.HandleMessage(Message: TMessage): Boolean;
     143begin
     144  if Message is TMessageMouseDown then begin
     145    if Assigned(FOnMouseDown) then FOnMouseDown(Self);
     146    Color := clGray;
     147    Paint;
     148  end else
     149  if Message is TMessageMouseUp then begin
     150    if Assigned(FOnMouseUp) then FOnMouseUp(Self);
     151    Color := clSilver;
     152    Paint;
     153    if Assigned(FOnClick) then FOnClick(Self);
     154  end;
     155end;
     156
    109157procedure TControl.Paint;
    110158var
     
    112160begin
    113161  for C in Controls do C.Paint;
     162end;
     163
     164function TControl.ScreenToClient(Position: TPoint): TPoint;
     165begin
     166  Result := Position.Substract(Bounds.TopLeft);
     167  if Assigned(Parent) then Result := Parent.ClientToScreen(Result);
     168end;
     169
     170procedure TControl.SetColor(const Value: TColor);
     171begin
     172  if FColor <> Value then begin
     173    FColor := Value;
     174    Paint;
     175  end;
    114176end;
    115177
     
    125187{ TButton }
    126188
     189constructor TButton.Create;
     190begin
     191  inherited;
     192  FColor := clSilver;
     193end;
     194
    127195procedure TButton.Paint;
    128196begin
    129197  inherited;
    130198  with Canvas do begin
     199    Brush.Color := Color;
     200    FillRect(TRectangle.Create(0, 0, Bounds.Width, Bounds.Height));
    131201    MoveTo(TPoint.Create(0, 0));
    132202    LineTo(TPoint.Create(Bounds.Width - 1, 0));
     
    176246end;
    177247
    178 function TWinControl.HandleClick(Position: TPoint): Boolean;
     248function TWinControl.HandleMessage(Message: TMessage): Boolean;
    179249var
    180250  Control: TControl;
     
    182252  Result := False;
    183253  for Control in Controls do begin
    184     if Control.Bounds.Contains(Position) then begin
    185       if Control is TWinControl then begin
    186         if TWinControl(Control).HandleClick(Position.Substract(Bounds.LeftTop)) then begin
    187           Result := True;
    188           Break;
    189         end;
    190       end else
    191       if Assigned(Control.OnClick) then begin
    192         Control.OnClick(Self);
     254    if Message is TMessageMouse then
     255    with TMessageMouse(Message) do begin
     256    if Control.Bounds.Contains(ScreenToClient(Position)) then begin
     257      if TWinControl(Control).HandleMessage(Message) then begin
    193258        Result := True;
    194259        Break;
     
    196261    end;
    197262  end;
     263  end;
    198264end;
    199265
Note: See TracChangeset for help on using the changeset viewer.