一派護法 十九級 |
ScrollWindow(hWnd, 水平滚动量, 垂直滚动量, NULL, NULL); UpdateWindow(hWnd);
当水平滚动量为负时向左滚动,为正时向右滚动。 垂直滚动量为负时向上滚动,为正时向下滚动。 滚动后最好调用UpdateWindow函数立即执行WM_PAINT。虽然不调用这个函数最终WM_PAINT也会执行,但是系统会拖到所有其他消息都处理完毕了才执行,有时会影响用户体验。 第三、四个参数一般为NULL,即滚动整个窗口。
滚动后,系统会自动将暴露出来的空白部分交给WM_PAINT绘制。
如果使用Ex版本的话,一般使用: ScrollWindowEx(hWnd, -40, 0, NULL, NULL, NULL, NULL, SW_INVALIDATE);
|
一派護法 十九級 |
【示例程序】 #include <tchar.h> #include <Windows.h>
BITMAP bmp; HBITMAP hbmp;
int nScrolled = 0;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { HDC hdc, hdcMem; PAINTSTRUCT ps;
switch (uMsg) { case WM_CHAR: if (wParam == '+') { nScrolled = 1; //ScrollWindowEx(hWnd, -40, 0, NULL, NULL, NULL, NULL, SW_INVALIDATE); ScrollWindow(hWnd, -40, 0, NULL, NULL); UpdateWindow(hWnd); } else if (wParam == '-') { nScrolled = 2; ScrollWindow(hWnd, +40, 0, NULL, NULL); UpdateWindow(hWnd); } break; case WM_CREATE: hbmp = (HBITMAP)LoadImage(NULL, TEXT("img21.bmp"), IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE); GetObject(hbmp, sizeof(bmp), &bmp); break; case WM_DESTROY: DeleteObject(hbmp); PostQuitMessage(0); break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); if (nScrolled > 0) { HBRUSH hbr; RECT rcClient; GetClientRect(hWnd, &rcClient); if (nScrolled == 1) hbr = CreateSolidBrush(RGB(0, 0, 255)); else hbr = CreateSolidBrush(RGB(0, 255, 0)); FillRect(hdc, &rcClient, hbr); DeleteObject(hbr); } else { hdcMem = CreateCompatibleDC(hdc); SelectObject(hdcMem, hbmp); BitBlt(hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY); DeleteDC(hdcMem); } EndPaint(hWnd, &ps); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return FALSE; }
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbClsExtra = wcex.cbWndExtra = 0; wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wcex.hInstance = hInstance; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = TEXT("Scrolling Demo Window"); wcex.lpszMenuName = NULL; wcex.style = NULL; RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(wcex.lpszClassName, TEXT("Scrolling Demo"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }
|
一派護法 十九級 |
// 返回值表示操作是否成功 BOOL ScrollDC(HDC hDC, int dx, int dy, const RECT *lprcScroll, const RECT *lprcClip, HRGN hrgnUpdate, LPRECT lprcUpdate); BOOL ScrollWindow(HWND hWnd, int XAmount, int YAmount, const RECT *lpRect, const RECT *lpClipRect);
// 返回值: 暴露出据矩形区域SIMPLEREGION,非矩形区域COMPLEXREGION,无暴露区域NULLREGION;操作失败ERROR int ScrollWindowEx(HWND hWnd, int dx, int dy, const RECT *prcScroll, const RECT *prcClip, HRGN hrgnUpdate, LPRECT prcUpdate, UINT flags);
|