开发Delphi控件时,如何增加相应的事件。如:增加Clicked事件

解决方案 »

  1.   

    unit ExMemo;interfaceuses
      Windows, Messages, Classes, StdCtrls;type  TExtendedMemo = class(TMemo)
      private
        FRow: Longint;
        FColumn: Longint;
        FOnHScroll: TNotifyEvent;
        FOnVScroll: TNotifyEvent;
        procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
        procedure WMVScroll(var Msg: TWMVScroll); message WM_VSCROLL;
        procedure SetRow(Value: Longint);
        procedure SetColumn(Value: Longint);
        function GetRow: Longint;
        function GetColumn: Longint;
      protected
        // Event dispatching methods
        procedure HScroll; dynamic;
        procedure VScroll; dynamic;
      public
        property Row: Longint read GetRow write SetRow;
        property Column: Longint read GetColumn write SetColumn;
      published
        property OnHScroll: TNotifyEvent read FOnHScroll write FOnHScroll;
        property OnVScroll: TNotifyEvent read FOnVScroll write FOnVScroll;
      end;procedure Register;implementationprocedure TExtendedMemo.WMHScroll(var Msg: TWMHScroll);
    begin
      inherited;
      HScroll;
    end;procedure TExtendedMemo.WMVScroll(var Msg: TWMVScroll);
    begin
      inherited;
      VScroll;
    end;procedure TExtendedMemo.HScroll;
    { This is the OnHScroll event dispatch method. It checks to see
      if OnHScroll points to an event handler and calls it if it does. }
    begin
      if Assigned(FOnHScroll) then
        FOnHScroll(self);
    end;procedure TExtendedMemo.VScroll;
    { This is the OnVScroll event dispatch method. It checks to see
      if OnVScroll points to an event handler and calls it if it does. }
    begin
      if Assigned(FOnVScroll) then
        FOnVScroll(self);
    end;procedure TExtendedMemo.SetRow(Value: Longint);
    { The EM_LINEINDEX returns the character position of the first
      character in the line specified by wParam. The Value is used for
      wParam in this instance. Setting SelStart to this return value
      positions the caret on the line specified by Value. }
    begin
      SelStart := Perform(EM_LINEINDEX, Value, 0);
      FRow := SelStart;
    end;function TExtendedMemo.GetRow: Longint;
    { The EM_LINEFROMCHAR returns the line in which the character specified
      by wParam sits. If -1 is passed as wParam, the line number at which
      the caret sits is returned. }
    begin
      Result := Perform(EM_LINEFROMCHAR, -1, 0);
    end;procedure TExtendedMemo.SetColumn(Value: Longint);
    begin
      { Get the length of the current line using the EM_LINELENGTH
        message. This message takes a character position as WParam.
        The length of the line in which that character sits is returned. }
      FColumn := Perform(EM_LINELENGTH, Perform(EM_LINEINDEX, GetRow, 0), 0);
      { If the FColumn is greater than the value passed in, then set
        FColumn to the value passed in }
      if FColumn > Value then
        FColumn := Value;
      // Now set SelStart to the newly specified position
      SelStart := Perform(EM_LINEINDEX, GetRow, 0) + FColumn;
    end;function TExtendedMemo.GetColumn: Longint;
    begin
      { The EM_LINEINDEX message returns the line index of a specified
        character passed in as wParam. When wParam is -1 then it
        retuns the index of the curent line. Subtracting SelStart from this
        value returns the column position }
      Result := SelStart - Perform(EM_LINEINDEX, -1, 0);
    end;procedure Register;
    begin
      RegisterComponents('DDG', [TExtendedMemo]);
    end;end.
      

  2.   

    标准事件可用  TNotifyEvent 过程定义
    非标准事件得自己定义 过程类型后再定义事件
      

  3.   

    我用这个方法增加了一个OnClicked事件,可惜的是我没法子触发这个事件。问大哥们是否可以告诉我如何让他要以变成可触发的事件。
      

  4.   

    TYourControl = class(TCustomControl)
      private
        FOnYourEvent: TNotifyEvent;
        ...
      published
        ...
        property OnYourEvent: TNotifyEvent read FOnYourEventwrite FOnYourEvent;
    end;
      

  5.   

    如果你需要触发就用:
    if (FOnYourEvent!=nil)
         OnYourEvent(self);
    语法可能不对,c和pascal有点区分不开,大致意识是这样
      

  6.   

    例如你要响应WM_CHANGE消息,可以这样
    type TChangeEvent = procedure of object;TYourControl = class(TCustomControl)
      private
        FOnChange: TChangeEvent;
        procedure do_onchange(var msg:message); message WM_CHANGE;
        ...
      published
        ...
        property OnChange: TChangeEvent read FOnChange write FOnChange;
    end;
    ……
    procedure TYourControl.do_onchange(var msg:message); 
    begin
       if FOnChange<>nil then FOnChange;
    end;