Jump to content
A-man

Form Events

Recommended Posts

Is there any information available on using events like OnMouseEnter or OnMouseDown in forms? I noticed in the example from the old help guide it used 'Button1.OnClick:= @buttonclick;' to exit the form. The form editor is really useful, but I have no idea where I can learn to use the events. It is not specific to those events, but those are 2 I need especially.

 

EDIT: I am also going to run into the problem of decimal precision with my mandlebrot veiwer. Is there a way to set the decimal precision? Or a way to code it to use quads or something?

Edited by A-man
Link to comment
Share on other sites

After you create you form you go to what object you wish to add the event on and add the corresponding event, with a function address then within the function u actually make the procedure do what you need it to do. The Functions name and parameters need to match that of the event you calling. View the example below

 

Run this example and look at the code

[scar]

type

TForm1 = record

Form1: TForm;

end;

 

var

Form1: TForm1;

 

procedure ChangeColor(Sender: TObject); // This is how a TNotifyEvent is setup

begin

With Form1 do

With Form1 do

Color := RandomRange(0, 255);

end;

 

procedure Form1_Init;

begin

with Form1 do

begin

Form1 := CreateForm;

with Form1 do

begin

Left := 376;

Top := 174;

Caption := 'Form1';

ClientHeight := 202;

ClientWidth := 304;

Color := clBtnFace;

Font.Charset := DEFAULT_CHARSET;

Font.Color := clWindowText;

Font.Height := -11;

Font.Name := 'Tahoma';

Font.Style := [];

OldCreateOrder := False;

PixelsPerInch := 96;

OnMouseEnter := @ChangeColor;

end;

end;

end;

 

procedure Form1_SafeInit;

var

v: TVariantArray;

begin

SetLength(v, 0);

ThreadSafeCall('Form1_Init', v);

end;

 

function Form1_ShowModal: Boolean;

begin

Result := Form1.Form1.ShowModal = mrOk;

end;

 

function Form1_SafeShowModal: Boolean;

var

v: TVariantArray;

begin

SetLength(v, 0);

Result := ThreadSafeCall('Form1_ShowModal', v);

end;

 

begin

Form1_SafeInit;

Form1_SafeShowModal;

FreeForm(Form1.Form1);

end.

[/scar]

Link to comment
Share on other sites

Ok, that makes sense. I guess I'll be more specific. Here is my code I want to execute. The program will be running, and when it gets to a certain point, I want it to call this draw function to display the image on the TImage.

 

procedure draw(Sender: TObject);
 var 
   i : TSCARBitmap;
begin
 i := TSCARBitmap.create(''); 
 i.loadfrompng('C:\Users\A-man\Pictures\mandlebrot\mandlebrot.png'); 
 with Image1 do
   i.assign(Picture);
end;

 

This doesn't do anything though. I think it is assigning Picture to i, not i to Picture. I'm not sure how to do that.

Link to comment
Share on other sites

Ok, that makes sense. I guess I'll be more specific. Here is my code I want to execute. The program will be running, and when it gets to a certain point, I want it to call this draw function to display the image on the TImage.

 

procedure draw(Sender: TObject);
 var 
   i : TSCARBitmap;
begin
 i := TSCARBitmap.create(''); 
 i.loadfrompng('C:\Users\A-man\Pictures\mandlebrot\mandlebrot.png'); 
 with Image1 do
   i.assign(Picture);
end;

 

This doesn't do anything though. I think it is assigning Picture to i, not i to Picture. I'm not sure how to do that.

 

Maybe try this, I don't think TBitmaps work in SCAR though:

 

[sCAR]

a.Picture := nil;

a.Picture.Bitmap := i as TBitmap;

[/sCAR]

 

You could always copy the canvas. Try using the CopyCanvas function.

 

[sCAR]

program New;

var

i: TSCARBitmap;

a: TImage;

 

begin

a := TImage.Create(nil);

CopyCanvas(i.Canvas, a.Canvas, 0, 0, sourceW, sourceH, 0, 0, destW, destH);

