Link to home
Start Free TrialLog in
Avatar of PPaul
PPaul

asked on

Component Bugs...

I've just started to enter the feild of Component Building(using D2)... The following component is has bugs that I can't figer out on how to get rid of..

Bug1) When The component is the Active Component and the from is not the active form it keeps repainting it self... (Flickers)

Bug2)When anothe form/Window is floating over it and then mover it doen't Refresh it self..

I'm using Compoent Create Program...

Need code for were I'm going wrong...

Thanks
PPaul
{ ****************************************************************** }
{                                                                    }
{   VCL component TSpaceEdit                                         }
{                                                                    }
{   Code generated by Component Create for Delphi                    }
{                                                                    }
{   Generated from source file d:\progra~1\borland\delphi~1.0\lib\pgcomps\spacee~1.cd }
{   on 2 Dec 1999 at 20:37                                           }
{                                                                    }
{   A public domain component by Peter Giroux                        }
{                                                                    }
{ ****************************************************************** }

unit SpaceEdit;

interface

uses WinTypes, WinProcs, Messages, SysUtils, Classes, Controls,
     Forms, Graphics, Stdctrls;

type
  TSpaceEdit = class(TCustomControl)
    private
      { Private fields of TSpaceEdit }
        FActive : Boolean;
        FActiveBorderColor : TColor;
        FAutoSize : Boolean;
        FBasicHeight : Integer;
        FCaption : String;
        FCaptionBGColor : TColor;
        FDivLine : Integer;
        FEditColor : TColor;
        FInactiveBorderColor : TColor;
        FText : String;
        FTextBGColor : TColor;
        SEdit : Tedit;

      { Private methods of TSpaceEdit }
        { Method to set variable and property values and create objects }
        procedure AutoInitialize;
        { Method to free any objects created by AutoInitialize }
        procedure AutoDestroy;
        function GetActive : Boolean;
        procedure SetActive(Value : Boolean);
        function GetActiveBorderColor : TColor;
        procedure SetActiveBorderColor(Value : TColor);
        function GetAutoSize : Boolean;
        procedure SetAutoSize(Value : Boolean);
        function GetCaption : String;
        procedure SetCaption(Value : String);
        function GetCaptionBGColor : TColor;
        procedure SetCaptionBGColor(Value : TColor);
        function GetDivLine : Integer;
        procedure SetDivLine(Value : Integer);
        function GetEditColor : TColor;
        procedure SetEditColor(Value : TColor);
        function GetInactiveBorderColor : TColor;
        procedure SetInactiveBorderColor(Value : TColor);
        function GetText : String;
        procedure SetText(Value : String);
        function GetTextBGColor : TColor;
        procedure SetTextBGColor(Value : TColor);
        procedure DrawBorder (Colour:Tcolor; Active:Boolean);
        procedure Paint; override;

    protected
      { Protected fields of TSpaceEdit }
        property Active : Boolean read GetActive write SetActive default False;

      { Protected methods of TSpaceEdit }
        procedure DoExit; override;
        procedure KeyPress(var Key : Char); override;
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure Loaded; override;

    public
      { Public fields and properties of TSpaceEdit }

      { Public methods of TSpaceEdit }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;

    published
      { Published properties of TSpaceEdit }
        property OnClick;
        property OnDblClick;
        property OnDragDrop;
        property OnEnter;
        property OnExit;
        property OnKeyDown;
        property OnKeyPress;
        property OnKeyUp;
        property OnMouseDown;
        property OnMouseMove;
        property OnMouseUp;
        property ActiveBorderColor : TColor
             read GetActiveBorderColor write SetActiveBorderColor
             default clBtnHighlight;
        property AutoSize : Boolean
             read GetAutoSize write SetAutoSize
             default False;
        property BasicHeight : Integer read FBasicHeight write FBasicHeight;
        { Caption }
        property Caption : String read GetCaption write SetCaption;
        property CaptionBGColor : TColor
             read GetCaptionBGColor write SetCaptionBGColor
             default clBtnHighlight;
        property Color;
        property DivLine : Integer read GetDivLine write SetDivLine default 49;
        property EditColor : TColor
             read GetEditColor write SetEditColor
             default ClWindow;
        property Font;
        property Height default 16;
        property InactiveBorderColor : TColor
             read GetInactiveBorderColor write SetInactiveBorderColor
             default clBtnShadow;
        property Text : String read GetText write SetText;
        property TextBGColor : TColor
             read GetTextBGColor write SetTextBGColor
             default clBtnFace;
        property Width default 20;

  end;

procedure Register;

implementation

procedure Register;
begin
     { Register TSpaceEdit with PGComps as its
       default page on the Delphi component palette }
     RegisterComponents('PGComps', [TSpaceEdit]);
end;

{ Method to set variable and property values and create objects }
procedure TSpaceEdit.AutoInitialize;
begin
     SEdit := Tedit.Create(Self);
     SEdit.Parent := Self;
     with SEdit do
          begin
          { Left := }
          { Top := }
          { Width := }
          { Height := }
          BorderStyle:=bsnone;
          Visible := False;
          end;
     FActive := False;
     FActiveBorderColor := clBtnHighlight;
     FAutoSize := False;
     FCaption := 'Caption';
     FCaptionBGColor := clBtnHighlight;
     FDivLine := 49;
     FEditColor := ClWindow;
     Height := 16;
     FInactiveBorderColor := clBtnShadow;
     FText := 'Text';
     FTextBGColor := clBtnFace;
     Width := 20;
