TAlarmMng = class(TObject)
  private
    Timer:TTimer;
  public
    constructor Create(AOwner: TComponent); 
    destructor Destroy; override;
    function Alarm(ATime:TdateTime):boolean;
    procedure ShowMsg(Sender: TObject);
  end;
function TAlarmMng.Alarm( ATime:TdateTime): boolean;
var
  Interval:Int64;
  Hour,Min,Sec,MSec:word;
begin
   self.Timer.Interval:=2000;
   self.Timer.Enabled:=true;
   self.Timer.OnTimer:=self.ShowMsg;
end;constructor TAlarmMng.Create(AOwner: TComponent);
begin
  inherited Create;
  self.Timer:=TTimer.Create(AOwner);
  self.Timer.Enabled:=false;
end;destructor TAlarmMng.Destroy;
begin  inherited;
end;procedure TAlarmMng.ShowMsg(Sender: TObject);
begin
  showmessage('你有新的消息');
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  AlarmMng:=TAlarmMNg.Create(self);
  AlarmMng.Alarm(now);
end;
什么反应都没有。
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  AlarmMng:=TAlarmMNg.Create(self);
  AlarmMng.ShowMsg(nil);
end;
可以显示信息框:‘你有新的消息’请问这是什么原因?

解决方案 »

  1.   

    你想写个事件代码。不是这样写的。事件是方法的指针。在属性里调用。//delphi 6开发人员指南,些的事件代码。
    //控件单元。
    {
    Copyright ?1999 by Delphi 5 Developer's Guide - Xavier Pacheco and Steve Teixeira
    }unit halfmin;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls,
      Forms, Dialogs, ExtCtrls;type
      { Define a procedure for the event handler. The event property will
        be of this procedure type. This type will take two parameters, the
        object that invoked the event and a TDateTime value to represent
        the time that the event occurred. For our component this will be
        every half-minute. }
      TTimeEvent = procedure(Sender: TObject; TheTime: TDateTime) of object;  TddgHalfMinute = class(TComponent)
      private
        FTimer: TTimer;
        { Define a storage field to point to the user's event handler.
          The user's event handler must be of the procedural type
          TTimeEvent. }
        FOnHalfMinute: TTimeEvent;
        FOldSecond, FSecond: Word; // Variables used in the code
        { Define a procedure, FTimerTimer that will be assigned to
          FTimer.OnClick. This procedure must be of the type TNotifyEvent
          which is the type of TTimer.OnClick. }
        procedure FTimerTimer(Sender: TObject);
      protected
        { Define the dispatching method for the OnHalfMinute event. }
        procedure DoHalfMinute(TheTime: TDateTime); dynamic;
      public
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        // Define the actual property that will show in the Object Inspector 
        property OnHalfMinute: TTimeEvent read FOnHalfMinute write FOnHalfMinute;
      end;implementationconstructor TddgHalfMinute.Create(AOwner: TComponent);
    { The Create constructor, creates the TTimer instanced for FTimer. It
      then sets up the various properties of FTimer, including its OnTimer
      event handler which is TddgHalfMinute's FTimerTimer() method. Notice
      that FTimer.Enabled is set to true only if the component is running
      and not while the component is in design mode. }
    begin
      inherited Create(AOwner);
      // If the component is in design mode, do not enable FTimer.
      if not (csDesigning in ComponentState) then
      begin
        FTimer := TTimer.Create(self);
        FTimer.Enabled := True;
        // Set up the other properties, including the FTimer.OnTimer event handler
        FTimer.Interval := 500;
        FTimer.OnTimer := FTimerTimer;
       end;
    end;destructor TddgHalfMinute.Destroy;
    begin
      FTimer.Free;
      inherited Destroy;
    end;procedure TddgHalfMinute.FTimerTimer(Sender: TObject);
    { This method serves as the FTimer.OnTimer event handler and is assigned
      to FTimer.OnTimer at run-time in TddgHalfMinute's constructor.  This method gets the system time, and then determines whether or not
      the time is on the minute, or on the half-minute. If either of these
      conditions are true, it calls the OnHalfMinute dispatching method,
      DoHalfMinute. }
    var
      DT: TDateTime;
      Temp: Word;
    begin
      DT := Now; // Get the system time.
      FOldSecond := FSecond; // Save the old second.
      // Get the time values, needed is the second value
      DecodeTime(DT, Temp, Temp, FSecond, Temp);  { If not the same second when this method was last called, and if
        it is a half minute, call DoOnHalfMinute. }
      if FSecond <> FOldSecond then
        if ((FSecond = 30) or (FSecond = 0)) then
          DoHalfMinute(DT)
    end;procedure TddgHalfMinute.DoHalfMinute(TheTime: TDateTime);
    { This method is the dispatching method for the OnHalfMinute event.
      it checks to see if the user of the component has attached an
      event handler to OnHalfMinute and if so, calls that code. }
    begin
      if Assigned(FOnHalfMinute) then
        FOnHalfMinute(Self, TheTime);
    end;end.//调用代码procedure TForm1.ddgHalfMinute(sender;TheTime:TDateTime);
    begin
      ShowMessage('The Time Is '+TimeToSte
    end;
      

  2.   

    //靠,按下Cap键,突然键盘不能动。//调用代码procedure TForm1.ddgHalfMinute(sender;TheTime:TDateTime);
    begin
      ShowMessage('The Time Is '+TimeToStr(TheTime));
    end;