cvbmn
asked on
Add events
Add events
How to add evetns to components ?
For example, events (OnClick and OnMouseMove) to all QuickReport components ?
How to add evetns to components ?
For example, events (OnClick and OnMouseMove) to all QuickReport components ?
ASKER
Can you give me some example ?
Hi cvbmn,
OK, here is it. Sample shows how to create your own event and handle mouse moving.
type
TMyEventProc = procedure(Sender: TObject; SomeString: String) of Object;
TMyControl = class(TCustomControl)
private
FMyEvent: TMyEventProc;
protected
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
published
property MyEvent: TMyEventProc read FMyEvent write FMyEvent;
end;
implementation
procedure TMyControl.MouseMove(Shift : TShiftState; X, Y: Integer);
begin
if Assigned(FMyEvent) then
FMyEvent(Self, 'Mouse position: '+IntToStr(X)+':'+IntToStr (Y));
Inherited;
end;
OK, here is it. Sample shows how to create your own event and handle mouse moving.
type
TMyEventProc = procedure(Sender: TObject; SomeString: String) of Object;
TMyControl = class(TCustomControl)
private
FMyEvent: TMyEventProc;
protected
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
published
property MyEvent: TMyEventProc read FMyEvent write FMyEvent;
end;
implementation
procedure TMyControl.MouseMove(Shift
begin
if Assigned(FMyEvent) then
FMyEvent(Self, 'Mouse position: '+IntToStr(X)+':'+IntToStr
Inherited;
end;
Take a look at this page, to find how to create to enahaced exist component
http://delphi.about.com/compute/delphi/cs/vclwriteenhance/index.htm
http://delphi.about.com/compute/delphi/cs/vclwriteenhance/index.htm
ASKER
//How to add QRRichText1MouseMove to QRRichText1.OnMouseMove event. Thanks.
//Here is code :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, QRCTRLS,
QuickRpt;
type
TForm1 = class(TForm)
QRRichText1: TQRRichText;
private
{ Private declarations }
private
// variable to hold the value passed to WM_SYSCOMMAND...
fSysCmd:integer;
// variable to hold the "default" cursor, just in case it isn't "crDefault"
fSavedCursor:TCursor;
FEnableMoving: Boolean;
FEnableSizing: Boolean;
procedure QRRichText1MouseMove(Sende r: TObject; Shift: TShiftState; X,
Y: Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// constants passed to WM_SYSCOMMAND
// For some reason, these are not listed in the help file,
// I had to play around a bit to find them...
const
SizeE = $F002; // east
SizeW = $F001; // west
SizeN = $F003; // north
SizeS = $F006; // south
SizeNW = $F004; // northwest
SizeNE = $F005; // northeast
SizeSW = $F007; // southwest
SizeSE = $F008; // southeast
// Drags the entire panel ...
MoveIt = $F012;
procedure TForm1.QRRichText1MouseMov e(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
const
Edge=15; // how close to the edge do we get before saying we're "on" it? (in pixels)
Corner=20; // the corners are a bit harder to detect, so we add some tolerance
var
P:TPoint;
re_align:TAlign;
begin
if Sender is TQRRichText then
with Sender as TQRRichText do
begin
re_align := Align;
Align := alNone;
// Create a TPoint from from the mouse coordinates passed to the function...
P:=point(X,Y);
Form1.Caption:=IntToStr(x) +' '+inttostr(y)+' '+(sender as TQRRichText).Name;
// All this if-then-else stuff decides which constant will be passed to WM_SYSCOMMAND
// Here we just use some math to create a small square at each corner,
// then check to see if the mouse is within any them...
if FEnableSizing then
begin
if ptInRect(Rect(0,0,Corner,C orner),P) then fSysCmd:=SizeNW
else if ptInRect(Rect(0,Height-Cor ner,Corner ,Height),P ) then fSysCmd:=SizeSW
else if ptInRect(Rect(Width-Corner ,Height-Co rner,Width ,Height),P ) then fSysCmd:=SizeSE
else if ptInRect(Rect(Width-Corner ,0,Width,C orner),P) then fSysCmd:=SizeNE
// if it's not in a corner, then is it near an edge?
else if (X < Edge) then fSysCmd:=SizeW
else if ( X > (Width-Edge) ) then fSysCmd:=SizeE
else if (Y < Edge) then fSysCmd:=SizeN
else if ( Y > (Height-Edge) ) then fSysCmd:=SizeS
// if it's none of the above, then MouseDown should drag the whole panel...
else
if FEnableMoving then fSysCmd:=MoveIt
else fSysCmd := 0;
end
else
if FEnableMoving then fSysCmd:=MoveIt
else fSysCmd := 0;
// Now that we have the correct system command, we can use its
// value to determine which screen cursor to display...
case fSysCmd of
SizeE,SizeW:Cursor:=crSize WE;
SizeN,SizeS:Cursor:=crSize NS;
SizeNE,SizeSW:Cursor:=crSi zeNESW;
SizeNW,SizeSE:Cursor:=crSi zeNWSE;
MoveIt:if (ssLeft in Shift) then Screen.Cursor := crSizeAll
else begin
Screen.Cursor := crDefault;
Cursor := crDefault;
end;
else Cursor:=crDefault;
end;
if (ssLeft in Shift) then
begin
ReleaseCapture;
(Sender as TQRRichText).Perform(WM_Sy sCommand,f SysCmd,0); //<- And here's the heart of the whole component!
end;
Align := Re_align;
end;
end;
end.
//Here is code :
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, QRCTRLS,
QuickRpt;
type
TForm1 = class(TForm)
QRRichText1: TQRRichText;
private
{ Private declarations }
private
// variable to hold the value passed to WM_SYSCOMMAND...
fSysCmd:integer;
// variable to hold the "default" cursor, just in case it isn't "crDefault"
fSavedCursor:TCursor;
FEnableMoving: Boolean;
FEnableSizing: Boolean;
procedure QRRichText1MouseMove(Sende
Y: Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
// constants passed to WM_SYSCOMMAND
// For some reason, these are not listed in the help file,
// I had to play around a bit to find them...
const
SizeE = $F002; // east
SizeW = $F001; // west
SizeN = $F003; // north
SizeS = $F006; // south
SizeNW = $F004; // northwest
SizeNE = $F005; // northeast
SizeSW = $F007; // southwest
SizeSE = $F008; // southeast
// Drags the entire panel ...
MoveIt = $F012;
procedure TForm1.QRRichText1MouseMov
Y: Integer);
const
Edge=15; // how close to the edge do we get before saying we're "on" it? (in pixels)
Corner=20; // the corners are a bit harder to detect, so we add some tolerance
var
P:TPoint;
re_align:TAlign;
begin
if Sender is TQRRichText then
with Sender as TQRRichText do
begin
re_align := Align;
Align := alNone;
// Create a TPoint from from the mouse coordinates passed to the function...
P:=point(X,Y);
Form1.Caption:=IntToStr(x)
// All this if-then-else stuff decides which constant will be passed to WM_SYSCOMMAND
// Here we just use some math to create a small square at each corner,
// then check to see if the mouse is within any them...
if FEnableSizing then
begin
if ptInRect(Rect(0,0,Corner,C
else if ptInRect(Rect(0,Height-Cor
else if ptInRect(Rect(Width-Corner
else if ptInRect(Rect(Width-Corner
// if it's not in a corner, then is it near an edge?
else if (X < Edge) then fSysCmd:=SizeW
else if ( X > (Width-Edge) ) then fSysCmd:=SizeE
else if (Y < Edge) then fSysCmd:=SizeN
else if ( Y > (Height-Edge) ) then fSysCmd:=SizeS
// if it's none of the above, then MouseDown should drag the whole panel...
else
if FEnableMoving then fSysCmd:=MoveIt
else fSysCmd := 0;
end
else
if FEnableMoving then fSysCmd:=MoveIt
else fSysCmd := 0;
// Now that we have the correct system command, we can use its
// value to determine which screen cursor to display...
case fSysCmd of
SizeE,SizeW:Cursor:=crSize
SizeN,SizeS:Cursor:=crSize
SizeNE,SizeSW:Cursor:=crSi
SizeNW,SizeSE:Cursor:=crSi
MoveIt:if (ssLeft in Shift) then Screen.Cursor := crSizeAll
else begin
Screen.Cursor := crDefault;
Cursor := crDefault;
end;
else Cursor:=crDefault;
end;
if (ssLeft in Shift) then
begin
ReleaseCapture;
(Sender as TQRRichText).Perform(WM_Sy
end;
Align := Re_align;
end;
end;
end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can't add events to existing components. You should write your own component derived from existing or change source code of the component that was parent component for all components you want to change.
------
Igor.