end; { of AutoInitialize }

{ Method to free any objects created by AutoInitialize }
procedure TSpaceEdit.AutoDestroy;
begin
     SEdit.Free;
end; { of AutoDestroy }

function TSpaceEdit.GetActive : Boolean;
begin
     Result := FActive;
end;

procedure TSpaceEdit.SetActive(Value : Boolean);
begin
     FActive := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

function TSpaceEdit.GetActiveBorderColor : TColor;
begin
     Result := FActiveBorderColor;
end;

procedure TSpaceEdit.SetActiveBorderColor(Value : TColor);
begin
     FActiveBorderColor := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

function TSpaceEdit.GetAutoSize : Boolean;
begin
     Result := FAutoSize;
end;

procedure TSpaceEdit.SetAutoSize(Value : Boolean);
begin
     FAutoSize := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

function TSpaceEdit.GetCaption : String;
begin
     Result := FCaption;
end;

procedure TSpaceEdit.SetCaption(Value : String);
begin
     FCaption := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
     Invalidate;
end;

function TSpaceEdit.GetCaptionBGColor : TColor;
begin
     Result := FCaptionBGColor;
end;

procedure TSpaceEdit.SetCaptionBGColor(Value : TColor);
begin
     FCaptionBGColor := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

function TSpaceEdit.GetDivLine : Integer;
begin
     Result := FDivLine;
end;

procedure TSpaceEdit.SetDivLine(Value : Integer);
begin
     FDivLine := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
     Invalidate;
end;

function TSpaceEdit.GetEditColor : TColor;
begin
     Result := FEditColor;
end;

procedure TSpaceEdit.SetEditColor(Value : TColor);
begin
     FEditColor := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

function TSpaceEdit.GetInactiveBorderColor : TColor;
begin
     Result := FInActiveBorderColor;
end;

procedure TSpaceEdit.SetInactiveBorderColor(Value : TColor);
begin
     FInActiveBorderColor := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

function TSpaceEdit.GetText : String;
begin
     Result := FText;
end;

procedure TSpaceEdit.SetText(Value : String);
begin
     FText := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
       SEdit.text:=Text;
      Invalidate;
end;

function TSpaceEdit.GetTextBGColor : TColor;
begin
     Result := FTextBGColor;
end;

procedure TSpaceEdit.SetTextBGColor(Value : TColor);
begin
     FTextBGColor := Value;

     { If changing this property affects the appearance of
       the component, call Invalidate here so the image will be
       updated. }
      Invalidate;
end;

{ Override OnExit handler from TCustomControl }
procedure TSpaceEdit.DoExit;
begin
     inherited DoExit;
     text:=SEdit.Text;
     Active:=False;

end;

{ Override OnKeyPress handler from TCustomControl }
procedure TSpaceEdit.KeyPress(var Key : Char);
begin
     { Call method of parent class }
     inherited KeyPress(Key);
end;

{ Override OnMouseDown handler from TCustomControl }
procedure TSpaceEdit.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
     inherited MouseDown(Button, Shift, X, Y);
     setfocus;
     Active:=true;
     repaint;
end;

constructor TSpaceEdit.Create(AOwner: TComponent);
begin
     inherited Create(AOwner);
     AutoInitialize;

     { Code to perform other tasks when the component is created }

end;

destructor TSpaceEdit.Destroy;
begin
     AutoDestroy;
     inherited Destroy;
end;

procedure TSpaceEdit.DrawBorder (Colour:Tcolor; Active:Boolean);
var x,y,Hx,Tw1,Tw2:integer;
begin
//Basic data
Hx:=strtoint(floattostr(int(Height/2)));
TW1:=Canvas.TextWidth(Caption);
TW2:=Canvas.TextWidth(Text);

//Left End
Canvas.pen.color:=colour;
canvas.Brush.color:=CaptionBGColor;
canvas.arc(0,0,
           Height,Height,
           round(height/2),0,
           round(height/2),height);


//Right End

canvas.arc(width-Height,0,width,Height,
           Width-Height+Hx,Height,
           Width-Height+Hx,0);

//Lines going across
//top line
Canvas.Moveto(Hx,0);
Canvas.Lineto(width-hx+1,0);
//Bottom Line
Canvas.Moveto(Hx,BasicHeight);
Canvas.Lineto(width-hx+1,BasicHeight);

//Dividing Line
Canvas.Moveto(DivLine,0);
Canvas.LineTo(DivLine,Height);

//Color Left
Canvas.FloodFill(HX,2,canvas.Pixels[Hx,2],fsSurface);
Canvas.TextOut(Hx,2,Caption);

