Solved

Delphi Component doesn't fire Paint at runtime

Posted on 2001-06-30
13
637 Views
Last Modified: 2013-11-23
I'm a bit frustrated. The documentation tells me how to create a Graphical component but when I implement that it doesn't work. I've written the code below. Can anyone tell me why the Paint procedure isn't fired at runtime but is at designtime?

unit Meta;

interface

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

type
  TMeta = class(TGraphicControl)
  private
    { Private declarations }
    FOnPaint:     TNotifyEvent;
  protected
    { Protected declarations }
          procedure          Paint; override;
       property          OnPaint:     TNotifyEvent read FOnPaint write FOnPaint;
  public
    { Public declarations }
          constructor          Create(Owner: TComponent); override;
          destructor          Destroy; override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('MyComponents', [TMeta]);
end;

constructor     TMeta.Create(Owner: TComponent);
begin
     inherited     Create(Owner);
end;

destructor          TMeta.Destroy;
begin
     inherited     Destroy;
end;

procedure          TMeta.Paint;
var
     MyMetaFile:     TMetafile;
   I:     Integer;
begin
//   if not ( csDesigning in ComponentState ) then
   begin
        MyMetafile                := TMetafile.Create;
             MyMetafile.Width      := self.Width;
          MyMetafile.Height      := self.Height;

             with TMetafileCanvas.Create(MyMetafile, 0) do
             try
                  Brush.Color := clRed;

               for I := 0 to 100 do
                      Ellipse(I+3,I+3,I+100,I+100);
             finally
                  Free;
             end;

        self.Canvas.Draw(0,0,MyMetafile);  (* 100 red circle's  *)
    end;
end;

end.
0
Comment
Question by:Paul Sinnema
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:bugroger
ID: 6241242
Hi,

Use this definition and add
"Inherited paint;" -> calls TGraphicControl().paint
at the first line of your paint function

type
 TMeta = class(TGraphicControl)
 private
   { Private declarations }
 protected
   { Protected declarations }
         procedure          Paint; override;
 public
   { Public declarations }
         constructor          Create(Owner: TComponent); override;
         destructor          Destroy; override;
 published
   { Published declarations }
 end;


procedure          TMeta.Paint;
var
    MyMetaFile:     TMetafile;
  I:     Integer;
begin
 Inherited Paint;
 ....
 ...
 ..
end;

GL
 bug
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6241245
maybe you should call inherited once, before or after your code
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6241248
hi bugroger, same idea :-)
0
 

Author Comment

by:Paul Sinnema
ID: 6241256
Boy, are you guys fast!

No, doesn't work. Still get no painting at runtime. Had already tried that myself. The problem is that the Paint Method isn't fired at. I even put in a MessageBox just to see what happens. At design time a get the messagebox at runtime I don't.

Paul
0
 

Author Comment

by:Paul Sinnema
ID: 6241258
Boy, are you guys fast!

No, doesn't work. Still get no painting at runtime. Had already tried that myself. The problem is that the Paint Method isn't fired at. I even put in a MessageBox just to see what happens. At design time a get the messagebox at runtime I don't.

Paul
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6241264

Does your current code have the "//" ?
//   if not ( csDesigning in ComponentState ) then
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:bugroger
ID: 6241283
If i tried this code and it works fine:


Unit Meta;

interface

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

type
 TMeta = class(TGraphicControl)
 private
   { Private declarations }
 protected
   { Protected declarations }
   procedure          Paint; override;
 public
   { Public declarations }
    constructor         Create(Owner: TComponent); override;
    destructor          Destroy; override;
 published
   { Published declarations }
 end;

procedure Register;

implementation

procedure Register;
begin
 RegisterComponents('MyComponents', [TMeta]);
end;

constructor     TMeta.Create(Owner: TComponent);
begin
    inherited     Create(Owner);
end;

destructor          TMeta.Destroy;
begin
    inherited     Destroy;
end;

procedure          TMeta.Paint;
var
  MyMetaFile:     TMetafile;
  I:     Integer;
begin
  if not ( csDesigning in ComponentState ) then
  begin
       MyMetafile                := TMetafile.Create;
            MyMetafile.Width      := self.Width;
         MyMetafile.Height      := self.Height;

            with TMetafileCanvas.Create(MyMetafile, 0) do
            try
                 Brush.Color := clRed;

              for I := 0 to 100 do
                     Ellipse(I+3,I+3,I+100,I+100);
            finally
                 Free;
            end;

       self.Canvas.Draw(0,0,MyMetafile);  (* 100 red circle's  *)
   end;
end;

end.
0
 

Author Comment

by:Paul Sinnema
ID: 6242869
yes, the // are in the code. So the drawing should be in both runtime and designtime.

What operating system are you on. I use windows 2000 professional. Could that make a difference? It doesn't work with me?
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6242918
I'm using Windows 98
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6242950
What version of Delphi do you have?
I have Delphi 5.

You can try to put a TImage component onto your form.
If it won' t be painted then you have a problem but
when it works you can try this Paint - code.
Also have a look at TImage.Paint !!!

procedure TMeta.Paint;
var
 MyMetaFile :     TMetafile;
 I          :     Integer;
begin
 with inherited Canvas do
 begin
  MyMetafile        := TMetafile.Create;
  MyMetafile.Width  := self.Width;
  MyMetafile.Height := self.Height;

   with TMetafileCanvas.Create(MyMetafile, 0) do
  try
   Brush.Color := clRed;

   for I := 0 to 100 do Ellipse(I+3,I+3,I+100,I+100);
  finally
   Free;
  end;
    Draw(0,0,MyMetafile);  (* 100 red circle's  *)
  end;
end;

procedure TImage.Paint;
var
  Save: Boolean;
begin
  if csDesigning in ComponentState then
    with inherited Canvas do
    begin
      Pen.Style := psDash;
      Brush.Style := bsClear;
      Rectangle(0, 0, Width, Height);
    end;
  Save := FDrawing;
  FDrawing := True;
  try
    with inherited Canvas do
      StretchDraw(DestRect, Picture.Graphic);
  finally
    FDrawing := Save;
  end;
end;

0
 

Author Comment

by:Paul Sinnema
ID: 6243053
I've found the trouble. Somehow a Meta.dcu file along with a Meta.pas file was put into the Lib directory of Delphi. These were pretty old (I think I wrestled with the Meta problem earlier last year and gave up). Because of that, the wrong .dcu file was installed into Delphi. The weird thing however is that Delphi did see the new .dcu at design time and didn't at runtime (compiled version). After removing the Meta.dcu and Meta.pas from the Lib directory, recompiling the example, everything worked as it should. Sorry for buying your time, thanks for the effort.

Paul.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 8701293
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

To be PAQ/Refund

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Thank you,
Russell

EE Cleanup Volunteer
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 8816319
Per recommendation,

PashaMod
Community Support Moderator @Experts Exchange
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy file in dll not working but working on exe ! 18 87
Delphi XE10, MySQL Query 4 125
Simple Delphi Question 9 83
Strange behavior when a form is closed 6 50
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now