Solved

Create Custom Shape Component

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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