?
Solved

Create Custom Shape Component

Posted on 2001-08-06
4
Medium Priority
?
242 Views
Last Modified: 2010-04-06
Hi, I need help on create a new component

This is my source code:
My problem is When I use method
   What should I do to make shape repaint when I add string to top items


thanks




unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,extctrls,
  StdCtrls;

type
  tmy=class(tshape)
  topitem:tstrings;
  bottomitem:tstrings;
  constructor Create(Owner: TComponent); override;
  procedure paint;override;
  end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  a:tmy;
implementation

{$R *.DFM}


constructor Tmy.Create(Owner: TComponent);
begin
  inherited Create(Owner);  // Initialize inherited parts
  topitem:=tstringlist.create;
end;

procedure tmy.paint;
begin
   inherited paint;
   if topitem.count>0 then
   canvas.textout(1,1,topitem.strings[0]);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
a:=tmy.create(self);
a.parent:=form1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   a.topitem.Add('Hello Hello');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
a.paint;
end;

end.
0
Comment
Question by:rsah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Expert Comment

by:comptebidon81
ID: 6355971

If you are trying to modify the apperance of a component, you can try to build a region in witch your component will painted:

var
    FormRegion  : THandle;
...

                FormRegion := CreateRectRgn(Left, Top, Right, Bottom);
                SetWindowRgn(YourComponent.Handle, FormRegion, True);

You can also use different shapes than Rectangle (see Help).

GunDamn
0
 
LVL 3

Accepted Solution

by:
nnbbb09 earned 400 total points
ID: 6357385
Hello,

What you could do is add a public procedure to your shape class that accepts a string and adds it to your stringlist. Once this is done it could invalidate itself causing its canvas to be repainted.

eg :

function TMy.AddTopItem(Item: String): Integer;
begin
  result := TopItem.Add(Item);
  invalidate;
end;

Users of your class should then use "a.AddTopitem('Hello Hello')" rather than "a.topitem.Add('Hello Hello')"

If you wanted to be completely thorough ,then you should hide the TStrings objects by making them private and then implement certain bits of stringlist behaviour such as AddTopItem, DeleteTopItem etc.. in your shape class. This would require more work, but would make your class more robust.

Jo
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 6360066
I agree with  nnbbb09, seems like the only logical way of doing what you need...

Also, do not forget to create a destructor and override it to free your StringLists

destructor MyClass.Destroy;
begin
 MyStringList1.Free;
 MyStringList2.Free;
 inherited Destroy;
end;

Shane Holmes
0
 

Author Comment

by:rsah
ID: 6360612
thanks nnbbb09
and also thanks to shaneholmes.
sorry to shaneholmes the credit is for nnbbb09
don't be angry
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

650 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