Image smoothing rolling effect VC implementation
Preface:
In the programming programming, this is often encountered, and a large picture is displayed in a limited area, then the portions of the image are viewed.
This requires the scrolling of the image. Regarding its implementation, many books have mentioned, but the key points and difficulties, that is, the refresh and flash in the drag
The problem, but there is not much, this is also the purpose of I write this article, and I will analyze the implementation method in detail.
Implementation and implementation methods:
Press the left mouse button in the image area to drag the image arbitrarily rolled in a limited area.
The method is: Calculate the offset of this time when dragging, and then changing the starting point of the image display and refreshes the image area.
Implementation part:
The first step: respond to the WM_LBUTTONDOWN message, the record is pressed to start dragging the starting position.
Void cwingimgdlg :: ONLBUTTONDOWN (uint nflags, cpoint point)
{
// Todo: add your message handler code here and / or call default
M_LPicoldLeft = Point.x;
m_lpicoldtop = point.y;
CDIALOG :: ONLBUTTONDOWN (NFLAGS, POINT);
}
Step 2: Respond to the WM_MOUSEMOVE message to implement scrolling.
Void CwingImgdlg :: OnMousemove (uint nflags, cpoint point)
{
// Todo: add your message handler code here and / or call default
File: // If the mouse is pressed
IF ((NFLAGS & MK_LBUTTON) == mk_lbutton)
{
M_LPICNEWLEFT = POINT.X;
M_LPICNEWTOP = POINT.Y;
DWORD DWLRSHIFT = M_LPICNEWLEFT - M_LPICOLDLEFT;
DWORD DWTBSHIFT = M_LPICNEWTOP - M_LPICOLDTOP;
File: / / Change the starting point of the image display
M_LPicleft = m_lpicleft - dwlrshift;
M_LPictop = M_LPictop - DWTBSHIFT;
File: // Judgment the statement of the boundary, save.
M_LPicoldLeft = M_LPICNEWLEFT;
M_LPicoldTop = M_LPICNEWTOP;
File: // For refreshing statements, see the fourth step.
}
CDIALOG :: OnMousemove (NFLAGS, POINT);
}
Step 3: Displayed in onpain, other parts of the display, such as the image of the image, etc., save.
Void cwingimgdlg :: onpaint ()
{
CPAINTDC DC (this); // Device Context for Painting
File: // Other display content, save.
IF (M_Pimginfo! = NULL)
DC.Bitblt (m_wshowadjleft, m_wshowadjtop, m_lwidth, m_lheight, & m_adjdc,
M_LPicleFT, M_LPictop, Srccopy;
CDIALOG :: onpaint ();
} The fourth step: refresh processing.
The most often thought of, of course, INVALIDATE (TRUE); // Refresh the entire invalid area
UpdateWindow ();
At this time, it will refresh the invalid zone of the entire program area, and the flaslen is very serious, the correction is as follows:
INVALIDATERECT (& M_RTPIC, TRUE); file: // Refresh only image display area
UpdateWindow ();
At this time, only the area where the image is refreshed, the flash is alleviated, and then further, can be used
INVALIDATERECT (& M_RTPIC, TRUE); file: // Using fast redraw
RedRawwindow (& M_RTPIC, NULL, RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW |
RDW_ERASE;
Performance, the flashing is further reduced, taken into account, other parts may affect the refresh area, simply use OnPaint () directly here, ie
Change to:
OnPaint ();
But if there is a lot of drawing statements in onPaint (), this method is still not feasible, considering the factor and control of ONPAINT () cannot be stimulated.
Refresh range, I use the following non-standard method to solve, the code is as follows:
CDC * PDC;
PDC = GetDC ();
IF (M_Pimginfo! = NULL)
PDC-> Bitblt (m_wshowleft, m_wshowtop, m_lwidth, m_lheight, & m_adjdc, m_lpicleft, m_lpictop, srccopy);
ReleaseDC (PDC);
This approach solves the problem of refreshing and flicker, and the image is very smooth and no flashes.
Author blog: http://blog.9cbs.net/zswzwy/