Delphi 滾動條的使用介紹
1、DELPHI的滾動條默認發送消息格式:
function TControl.Perform(
Msg: Cardinal;
WParam: WPARAM;
LParam: LPARAM
): LRESULT;
//如:Memo1.Perform(WM_HSCROLL, SB_LEFT, 0);
2、水平/垂直滾動條:
2.1 水平滾動條 消息 WM_HSCROLL
SendMessage(Memo1.Handle, WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, 50), 0); //滾動至此 SendMessage(Memo1.Handle, WM_HSCROLL, SB_LEFT, 0); //左邊緣 SendMessage(Memo1.Handle, WM_HSCROLL, SB_RIGHT, 0); //右邊緣 SendMessage(Memo1.Handle, WM_HSCROLL, SB_PAGELEFT, 0); //向左翻頁 SendMessage(Memo1.Handle, WM_HSCROLL, SB_PAGERIGHT, 0); //向右翻頁 SendMessage(Memo1.Handle, WM_HSCROLL, SB_LINELEFT, 0); //向左滾動 SendMessage(Memo1.Handle, WM_HSCROLL, SB_LINERIGHT, 0); //向右滾動
2.2 垂直滾動條 消息 WM_VSCROLL
SendMessage(Memo1.Handle, WM_VSCROLL, MAKEWPARAM(SB_THUMBPOSITION, 50), 0); //滾動至此 50為位置 滾動到指定行號 SendMessage(Memo1.Handle, WM_VSCROLL, SB_TOP, 0); //頂部 SendMessage(Memo1.Handle, WM_VSCROLL, SB_BOTTOM, 0); //底部 SendMessage(Memo1.Handle, WM_VSCROLL, SB_PAGEUP, 0); //向上翻頁 SendMessage(Memo1.Handle, WM_VSCROLL, SB_PAGEDOWN, 0); //向下翻頁 SendMessage(Memo1.Handle, WM_VSCROLL, SB_LINEUP, 0); //向上滾動 SendMessage(Memo1.Handle, WM_VSCROLL, SB_LINEDOWN, 0); //向下滾動
3、獲得滾動條的位置(GetScrollPos函數)
GetScrollPos函數檢索指定滾動條中滾動框(拇指)的當前位置。當前位置是一個相對值,取決於當前滾動范圍。
例如,如果滾動范圍為0到100,並且滾動框位於條的中間,則當前位置為50。
int GetScrollPos(
HWND hWnd,
int nBar
);
示例:
var
h, v: Integer;
begin
h := GetScrollPos(Memo1.Handle, SB_HORZ);
v := GetScrollPos(Memo1.Handle, SB_VERT);
Caption := Format('水平數值=%d 垂直數值=%d', [h, v]);
end;
4、顯示和隱藏滾動條
ShowScrollBar(Memo1.Handle,SB_HORZ,false); //隱藏MEMO水平滾動條
ShowScrollBar(Memo1.Handle,SB_VERT,false); //隱藏MEMO垂直滾動條
5、判斷 滾動條是否出現
procedure TForm1.Button1Click(Sender: TObject);
begin
if (GetWindowlong(Memo1.Handle, GWL_STYLE) and WS_VSCROLL) > 0 then ShowMessage('垂直滾動條顯示');
if (GetWindowlong(Memo1.Handle, GWL_STYLE) and WS_HSCROLL) > 0 then ShowMessage('水平滾動條顯示');
end;
創建時間:2019.11.18 更新時間:2020.05.18