Win32_滚动条

一 滚动条

  1 滚动条样式
    1.1 水平滚动条 SBS_HORZ
    1.2 垂直滚动条 SBS_VERT

  2 滚动条的使用
    2.1 滚动条的创建
      窗口类名称 SCROLLBAR
    2.2 消息处理
      1) 水平滚动条 WM_HSCROLL
        LOWORD(wParam) - 滚动的标示
        HIWORD(wParam) - 当前滑块的位置
        lParam - 窗口的句柄
      2)垂直滚动条 WM_VSCROLL
        LOWORD(wParam) - 滚动的标示
        HIWORD(wParam) - 当前滑块的位置
        lParam - 窗口的句柄
      3)HIWORD(wParam)只在 SB_THUMBPOSITION/SB_THUMBTRACK时有效

    2.3 滚动条的滚动范围
      SBM_SETRANGE 设置滚动条的滚动范围
        wParam - 最小值,起始值
        lParam - 最大值,终止值
      SBM_GETRANGE 获取滚动条的滚动范围
        wParam - 最小值存放的INT的地址
        lParam - 最大值存放的INT的地址
    2.4 滑块位置的设置和获取
      SBM_SETPOS 设置滑块位置
        wParam - 新的滑块位置
        lParam  - 重新绘制窗口
      SBM_GETPOS 获取返回当前位置