if Active then
begin
Canvas.Brush.color:=EditColor;
Canvas.FloodFill(Width-Hx,2,canvas.Pixels[Width-Hx,2],fsSurface);
Canvas.TextOut(DivLine+2,2,text);
SEdit.Top:=2;
SEdit.Width:=Width-Hx-DivLine-2;
SEdit.Left:=DivLine+2;
SEdit.Color:=EditColor;
//SEdit.BorderStyle:=bsNone;
SEdit.Height:=Height-4;
SEdit.Text:=Text;
SEdit.visible:=True;
end
else
Begin
//Color Right If not Active
canvas.brush.color:=TextBGColor;
Canvas.FloodFill(Width-HX,2,canvas.Pixels[Width-Hx,2],fsSurface);
Canvas.TextOut(DivLine+2,2,text);
SEdit.Top:=Height+1;
end;
end;

procedure TSpaceEdit.Loaded;
begin
     inherited Loaded;

     { Perform any component setup that depends on the property
       values having been set }

end;

procedure TSpaceEdit.Paint;
var x,y,Hx,Tw1,Tw2:integer;
begin
//Basic data
Hx:=strtoint(floattostr(int(Height/2)));
TW1:=Canvas.TextWidth(Caption);
TW2:=Canvas.TextWidth(Text);
if DivLine <(Hx+2+TW1+3) then DivLine:=(Hx+2+TW1+3);
if width <(DivLine+TW2+2+Hx) then Width:=DivLine+TW2+2+Hx;

if AutoSize then width:=DivLine+2+TW2+Hx;

     { Draw the component using the methods supplied by its Canvas
       property (of type TCanvas).  For example: }
     Canvas.font:=Font;
     SEdit.top:=height+1;
     BasicHeight:=Canvas.TextHeight('Oy')+3;

     Height:=BasicHeight+1;
     SEdit.top:=height+1;
if Active then DrawBorder (ActiveBorderColor,Active)
else
DrawBorder (InActiveBorderColor,Active);


end;


end.


I think I got the components Pas file set up right but I'm not sure.
Avatar of scrapdog
scrapdog
Flag of United States of America image

still looking...but this line caught my eye:

Hx:=strtoint(floattostr(int(Height/2)));


!!!!

Hx := round(Height/2);

would be a lot faster
Hi,
As a rule of thumb when setting properties it is wise to check the previous value, so for example

instead of

     FActive := Value;
     Invalidate;

we better

  if FActive <> Value then
  begin
     FActive := Value;
     Invalidate;
  end;

....igor
In your component, you have an Edit Box, but it appears that you keep it hidden.  Then you are manually painting everything on the canvas, including the text.

Wouldn't it be easier just to place the edit box within view at all times, and let the edit box handle the painting of text?

When your control receives focus (you can catch this through OnEnter), it could pass it straight to the edit box (using SetFocus).

This way, the only thing you would have to do in the Paint is draw the borders.  You would not have to handle any keypresses, worry about selected text, or paint any text, because the Edit Box would do that for you.  In fact, your component would then behave just like a standard Windows control (can be tabbed to, etc.)

In order to do this, you would probably want to derive your component from TWinControl instead.

If it is the appearance of the edit box you are worried about, you can set the borderstyle of the edit box to bsNone.  And you can set the background color, text color, and font of the Edit Box as well, within your component.  The user would not notice that there is an edit box inside your component.
On the other hand, a TWinControl doesn't have a Paint method.  Therefore my advice would be to implement the plan described in my previous comment, but continue deriving it from TCustomControl.  Then, in the *OnClick*, set the focus of the edit control.
Avatar of PPaul
PPaul

ASKER

Thanks... I did change the Hx:=strtoint(floattostr(int(Height/2))); to  Hx := round(Height/2);.

I've also put inplace all of the other changes you sugested and add in an Label for the Caption section.  I've also added in a OnTextChange event.

By making the edit box stay inplace has made it easyer... It now  seems to be a very smooth in Tabing and selecting... But the final problem (and I hope I was clear on that..:) is that when soenthing is over it and then moved it is the messed up How can this be fixed???

Thanks
PPaul

BTW:If you would like a I can send the updated code eather to your e-Mail or Post it here... My e-Mail is 920595@ican.net

Avatar of PPaul

ASKER

ScrapDog I would like to give you the points...
Sorry I didn't get to your e-mail...I've been so busy working on a VB database front-end (*ouuuuuchhhhhh*!) for a dumb project...I didn't get much time to look at your updated code.

However, let me know if you got it working now, or if you have any more questions.  If it isn't working, I will take a peak at your source code now...
btw, do you plan on implementing a bunch of components in this style?  I think it is kind of cool...
Avatar of PPaul

ASKER

Yea... I have in mind and SpinList, Caption(look like the Edit type) and and Number Spin... I would of liked to make an DropDownBox verson But I cant seem to fuger it out.. and an Buttion... But the base is the edit verson..
Avatar of PPaul

ASKER

Yea... I have in mind and SpinList, Caption(look like the Edit type) and and Number Spin... I would of liked to make an DropDownBox verson But I cant seem to fuger it out.. and an Buttion... But the base is the edit verson..
ASKER CERTIFIED SOLUTION
Avatar of scrapdog
scrapdog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial