问题
I would like to scroll window contents in which drawing is performed with Direct2D api through ID2D1RenderTarget.
In GDI I could create a buffer with CreateCompatibleDC and later scroll its contents with ScrollDC, redraw exposed area and BitBlt the buffer to window.
I cannot see any necessary API in Direct2D to perform the same operations. How can I achieve the same functionality without using GetDC (and GDI), and without using own third buffer?
回答1:
There is no Scroll API in Direct2D. Your best solution to get hardware accelerated scrolling is to use a 2nd buffer. On the ID2D1RenderTarget
that you want to scroll, use CreateCompatibleRenderTarget()
to create an ID2D1BitmapRenderTarget
(it's a good idea to cache this guy) with the same pixel size as ID2D1RenderTarget::GetPixelSize()
and with the same resolution as returned from ID2D1RenderTarget::GetDpi()
. Then, use ID2D1BitmapRenderTarget::GetBitmap()
to get the underlying ID2D1Bitmap
. Next, use ID2D1Bitmap::CopyFromRenderTarget()
copy the contents with adjustments for the distance you're scrolling. Then copy that bitmap's contents back to the original render target, re-render the uncovered area, and present (via EndDraw).
回答2:
You can use translation.
MSDN: To translate a 2-D object is to move the object along the x-axis, the y-axis, or both.
m_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(20, 10));
More details here http://msdn.microsoft.com/en-us/library/windows/desktop/dd756691(v=vs.85).aspx
回答3:
In DXGI 1.2 there is a new IDXGISwapChain1::Present1
API call with DXGI_PRESENT_PARAMETERS
parameter. It contains functionality supporting scrolling window contents.
来源:https://stackoverflow.com/questions/10579208/how-to-scroll-window-contents-using-direct2d-api