Solved

Delphi Component doesn't fire Paint at runtime

Posted on 2001-06-30
13
658 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
[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
  • 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
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!

 

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
 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

617 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