Solved

Add events

Posted on 2001-07-08
6
230 Views
Last Modified: 2010-04-06
Add events

How to add evetns to components ?
For example, events (OnClick and OnMouseMove) to all QuickReport components ?
0
Comment
Question by:cvbmn
6 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 6262236
Hi cvbmn,

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.
0
 

Author Comment

by:cvbmn
ID: 6262237
Can you give me some example ?
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6262244
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;


0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6262250
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

0
 

Author Comment

by:cvbmn
ID: 6262318
//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(Sender: 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.QRRichText1MouseMove(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,Corner),P) then fSysCmd:=SizeNW
 else if ptInRect(Rect(0,Height-Corner,Corner,Height),P) then fSysCmd:=SizeSW
 else if ptInRect(Rect(Width-Corner,Height-Corner,Width,Height),P) then fSysCmd:=SizeSE
 else if ptInRect(Rect(Width-Corner,0,Width,Corner),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:=crSizeWE;
 SizeN,SizeS:Cursor:=crSizeNS;
 SizeNE,SizeSW:Cursor:=crSizeNESW;
 SizeNW,SizeSE:Cursor:=crSizeNWSE;
 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_SysCommand,fSysCmd,0); //<- And here's the heart of the whole component!
end;
Align := Re_align;
end;
end;

end.
0
 
LVL 2

Accepted Solution

by:
vbmn earned 200 total points
ID: 6262586
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, QRCTRLS;

Type
 TEventQRRT = class(TQRRichText)
 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;

   // -> Bug fix (explained below)
   fClientControl:TWinControl;
   fClientMouseMove:TMouseMoveEvent;
 public
    CONSTRUCTOR Create(AOwner:TComponent); override;
 protected
 published
   property EnableMoving: Boolean read FEnableMoving write FEnableMoving;
   property EnableSizing: Boolean read FEnableSizing write FEnableSizing;
 end;


  TForm1 = class(TForm)
    procedure EventQRTT1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormShow(Sender: TObject);
  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;
  public

 end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$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;

CONSTRUCTOR TEventQRRT.Create(AOwner:TComponent);
begin
 inherited Create(AOwner);
 FEnableMoving := true;
 FEnableSizing := true;
end;

procedure TForm1.EventQRTT1MouseMove(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 TEventQRRT then
with Sender as TEventQRRT 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 TEventQRRT).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,Corner),P) then fSysCmd:=SizeNW
 else if ptInRect(Rect(0,Height-Corner,Corner,Height),P) then fSysCmd:=SizeSW
 else if ptInRect(Rect(Width-Corner,Height-Corner,Width,Height),P) then fSysCmd:=SizeSE
 else if ptInRect(Rect(Width-Corner,0,Width,Corner),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:=crSizeWE;
 SizeN,SizeS:Cursor:=crSizeNS;
 SizeNE,SizeSW:Cursor:=crSizeNESW;
 SizeNW,SizeSE:Cursor:=crSizeNWSE;
 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 TEventQRRT).Perform(WM_SysCommand,fSysCmd,0); //<- And here's the heart of the whole component!
end;
Align := Re_align;
end;

end;

procedure TForm1.FormShow(Sender: TObject);
var Test:TEventQRRT;
begin
Test :=TEventQRRT.Create(self);
Test.width:=200;
Test.Height:=200;
Test.parent:=Form1;
Test.OnMouseMove:=EventQRTT1MouseMove;
end;

end.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 221
Internet Explorer View Settings Question 15 116
Delphi: making a BW image transparent 10 60
MS Access from Delphi 31 60
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question