Problem - komponent sam se ustala wysokość

0

Cześć, dziwniejszego problemu chyba nie miałem nigdy. Już drugą godzinę nad tym siedzę i zaraz popełnię samobójstwo.

Napisałem sobie bardzo prosty komponencik dziedziczący po TGraphicControl. Na tym etapie ma właściwie tylko wyglądać - rysuje małe kółko i dołączoną do niego krótką kreskę. I niby wszystko działa.
Komponent ma zakodowaną domyślną wysokość na 5.
Jednak jak kładę go na formę, to wysokość mu się ustawia na 41(!).
Mało tego. Jeśli go tworzę dynamicznie, to wszystko jest w porządku! Gdzie tu sens, gdzie logika? O co tu chodzi?

W konstruktorze mam wpisanie wyraźnie, że height i width tyle i tyle. Dodatkowo property Height i Width mają wpisane default. Ale on i tak swoje.

Poniżej zamieszczam jego cały kod(metoda Paint jak widać nie jest dokończona).

unit JuhasSocket;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Graphics;

type
  TJSDirection = (jsdLeft, jsdUp, jsdRight, jsdDown);
  TJuhasSocket = class(TGraphicControl)
  private
    FDirection: TJsDirection;
    FColor: TColor;
  protected
    procedure Paint(); override;

    procedure SetDirection(AValue: TJSDirection);
    procedure SetColor(AValue: TColor);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Color: TColor read FColor write SetColor default clBlack;
    property Cursor;
    property Direction: TJSDirection read FDirection write SetDirection default jsdLeft;
    property HelpType;
    property HelpKeyword;
    property HelpContext;
    property Hint;
    property Left;
    property Top;

    property Width default 10;
    property Height default 5;

    property OnClick;
    property OnContextPopup;
    property OnDblClick;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Juhas', [TJuhasSocket]);
end;

{ TJuhasSocket }
constructor TJuhasSocket.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  
  FColor:=clBlack;
  FDirection:=jsdLeft;
  Canvas.Pen.Style:=psSolid;
  Canvas.Pen.Width:=1;
  Canvas.Pen.Color:=clBlack;

  Canvas.Brush.Style:=bsSolid;
  Canvas.Brush.Color:=clBlack;

  Width:=10;
  Height:=5;   
end;

destructor TJuhasSocket.Destroy;
begin

  inherited;
end;


//========= Paint ============\\
procedure TJuhasSocket.Paint;
begin
  inherited;
  case FDirection of
    jsdLeft:
      begin
        canvas.Ellipse(0, 0, Width div 2, Height);
        canvas.MoveTo(Width div 2, Height div 2);
        canvas.LineTo(Width, height div 2);
      end;
  end;    
end;

//=========== Zmiana koloru =====================\\
procedure TJuhasSocket.SetColor(AValue: TColor);
begin
  if FColor = AValue then exit;
  FColor:=AValue;
  Canvas.Pen.Color:=AValue;
  Canvas.Brush.Color:=AValue;
  Invalidate();
end;

//============ Zmiana kierunku ===========================\\
procedure TJuhasSocket.SetDirection(AValue: TJSDirection);
begin
  if FDirection = AValue then exit;
  FDirection:=AValue;
  Invalidate();
end;

end.
0

znasz zasady forum?
najpierw google, pozniej forum.
google: "delphi component default size 41"

0
cimak napisał(a)

znasz zasady forum?
najpierw google, pozniej forum.
google: "delphi component default size 41"

Cimak, googlowałem dość sporo, tylko bardziej pod kątem komponentów dziedziczących po TGraphicControl i ich rozmiarów. Anyway informacja, którą znalazłem niezawiele mi pomogła ;)

Poradziłem sobie jedak z tym w taki sposób:
(przesłonięte SetBounds)

  if csDesigning in ComponentState then
  begin
    if AHeight>0 then FHeightCount:=FHeightCount+1;
    if FHeightCount = 2 then
          AHeight:=5;
  end;

  inherited SetBounds(ALeft, ATop, AWidth, AHeight);

FHeightCount to oczywiście integer.

1 użytkowników online, w tym zalogowanych: 0, gości: 1