Link to home
Start Free TrialLog in
Avatar of Stuart_Johnson
Stuart_Johnson

asked on

TEdit with Canvas

Hi.

I need a TEdit control with a canvas property.  Any ideas how I can do this?

Stu.
Avatar of Stuart_Johnson
Stuart_Johnson

ASKER

That was pretty vague.  Let me expand....

I am writing a suite of new components for a project I am writing.  One of them is an edit control.  However, I need it to have a canvas so I can draw on it.  Is there any possible way I can achieve this without having to completely write a new component?  I would love to derive it from TCustomEdit if possible.

Thanks.

Stu.
Hi Stuart,
Here is a source example to make a Canvas to a TEdit control

Greetings Smilly.

unit edcanvas;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    FCanvas: TCanvas;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
     FCanvas.LineTo(edit1.width,edit1.height);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
     FCanvas := TControlCanvas.Create;
     TControlCanvas(FCanvas).Control := edit1;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
     FCanvas.Free;
end;

end.


Smilly.  Can you show me how that would look in a skeleton component?  I don't follow what you have done.

Im about to go home for the day, so I wont get a chance to look at it until the morning.

Stu.
Hi Stuart,
Okay here is an example component, I hope this will help you.....

Smilly


unit EdCanvas;

interface

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

type
  TEdCanvas = class(TCustomEdit)
  private
    FCanvas:    TCanvas;
  public
    Constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
  published
    property Canvas: TCanvas read FCanvas;
  end;

procedure Register;

implementation

Constructor TEdCanvas.Create(AOwner: TComponent);
begin
     inherited Create(AOwner);
     FCanvas := TControlCanvas.Create;
     TControlCanvas(FCanvas).Control := Self;
end;

destructor TEdCanvas.Destroy;
begin
     FCanvas.Free;
     inherited;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TEdCanvas]);
end;

end.
Hi Smilly,

That doesnt seem to work.  Sure, I now have a canvas property, but I cant actually do anything with it.  I have no paint method either, so I cant update anything I need to draw on it.

I dont just want a Canvas property, I want to be able to use the TEdit control like a graphic control - draw on it, erase bits of it etc etc.

Stu.
Hi Stuart,
You can draw on the edit control with the Canvas,

EdCanvas1.Canvas.LineTo(100,100);

Smilly
Smilly,

I want to do this within the component though, not from the project.  I want to be able to give the control various effects.  As there is no paint method at all with a TCustomEdit, I can not draw anything and have it automatically refresh.

Stu.
Smilly,

Dont take offence, I am not rejecting your answer because it is crap, I just want it left open so other people can see it.

Stu,
Stu,

reading your conversation with Smilly I assume you aren't asking for a canvas but how to enable "custom" draw in your edit control. If this is correct then I'd like to direct your attention to WM_PAINT. This is a message which is posted to a window if it needs to be redrawn. It passes along device context (actually a canvas's handle is a DC). So you could write:

type
  TMyControl = class(TCustomEdit)
  private
    FCanvas: TCanvas;
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
  end;

procedure TMyControl.WMPaint(var Message: TWMPaint);

var PS: TPaintStruct;

begin
  // Message.DC is <> 0 if the paint has to be done to a redirected device context (printer, bitmap, another window etc.)
  if Message.DC = 0 then FCanvas.Handle := BeginPaint(Handle, PS)
                    else FCanvas.Handle := Message.DC;
  try
    // do your painting here
  finally
    // important: the handle must not be further used by this canvas
    FCanvas.Handle := 0;
    if Message.DC = 0 then EndPaint(Handle, PS);
  end;
end;

The problem here is, though, that you have to do all the paintings yourself, as the default window proc of the edit won't draw anything if you have already called BeginPaint (the update rect is reset by this call). But as you want to do your own effects this is probably not a limitiation at all.

Ciao, Mike
Hi Stuart,

Thats okay, if you want to control the paint method, then Mike is right, then you have to paint everything yourself.

good luck....

Smilly
Mike,

Thats wonderful!  Works perfectly.  I read your final comment about having to paint everything myself, which is fine, but does this extend to text as well?  Because if I update the text property in the OE, it doesnt paint on the new component.

Please post an answer Mike.  Much appreciated.  And thank you to Smilly to.  This is great!

Stu.
Just a question.  I don't seem to be able to highlight and copy text from EE anymore.  Every other web page works perfectly.  Does anyone else have this problem?

Stu.
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks again Mike and All.

Stu.

PS.  Anyone want to tell me if they can highlight text here?  I get the crNoDrop cursor when I try to highlight.
This is fixed since yesterday...
Yep.  Is too.  Wasnt this morning, but it is now.

Stu
This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
https://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange