SRL Archive - General

How do I drawing on to a form's canvas?


Ron

2009-05-28 01:04:31

How do I draw onto a form's canvas?

I searched through thread after thread but I could only find canvas related help for the Debug Canvas rather than the form canvas. Can anyone help?

I want to use the regular drawing functions to draw lines, ellipses, rectangles, etc.

Thanks,
Ron

bullzeye95

2009-05-28 01:34:46

What exactly are you wanting to draw on it? A bitmap, or just general canvas stuff (lines, shapes, etc.)?

Cazax

2009-05-28 01:40:06

Use the property Form.Canvas. To draw on it use CopyCanvas or SafeCopyCanvas, you can find a tutorial by yakman. You can also use it to draw rectangles, circles, etc with Form.Canvas.Rectangle/Pie/Arc/whatever.

marpis

2009-05-28 13:58:18

I think it's done the same way than bitmaps?
SafeCopyCanvas(source.canvas, From1.canvas, …)

doesn't that work? because i think TForm has a 'canvas' property

Ron

2009-05-29 00:19:11

This is really weird… I've gotten this to work before with 2.03. Any way, I wanted to be able to draw lines, rectangles, ellipses, etc. However, I can't seem to get it to work correctly.

Here is my code:

program CanvasForm;

var
frmDesign : TForm;
canvas : TCanvas;

procedure InitForm;
var
i : Integer;
mnuMenu : TMenuItem;
begin
frmDesign := CreateForm;
with frmDesign do
begin
Position := poDesktopCenter;
Width := 350;
Height := 250;
Caption := 'Canvas Form';
Visible := False;
end;
//Canvas := frmDesign.Canvas;
frmDesign.Canvas.Brush.Style := bsSolid;
frmDesign.Canvas.Brush.Color := clRed;
frmDesign.Canvas.Rectangle(5, 5, 245, 245);
SafeDrawBitmap( BitmapFromString( 400, 400, '' ), frmDesign.Canvas, 0, 0 );
frmDesign.Canvas.Refresh;
end;

procedure SafeInitForm;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'InitForm', v );
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'ShowFormModal', v );
end;

begin
ClearDebug;
SafeInitForm;
SafeShowFormModal;
FreeForm( frmDesign );
end.

~Ron

Iroki

2009-05-29 00:32:36

Ron, maybe tray to make draving on canvas procedure in plugin, it should be easier way :)

Ron

2009-05-29 00:54:39

OK so I found Yakman's tutorial and it helped a little. I can draw on canvases but it only seems to work in a MouseMove event… I tried to paste the same code in an OnShow event and it didn't work. :(

Here's my code now:

program CanvasForm;

var
frmDesign : TForm;

procedure FormMouseMove(Sender : TObject; Shift : TShiftState; x , y : Integer);
begin
frmDesign.Canvas.Pen.Color := 255;
frmDesign.Canvas.Pen.Width := 5;
frmDesign.Canvas.MoveTo(x - 5, y - 5);
frmDesign.Canvas.LineTo(x, y);
end;

procedure FormOnShow(Sender: TObject);
begin
frmDesign.Canvas.Pen.Color := 255;
frmDesign.Canvas.Pen.Width := 5;
frmDesign.Canvas.MoveTo(0, 0);
frmDesign.Canvas.LineTo(55, 55);
end;

procedure InitForm;
begin
frmDesign := CreateForm;
with frmDesign do
begin
Position := poDesktopCenter;
Width := 350;
Height := 250;
Caption := 'Canvas Form';
Visible := False;
OnMouseMove:= @FormMouseMove;
OnShow := @FormOnShow;
end;
{frmDesign.Canvas.Brush.Style := bsSolid;
frmDesign.Canvas.Brush.Color := clRed;
frmDesign.Canvas.Rectangle(5, 5, 245, 245);}
//CopyCanvas( Canvas, frmDesign.Canvas, 1, -1, 300, 520, 0, 0, 315, 520);
//SafeDrawBitmap(BitmapFromString(100,100,''), frmDesign.Canvas, 0, 0);
end;

procedure SafeInitForm;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'InitForm', v );
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'ShowFormModal', v );
end;

