简体   繁体   中英

win32, scrollwindowex(): How to display back the up area from “off window” that disappeared after scrolling down?

My application's main window is starting to have lots of stuff so I need a vertical scrollbar to fit everything inside client area. I coded a scrollbar control, WM_VSCROLL messages like SB_LINEDOWN are being processed and scrollbar moves nicely. The last part is to make the content of a main window to move along with the thumb of a scrollbar and it seems a bit hard task for me. This is my best try:

                int dy = -( CurrPos - si.nPos );
                RECT rctMainWindowArea = { 0, 0, 1000, main_window.bottom };
                ScrollWindowEx( hwndMainWindow, 0, dy,( CONST RECT * ) &rctMainWindowArea,( CONST RECT * ) NULL,( HRGN ) NULL,( LPRECT ) NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE );
                UpdateWindow( hwndMainWindow );

It works as long I'm scrolling down. When I scroll back up again everything gets messed up. I've been googling about this issue for a while and it seems that I have to redraw the lost client area of main window. However I have no idea how to do it. I've found on the web only examples where text is being scrolled inside edit control. I need to scroll whole main window which has couple of different basic controls, some bmp graphic, some other graphic elements like TextOut(), RoundRect() and so on.

I need some code examples how to solve my issue or at least some simple explanation (I'm amateur programmer). Thanks a lot !

Windows doesn't keep track of how much the window has scrolled, so when it asks you to repaint part of the window, you need to change what you paint based on how much scrolling you've done.

The easiest way to do this is to adjust the window origin to match the amount of scrolling you've done. Your WM_PAINT handler might look something like this. offsetX and offsetY are the distances you've scrolled in the X and Y directions respectively.

// Adjust coordinates to automatically scroll
POINT origin;
GetWindowOrgEx(hdc, &origin);
SetWindowOrgEx(hdc, origin.x + offsetX, origin.y + offsetY, 0);

// Move the paint rectangle into the new coordinate system
OffsetRect(&ps.rcPaint, offsetX, offsetY);

// Do the painting
// Change this to call your painting function
CWindow::DoPaint(hdc, ps);

// Restore coordinates
SetWindowOrgEx(hdc, origin.x, origin.y, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM