Solved

Delphi Component doesn't fire Paint at runtime

Posted on 2001-06-30
13
634 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

18 Experts available now in Live!

Get 1:1 Help Now