Link to home
Start Free TrialLog in
Avatar of mtieland
mtieland

asked on

component writing: events how to?

Can anyone explain me in detail how te create costom and non-costom events in a component?
For instance I want to make a RAS component that triggers an event when a connection is made...
Avatar of BoRiS
BoRiS

mtieland

here is one way to do it....

private
 FOnConnect: TNotifyEvent;
 procedure Connected(Value: Boolean)

published
 property OnConnect: TNotifyEvent read FOnConnect write FOnConnect;

then in the code...

procedure TRasConnection.Connected(Vaule: Boolean);
begin
   if value <> FOnConnected then FOnConnected := value;
    if Value then
     begin
      ShowMessage('You have Connected');
        if Assigned (FOnConnected) then FOnConnected(Self);
 end;
end;

I have not tested this code, I just wrote here quickly to give you the basic idea of the Events structure, this may or may not work(the code)but you can get the idea...

Later
BoRiS
mtieland

sorry replace this (every where you find it)
 FOnConnect

with this
 
FOnconnected

Later
BoRiS
Avatar of mtieland

ASKER

Can you give me an example how to make an event that responds to a windows api event (There should be a api event that is triggerd when ras is connected)

Thanx
mtieland

use the same as above for API then just call the API in the event handler...

procedure TRasConnection.Connected(Vaule: Boolean);
       begin
          if value <> FOnConnected then FOnConnected := value;
           if Value then
            begin
             ShowMessage('You have Connected');
              RasGetConnection(blahblah);
               RasGetModemStatus(blahblah);
               if Assigned (FOnConnected) then FOnConnected(Self);
        end;
       end;

just remeber to add the correct files to the uses clause...
uses
Windows, Forms, ..., winsock;

Later
BoRiS
What if the API function already is an event?
What I mean is can you enscapulate a already existing system event into your own component...?
Yes you can!

Every event is build on the windows messagesystem provided with a specific code like CM_MOUSEENTER. The prettiest way to do this is like this:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FOnMouseEnter: TNotifyEvent;
    FOnMouseLeave: TNotifyEvent;
    Procedure CMMouseLeave(var Msg: TMessage); Message CM_MOUSELEAVE;
    Procedure CMMouseEnter(var Msg: TMessage); Message CM_MOUSEENTER;
  public
    //Test procedures
    procedure OnMyMouseEnter(Sender: TObject);
    procedure OnMyMouseLeave(Sender: TObject);
  //published
    //If you are to write components, this section should be published
    property OnMouseEnter: TNotifyEvent read FOnMouseEnter write FOnMouseEnter;
    property OnMouseLeave: TNotifyEvent read FOnMouseLeave write FOnMouseLeave;

  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Procedure TForm1.CMMouseEnter(var Msg: TMessage);
Begin
  If Assigned(FOnMouseEnter) then FOnMouseEnter(Self);
  inherited
End;

Procedure TForm1.CMMouseLeave(var Msg: TMessage);
Begin
  If Assigned(FOnMouseLeave) then FOnMouseLeave(Self);
  inherited
End;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OnMouseEnter:= OnMyMouseEnter;
  OnMouseLeave:= OnMyMouseLeave;
end;

procedure TForm1.OnMyMouseEnter(Sender: TObject);
begin
  Label1.caption:= 'Inside!';
end;

procedure TForm1.OnMyMouseLeave(Sender: TObject);
begin
  Label1.caption:= 'Outside!';
end;


end.
Please note:

The procedure:

    Procedure CMMouseLeave(var Msg: TMessage); Message CM_MOUSELEAVE;

Needs to be named like the message_id, else it won't work.

Regards,
Williams
Thanx Williams please perpose your comment as an answer to get the points!

Martijn
ASKER CERTIFIED SOLUTION
Avatar of williams2
williams2

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