Solved

Create Custom Shape Component

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Print Graphic and Text to Epson TM-T88v 12 260
Error E2158 compiling with Delphi XE10 Seattle 2 100
Unique identifier on a terminal server (rdp) 4 55
Dynamically Created Query 3 55
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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