我有一个VCL TMemo控件,每次滚动文本时都需要得到通知。没有OnScroll事件,滚动消息似乎没有传播到父窗体。
知道怎么收到通知吗?作为最后的手段,我可以放置一个外部TScrollBar并在OnScroll事件中更新TMemo,但是当我移动光标或在TMemo中滚动鼠标轮时,我必须保持它们的同步。
发布于 2013-12-09 08:19:40
您可以在运行时对备忘录的WindowProc属性进行子类,以捕获发送给备忘录的所有消息,例如:
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-09 05:33:54
您可以使用interposer类来处理WM_VSCROLL和WM_HSCROLL消息以及EN_VSCROLL和EN_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;https://stackoverflow.com/questions/20463253
复制相似问题