View Code
  1 // WinScrollBar.cpp : Defines the entry point for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 
  6 HINSTANCE g_hInst = NULL;
  7 HWND      g_hHorz = NULL;
  8 HWND      g_hVert = NULL;
  9 void OnCreate( HWND hWnd, WPARAM wParam,
 10     LPARAM lParam )
 11 {    //创建滚动条
 12     g_hHorz = CreateWindow( "SCROLLBAR", "HORZ",
 13         WS_CHILD|WS_VISIBLE|SBS_HORZ,
 14 , 300, 300, 30, hWnd,
 15         (HMENU)1001, g_hInst, NULL );
 16     g_hVert = CreateWindow( "SCROLLBAR", "VERT",
 17         WS_CHILD|WS_VISIBLE|SBS_VERT,
 18 , 0, 30, 300, hWnd,
 19         (HMENU)1002, g_hInst, NULL );
 20     //设置滚动范围
 21     SendMessage( g_hHorz, SBM_SETRANGE, 0, 300 );
 22     SendMessage( g_hVert, SBM_SETRANGE, 0, 300 );
 23 
 24 }
 25 void OnHScroll( HWND hWnd, WPARAM wParam,
 26     LPARAM lParam )
 27 {
 28     int nScrollCode = LOWORD( wParam );
 29     int nPos        = HIWORD( wParam ); 
 30     switch( nScrollCode )
 31     {
 32     case SB_LINELEFT: //左边箭头
 33         nPos = SendMessage( g_hHorz, SBM_GETPOS,
 34 , 0 );
 35         nPos--;
 36         SendMessage( g_hHorz, SBM_SETPOS, 
 37             nPos, TRUE );
 38         break;
 39     case SB_LINERIGHT: //右边箭头
 40         nPos = SendMessage( g_hHorz, SBM_GETPOS,
 41 , 0 );
 42         nPos++;
 43         SendMessage( g_hHorz, SBM_SETPOS, 
 44             nPos, TRUE );
 45         break;
 46     case SB_PAGELEFT: //滑块左边的空白区域
 47         nPos = SendMessage( g_hHorz, SBM_GETPOS,
 48 , 0 );
 49         nPos -= 10;
 50         SendMessage( g_hHorz, SBM_SETPOS, 
 51             nPos, TRUE );
 52         break;
 53     case SB_PAGERIGHT://滑块右边的空白区域
 54         nPos = SendMessage( g_hHorz, SBM_GETPOS,
 55 , 0 );
 56         nPos += 10;
 57         SendMessage( g_hHorz, SBM_SETPOS, 
 58             nPos, TRUE );
 59         break;
 60     case SB_THUMBPOSITION://拖拽滑块后
 61         SendMessage( g_hHorz, SBM_SETPOS,
 62             nPos, TRUE );
 63         break;
 64     }
 65 
 66     InvalidateRect( hWnd, NULL, TRUE );
 67 }
 68 
 69 void OnVScroll( HWND hWnd, WPARAM wParam,
 70     LPARAM lParam )
 71 {
 72     int nScrollCode = LOWORD( wParam );
 73     int nPos        = HIWORD( wParam ); 
 74     switch( nScrollCode )
 75     {
 76     case SB_LINEUP: //上边箭头
 77         nPos = SendMessage( g_hVert, SBM_GETPOS,
 78 , 0 );
 79         nPos--;
 80         SendMessage( g_hVert, SBM_SETPOS, 
 81             nPos, TRUE );
 82         break;
 83     case SB_LINEDOWN://下边箭头
 84         nPos = SendMessage( g_hVert, SBM_GETPOS,
 85 , 0 );
 86         nPos++;
 87         SendMessage( g_hVert, SBM_SETPOS, 
 88             nPos, TRUE );
 89         break;
 90     case SB_PAGEUP://滑块上边的空白区域
 91         nPos = SendMessage( g_hVert, SBM_GETPOS,
 92 , 0 );
 93         nPos -= 10;
 94         SendMessage( g_hVert, SBM_SETPOS, 
 95             nPos, TRUE );
 96         break;
 97     case SB_PAGEDOWN://滑块下边的空白区域
 98         nPos = SendMessage( g_hVert, SBM_GETPOS,
 99 , 0 );
100         nPos += 10;
101         SendMessage( g_hVert, SBM_SETPOS, 
102             nPos, TRUE );
103         break;
104     case SB_THUMBPOSITION://拖拽滑块后
105         SendMessage( g_hVert, SBM_SETPOS,
106             nPos, TRUE );
107         break;
108     }
109 
110     InvalidateRect( hWnd, NULL, TRUE );
111 }    
112 
113 void OnPaint( HWND hWnd, WPARAM wParam,
114     LPARAM lParam )
115 {
116     PAINTSTRUCT ps = { 0 };
117     HDC hDC = BeginPaint( hWnd, &ps );
118     
119     int nVert = SendMessage( g_hVert, 
120         SBM_GETPOS, 0, 0 );
121     int nHorz = SendMessage( g_hHorz, 
122         SBM_GETPOS, 0, 0 );
123     
124     CHAR szText[] = "Hello ScroolBar";
125     TextOut( hDC, 150 - nHorz, 150 - nVert, 
126         szText, strlen(szText) );
127 
128     EndPaint( hWnd, &ps );
129 }
130 
131 LRESULT CALLBACK WndProc( HWND   hWnd, 
132                           UINT   nMsg,
133                           WPARAM wParam,
134                           LPARAM lParam )
135 {
136     switch( nMsg )
137     {
138     case WM_PAINT:
139         OnPaint( hWnd, wParam, lParam );
140         break;
141     case WM_CREATE:
142         OnCreate( hWnd, wParam, lParam );
143         break;
144     case WM_HSCROLL:
145         OnHScroll( hWnd, wParam, lParam );
146         break;
147     case WM_VSCROLL:
148         OnVScroll( hWnd, wParam, lParam );
149         break;
150     case WM_DESTROY:
151         PostQuitMessage( 0 );
152         return 0;
153     }
154     return DefWindowProc( hWnd, nMsg,
155         wParam, lParam );
156 }
157 
158 BOOL RegisterWnd( LPSTR pszClassName )
159 {
160     WNDCLASSEX wce = { 0 };
161     wce.cbSize        = sizeof( wce );
162     wce.cbClsExtra    = 0;
163     wce.cbWndExtra    = 0;
164     wce.hbrBackground = HBRUSH(COLOR_WINDOW);
165     wce.hCursor       = NULL;
166     wce.hIcon         = NULL;
167     wce.hIconSm       = NULL;
168     wce.hInstance     = g_hInst;
169     wce.lpfnWndProc   = WndProc;
170     wce.lpszClassName = pszClassName;
171     wce.lpszMenuName  = NULL;
172     wce.style         = CS_HREDRAW|CS_VREDRAW;
173 
174     ATOM nAtom = RegisterClassEx( &wce );
175     if( 0 ==  nAtom )
176     {
177         return FALSE;
178     }
179 
180     return TRUE;
181 }
182 
183 HWND CreateWnd( LPSTR pszClassName )
184 {
185     HWND hWnd = CreateWindowEx( 0,
186         pszClassName, "MyWnd", 
187         WS_OVERLAPPEDWINDOW|WS_HSCROLL|WS_VSCROLL,
188         CW_USEDEFAULT,
189         CW_USEDEFAULT, CW_USEDEFAULT,
190         CW_USEDEFAULT, NULL, NULL, g_hInst,
191         NULL );
192     return hWnd;
193 }
194 
195 void DisplayWnd( HWND hWnd )
196 {
197     ShowWindow( hWnd, SW_SHOW );
198     UpdateWindow( hWnd );
199 }
200 
201 void Message( )
202 {
203     MSG msg = { 0 };
204     while( GetMessage( &msg, NULL, 0, 0 ) )
205     {
206         TranslateMessage( &msg );
207         DispatchMessage( &msg );
208     }
209 }
210 
211 int APIENTRY WinMain(HINSTANCE hInstance,
212                      HINSTANCE hPrevInstance,
213                      LPSTR     lpCmdLine,
214                      int       nCmdShow)
215 {
216     g_hInst = hInstance;
217     RegisterWnd( "MYWND" );
218     HWND hWnd = CreateWnd( "MYWND" );
219     DisplayWnd( hWnd );
220     Message( );
221     return 0;
222 }

 

posted @ 2012-10-13 11:09  吟唱黑暗  阅读(757)  评论(0编辑  收藏  举报