begin
ClearDebug;
SafeInitForm;
SafeShowFormModal;
FreeForm( frmDesign );
end.

and no thank you. Id rather learn how to do it in scar than create a plugin.

~Ron

bullzeye95

2009-05-29 00:58:29

That won't work because you draw on it, and then display it.

program CanvasForm;

var
frmDesign : TForm;
canvas : TCanvas;

procedure OnPaint(Sender: TObject);
begin
frmDesign.Canvas.Brush.Style := bsSolid;
frmDesign.Canvas.Brush.Color := clRed;
frmDesign.Canvas.Rectangle(5, 5, 245, 245);
SafeDrawBitmap( BitmapFromString( 400, 400, '' ), frmDesign.Canvas, 0, 0 );
frmDesign.Canvas.Refresh;
showmessage('nothacks');
end;

procedure InitForm;
var
i : Integer;
mnuMenu : TMenuItem;
begin
frmDesign := CreateForm;
with frmDesign do
begin
Position := poDesktopCenter;
Width := 350;
Height := 250;
Caption := 'Canvas Form';
Visible := False;
end;
frmDesign.OnPaint:= @OnPaint;
//Canvas := frmDesign.Canvas;
end;

procedure SafeInitForm;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'InitForm', v );
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'ShowFormModal', v );
end;

begin
ClearDebug;
SafeInitForm;
SafeShowFormModal;
FreeForm( frmDesign );
end.

EDIT: That sentence was meant for your previous post. OnShow is called right before it's actually shown, not right after.

EDIT2: Sorry, removed the malicious code.

Ron

2009-05-29 03:11:21

Don't ever put malicious code in a code snippet again.

On a lighter note, thanks a lot bullseye, here is some code I was having fun with.


// canvas painting by Ron
//
// draws a face using...
// - rectangle
// - 2 ellipses
// - 1 polygon using TPoints
// bullseye95 helped with painting on the form, thank you

program CanvasFormByRon;

var
frmDesign : TForm;

// necessary to paint on a form
// thanks bullseye95
procedure OnPaint(Sender : TObject);
var
a : Array of TPoint;
xOff, yOff : Integer;
begin
frmDesign.Canvas.Brush.Style := bsSolid;
frmDesign.Canvas.Brush.Color := clRed;
frmDesign.Canvas.Rectangle(5, 5, 345, 245);
frmDesign.Canvas.Brush.Color := clCream;
frmDesign.Canvas.Ellipse( 100, 100, 10, 10 ); // left ellipse
frmDesign.Canvas.Ellipse( 340, 100, 250, 10 ); // right ellipse
frmDesign.Canvas.Brush.Color := clPurple;
SetArrayLength( a, 4 );
xOff := 0;
yOff := 30;
a[ 0 ].x := 50 + xOff;
a[ 0 ].y := 100 + yOff;
a[ 1 ].x := 300 + xOff;
a[ 1 ].y := 100 + yOff;
a[ 2 ].x := 200 + xOff;
a[ 2 ].y := 170 + yOff;
a[ 3 ].x := 150 + xOff;
a[ 3 ].y := 170 + yOff;
frmDesign.Canvas.Polygon( a );
end;

procedure InitForm;
var
i : Integer;
begin
frmDesign := CreateForm;
with frmDesign do
begin
Position := poDesktopCenter;
ClientWidth := 350;
ClientHeight := 250;
Caption := 'Canvas Form by Ron';
Visible := False;
OnPaint := @OnPaint;
end;
end;

procedure SafeInitForm;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'InitForm', v );
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v : TVariantArray;
begin
SetArrayLength( v, 0 );
ThreadSafeCall( 'ShowFormModal', v );
end;

begin
ClearDebug;
SafeInitForm;
SafeShowFormModal;
FreeForm( frmDesign );
end.

moral of the thread is use the OnPaint procedure if you are painting on a form.

~Ron