4

我有一个 VCLTMemo控件,每次滚动文本时都需要通知。没有OnScroll事件,滚动消息似乎没有传播到父表单。

知道如何获得通知吗?作为最后的手段,我可​​以放置一个外部TScrollBar并更新TMemo事件OnScroll,但是当我移动光标或滚动鼠标滚轮时我必须保持它们同步TMemo......

4

2 回答 2

3

您可以使用插入器类来处理WM_VSCROLLWM_HSCROLL消息以及 EN_VSCROLLEN_HSCROLL通知代码(通过 WM_COMMAND 消息公开)。

试试这个样本

type
  TMemo  = class(Vcl.StdCtrls.TMemo)
  private
   procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
   procedure WMVScroll(var Msg: TWMHScroll); message WM_VSCROLL;
   procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
  end;

  TForm16 = class(TForm)
    Memo1: TMemo;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form16: TForm16;

implementation

{$R *.dfm}


{ TMemo }

procedure TMemo.CNCommand(var Message: TWMCommand);
begin
   case Message.NotifyCode of
    EN_VSCROLL : OutputDebugString('EN_VSCROLL');
    EN_HSCROLL : OutputDebugString('EN_HSCROLL');
   end;

   inherited ;
end;

procedure TMemo.WMHScroll(var Msg: TWMHScroll);
begin
   OutputDebugString('WM_HSCROLL') ;
   inherited;
end;

procedure TMemo.WMVScroll(var Msg: TWMHScroll);
begin
   OutputDebugString('WM_HSCROLL') ;
   inherited;
end;
于 2013-12-09T05:33:54.037 回答
2

You can subclass the Memo's WindowProc property at runtime to catch all of messages sent to the Memo, eg:

private:
    TWndMethod PrevMemoWndProc;
    void __fastcall MemoWndProc(TMessage &Message);

__fastcall TMyForm::TMyForm(TComponent *Owner)
    : TForm(Owner)
{
    PrevMemoWndProc = Memo1->WindowProc;
    Memo1->WindowProc = MemoWndProc;
}

void __fastcall TMyForm::MemoWndProc(TMessage &Message)
{
    switch (Message.Msg)
    {
        case CN_COMMAND:
        {
            switch (reinterpret_cast<TWMCommand&>(Message).NotifyCode)
            {
                case EN_VSCROLL:
                {
                    //...
                    break;
                }

                case EN_HSCROLL:
                {
                    //...
                    break;
                }
            }

            break;
        }

        case WM_HSCROLL:
        {
            //...
            break;
        }

        case WM_VSCROLL:
        {
            //...
            break;
        }
    }

    PrevMemoWndProc(Message);
}
于 2013-12-09T08:19:40.910 回答