Solved

Create Custom Shape Component

Posted on 2001-08-06
4
229 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
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 100 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now