スクロールバーの使い方

1.ウィンドウにスクロールバーをつける方法

CreateWindow() の dwStyle に、WS_HSCROLL(垂直)、WS_VSCROLL(水平) を指定する。

2.スクロール範囲の設定

SetScrollInfo()を使用
SCROLLINFO scInfo;
ZeroMemory(&scInfo, sizeof(SCROLLINFO));
scInfo.cbSize = sizeof(SCROLLINFO);  // サイズを必ず渡す
scInfo.fMask = SIF_RANGE             // nMin, nMaxでスクロール範囲を設定する
       | SIF_DISABLENOSCROLL  // スクロールの必要がないときには、スクロールバーをDISABLEにする(デフォルトは消す)
scInfo.nMin = 0;
scInfo.nMax = 100;
 

3.メッセージ通知

WM_HSCROLL (垂直)
WM_VSCROLL(水平)

lParam: スクロールバーがついているウィンドウのハンドル
LOWPRD(wParam):通知コード
HIWORD(wParam):位置

通知コード一覧
SB_ENDSCROLL
Ends scroll.
SB_LEFT
Scrolls to the upper left.
SB_RIGHT
Scrolls to the lower right.
SB_LINELEFT
Scrolls left by one unit.
SB_LINERIGHT
Scrolls right by one unit.
SB_PAGELEFT
Scrolls left by the width of the window.
SB_PAGERIGHT
Scrolls right by the width of the window.
SB_THUMBPOSITION
The user has dragged the scroll box (thumb) and released the mouse button. The high-order word indicates the position of the scroll box at the end of the drag operation.
SB_THUMBTRACK
The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The high-order word indicates the position that the scroll box has been dragged to.

※ 位置の通知は16bit範囲でしか行われない。32bit範囲での値を取得する場合には、以下を使用する。

4.スクロール位置の取得(32bit)

WM_HSCROLL, WB_VSCROLLのメッセージ処理中に以下の処理で、移動先の位置が取れる。(通知コードは問わない)
SCROLLINFO scCurInfo = {0};
 
	scCurInfo.fMask = SIF_TRACKPOS;
	scCurInfo.cbSize = sizeof(SCROLLINFO);
	::GetScrollInfo(this->m_hWndMain, SB_HORZ, &scCurInfo);
	// scCurInfo.nTrackPos に移動先の位置が格納される
 

参考URL:

タグ:

Win32
最終更新:2010年07月30日 07:59