?
Solved

Component Bugs...

Posted on 1999-12-02
11
Medium Priority
?
513 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:PPaul
  • 6
  • 4
11 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 2251844
still looking...but this line caught my eye:

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


!!!!

Hx := round(Height/2);

would be a lot faster
0
 
LVL 5

Expert Comment

by:inter
ID: 2251863
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
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 2251865
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.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:scrapdog
ID: 2251869
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.
0
 

Author Comment

by:PPaul
ID: 2254679
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

0
 

Author Comment

by:PPaul
ID: 2276169
ScrapDog I would like to give you the points...
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 2276182
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...
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 2276185
btw, do you plan on implementing a bunch of components in this style?  I think it is kind of cool...
0
 

Author Comment

by:PPaul
ID: 2276193
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..
0
 

Author Comment

by:PPaul
ID: 2276206
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..
0
 
LVL 5

Accepted Solution

by:
scrapdog earned 1400 total points
ID: 2276214
So I take it you have it working now?

The drop down box could probably be implemented in the same way as the edit (using a Windows combobox as a child of your component).  It depends on how much you want to customize it.  If you want the actual behavior of the combobox to change, you may have to build it from scratch (and this would be pretty tough).

The button you would probably have to do from scratch, however that wouldn't be too hard.  You could use a TLabel as a child of the component, and draw the border manually.  Your Paint method could paint the button in two different states...when the button is depressed, and when it is up (this can be achieved by changing the colors of the borders, and offsetting the caption a bit).
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question