• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Run code in Design mode - possible?

Hi

is it possible to create a component that can execute some code in design mode?

let's say I have a Panel which holds squares, 3 different sizes 10x10, 100x100 and 200x200... and they are drawn on Panel, depending on the width and height of the panel. For example, if Panel has a height set below 100, then 10x10 squares get drawn, depending on how many can be drawn in width. If Panel is more than 100 in height and width, then squares of 100x100 are drawn...  So, the bigger the Panel the more squares are drawn on it, and of course it depends which of the three types of squares are drawn - which fit in.

I hope it's clear enough.

But the developer doesn't really know how many squares will fit in Panel in design mode when resizing and positioning Panel on Form, only when program is run the squares are drawn.

I would like to know if it's possible to create a component that will draw the squares on Panel (Component) in design mode, so the developer will immediately see the result as if it's in runtime mode. So, when the Panel is resized and moved around on the form, it will draw the squares so the developer knows sees exactly how it will look like in runtime.

The squares are drawn on Paint event of Panel.

Thank you
0
Delphi_developer
Asked:
Delphi_developer
2 Solutions
 
rfwoolfCommented:
I don't really do component writing, I'm sure this is possible, but I wouldn't be able to tell you how to do it.
0
 
MerijnBSr. Software EngineerCommented:
The paint method is called in design time just like in run time.
0
 
HypoCommented:
Yes, It is possible to do that.
As MerijnB says, the paint method is called in design time just like in runtime, so the only thing you have to do is to override the Paint method of your component; and if you only want to paint the rectangles in design time and not in runtime, then you can determine if the component exists in design time by checking the ComponentState property like in the example below.

regards
Hypo


Procedure TMyPanel.Paint; 
  inherited; 
  if csDesigning in ComponentState then begin
    // paint your rectangles here... 
  end;
end;

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Geert GruwezOracle dbaCommented:
in this case the TPanel would act as a container for other objects.
If you want this at designtime, the best thing is for the items to be a descendant of the TWinControl type
you would need to create a descendant of TPanel
and add objects to it with the panel as the owner
because the tPanel is the owner it will call the child objects paint methods and draw them.

0
 
reynaldioCommented:
i think you can write a component editor for this. just like delphi use it for its components, e.g TCLientDataset when you want to add a field. You run the process by right clicking the component.

http://delphi.about.com/library/bluc/text/uc092501a.htm
0
 
Delphi_developerAuthor Commented:
Sorry for the delay, I have so many projects going on....

I've been trying to set this up, but I'm not sure how exactly. I'm attaching a code, but on compile I get error message:

"A class names TPanel already exists".

Please help.

Thank you


unit MyPanel_paint;
 
interface
 
uses
  SysUtils,richEdit, Classes, Controls, ComCtrls, ExtCtrls,Forms, GRaphics, Dialogs;
 
type
 
  TPanel = class(ExtCtrls.TPanel)
    protected
     FOnPaint : TNotifyEvent;
//     Procedure Paint; Override;
    public
     Property OnPaint : TNotifyEvent read FOnPaint write FOnPaint;
  end;
 
  TPanel_1_paint = class(TPanel)
  public
    { Public declarations }
    constructor Create (AOwner: TComponent); override;
  published
    { Published declarations }
    Procedure Paint;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('Samples', [TPanel_1_paint]);
end;
 
 
constructor TPanel_1_paint.Create(AOwner: TComponent);
begin
  inherited Create (AOwner);
end;
 
Procedure TPanel_1_paint.Paint;
begin
  inherited;
  showmessage('painting');
end;
 
end.

Open in new window

0
 
HypoCommented:
Perhaps you already have registered your component once. Check Component->InstallPackages to see if you can remove your component, and then try to reregister it again. Otherwise, you can try the source below.

/H
unit PaintPanel;
 
interface
 
uses
  SysUtils,richEdit, Classes, Controls, ComCtrls, ExtCtrls,Forms, GRaphics, Dialogs;
 
type
  TPaintPanel = class(TPanel)
  public
    { Public declarations }
    FOnPaint : TNotifyEvent;
  published
    { Published declarations }
    Procedure Paint; Override; 
    Property OnPaint : TNotifyEvent read FOnPaint write FOnPaint;
    Property Canvas;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('Samples', [TPaintPanel]);
end;
 
Procedure TPaintPanel.Paint;
begin
  inherited;
  if csDesigning in ComponentState then begin
    Canvas.TextOut(10, 10, 'In design mode');
  end;
  if Assigned(FOnPaint) then
    FOnPaint(Self);
end;
 
end.

Open in new window

0
 
Delphi_developerAuthor Commented:
Thank you, it works now.

Now it works only once, how can I make it draw 'continuously'? For example if I want the text to be drawn in the center of the panel, and repaints on the new center when I resize the panel, in csDesigning mode. Is this possible?
0
 
HypoCommented:
I'm not sure what you mean by drawing continuosly? The paint handler will be triggered for several different reasons, for which one already includes resizing of the control. I've altered the paint function so that it centers the text; but If that's not what you were asking for, then I think you need to explain som more what you are after.  :)

regards
Hypo
Procedure TPaintPanel.Paint;
var strText : String;
begin
  inherited;
  if csDesigning in ComponentState then begin
    strText := 'Only in design mode';
    Canvas.TextOut((Width - Canvas.TextWidth(strText)) div 2,
                   (Height div 2) + Canvas.TextHeight(Caption),
                   strText);
  end;
  if Assigned(FOnPaint) then
    FOnPaint(Self);
end;

Open in new window

0
 
Delphi_developerAuthor Commented:
That's it, thank you!

Well, I'm trying to draw rectangles on panel, and it should draw them as much as it can, depending on their size.

I think I'll be able to start working with what I have now.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now