Solved

Create Custom Shape Component

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 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