end.

 

[/sCAR]

Link to comment
Share on other sites

Copy canvas is working great. Next question:

OnMouseEnter := @draw; works, whereas OnMouseEnter := @click does not work. OnMouseEnter throws a type mismatch for some reason. I think it's because 1 is a TMouseEvent and one is a TNotifyEvent. OnMouseEnter works when I change click() from a procedure to a function, but I don't want it to return anything.

Link to comment
Share on other sites

Ok, that makes sense. I guess I'll be more specific. Here is my code I want to execute. The program will be running, and when it gets to a certain point, I want it to call this draw function to display the image on the TImage.

 

procedure draw(Sender: TObject);
 var 
   i : TSCARBitmap;
begin
 i := TSCARBitmap.create(''); 
 i.loadfrompng('C:\Users\A-man\Pictures\mandlebrot\mandlebrot.png'); 
 with Image1 do
   i.assign(Picture);
end;

 

This doesn't do anything though. I think it is assigning Picture to i, not i to Picture. I'm not sure how to do that.

 

[scar]procedure draw(Sender: TObject);

var

Bmp: TSCARBitmap;

begin

Bmp := TSCARBitmap.Create('');

try

Bmp.LoadFromPng('C:\Users\A-man\Pictures\mandlebrot\mandlebrot.png');

Bmp.AssignTo(Image1.Picture);

finally

Bmp.Free;

end;

end;[/scar]

 

Maybe try this, I don't think TBitmaps work in SCAR though:

 

[sCAR]

a.Picture := nil;

a.Picture.Bitmap := i as TBitmap;

[/sCAR]

 

TBitmap works just fine in SCAR, but casting a TSCARBitmap to a TBitmap won't magically make it a TBitmap object... + you should never assign nil to an object property.

 

Copy canvas is working great. Next question:

OnMouseEnter := @draw; works, whereas OnMouseEnter := @click does not work. OnMouseEnter throws a type mismatch for some reason. I think it's because 1 is a TMouseEvent and one is a TNotifyEvent. OnMouseEnter works when I change click() from a procedure to a function, but I don't want it to return anything.

 

http://docwiki.embarcadero.com/Libraries/en/Vcl.ExtCtrls.TImage_Events

Edited by Freddy
Link to comment
Share on other sites

[scar]procedure draw(Sender: TObject);

var

Bmp: TSCARBitmap;

begin

Bmp := TSCARBitmap.Create('');

try

Bmp.LoadFromPng('C:\Users\A-man\Pictures\mandlebrot\mandlebrot.png');

Bmp.AssignTo(Image1.Picture);

finally

Bmp.Free;

end;

end;[/scar]

 

 

 

TBitmap works just fine in SCAR, but casting a TSCARBitmap to a TBitmap won't magically make it a TBitmap object... + you should never assign nil to an object property.

 

 

 

http://docwiki.embarcadero.com/Libraries/en/Vcl.ExtCtrls.TImage_Events

 

Is there a way to, load the TSCARBitmap variable into a TBitmap without saving as a .bmp or image format?

This might help some what with the form components for SCAR. I guess you could just use CopyCanvas though...

 

Is there a way to load a TImage component by itself. Like show it onto the screen without it having the form as the parent or anything as the parent?

Link to comment
Share on other sites

Is there a way to, load the TSCARBitmap variable into a TBitmap without saving as a .bmp or image format?

This might help some what with the form components for SCAR. I guess you could just use CopyCanvas though...

 

Is there a way to load a TImage component by itself. Like show it onto the screen without it having the form as the parent or anything as the parent?

 

Why not just use the TSCARBitmap AssignTo function? Thats the whole reason he implimented the assignto function, to make it easier to assign images to TObjects.

Link to comment
Share on other sites

Why not just use the TSCARBitmap AssignTo function? Thats the whole reason he implimented the assignto function, to make it easier to assign images to TObjects.

 

Crap my bad, I thought assign to was only for that type? You know I got right to that point in his video and that i think is where I had to stop because I had to do something. Lol...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...