在 VCL 應用程式中,我試圖在TListView使用此插入器類代碼結束水平滾動時收到通知:
type
TListView = class(Vcl.ComCtrls.TListView)
private
procedure WMNotify(var AMessage: TWMNotify); message WM_NOTIFY; // used for other purposes
procedure CNCommand(var Message: TWMCommand); message CN_COMMAND;
procedure WMVScroll(var Msg: TWMHScroll); message WM_VSCROLL;
procedure WMHScroll(var Msg: TWMHScroll); message WM_HSCROLL;
protected
procedure CreateWnd; override;
end;
implementation
procedure TListView.CNCommand(var Message: TWMCommand);
begin
case Message.NotifyCode of
EN_VSCROLL: CodeSite.Send('TListView.CNCommand: EN_VSCROLL'); // does not work
EN_HSCROLL: CodeSite.Send('TListView.CNCommand: EN_HSCROLL'); // does not work
end;
inherited ;
end;
procedure TListView.WMHScroll(var Msg: TWMHScroll);
begin
CodeSite.Send('TListView.WMHScroll: WM_HSCROLL'); // does work
inherited;
end;
procedure TListView.WMVScroll(var Msg: TWMHScroll);
begin
CodeSite.Send('TListView.WMVScroll: WM_VSCROLL'); // does work
inherited;
end;
但是,只有在滾動時,我才會不斷收到通知WM_HSCROLL并WM_VSCROLL生成大量訊息。
但我只需要在水平滾動結束時收到通知!這可能嗎?
uj5u.com熱心網友回復:
給 Q 的評論非常相關。
首先,正如 Remy Lebeau 所說,WM_HSCROLL訊息會告訴您操作是否完成:
procedure TListView.WMHScroll(var Msg: TWMHScroll);
begin
inherited;
if Msg.ScrollCode = SB_ENDSCROLL then
ShowMessage('End scroll')
end;
但是,這只會讓您知道由水平滾動條啟動的滾動操作何時完成。目前,這包括以下滾動條操作:
- 拇指松開
- 單擊滾動條向左或向右按??鈕
- 單擊滾動條空白區域(用于頁面滾動)
- 已選擇滾動條背景關系選單項
但是串列視圖控制元件還有很多其他的滾動方式,與滾動條無關:
- 使用滑鼠的水平滾輪(如果只有水平滾動條而沒有垂直滾動條,則使用標準垂直滾輪)
- 使用鍵盤的左右箭頭鍵(或 Ctrl Left/Right 進行頁面滾動)
- 使用
MultiSelect = True, 用滑鼠創建一個選擇矩形(開始拖動到任何串列視圖項之外)
因此,僅對 做出反應WM_HSCROLL,您將不會檢測到這些滾動事件。幾乎可以肯定,您想在滾動位置發生變化時做出反應,無論它是如何變化的。
而且,正如 AmigoJack 所寫,“結束”的含義并不完全清楚(除了在拖動滾動條拇指后釋放滑鼠按鈕時)。例如,如果您使用滑鼠滾輪滾動,結果是一個大滾動操作還是幾個小滾動操作?畢竟,在任何情況下,即使是拇指跟蹤,控制元件都會在每一個小步驟中重新繪制自己。
所以可能你最好的選擇是使用
procedure TListView.CNNotify(var Message: TMessage);
begin
inherited;
if PNMHDR(Message.lParam).code = LVN_ENDSCROLL then
// Scrolled
end;
根據檔案,
Notifies a list-view control's parent window when a scrolling operation ends.
Notice that the documentation says that the notification is sent when the operation ends. Still, you will find that it is sent for every small update while you drag the scroll bar thumb. As mentioned above, this is reasonable: scrolling has indeed been performed after every such small step.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/424510.html
