原理就是 Hook D3D的Present方法,
在执行Present之前,我们输出我们需要显示的东西,
这样我们显示的东西就在D3D输出的顶端,就不会产生闪烁的现象。
// draw our own things on top 绘制我们自己的东西
m_pGraphics->Render();
// call original routine 执行原来的Present
HRESULT hres = m_pIDirect3DDevice8->Present( pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
在Render里面我们可以:
显示图片:
LPD3DXSPRITE pSprite;
HRESULT hRes = D3DXCreateSprite(m_pDevice, &pSprite);
if (hRes != D3D_OK) return;
D3DXVECTOR2 vec;
vec.x = (float)wPosX; vec.y = (float)wPosY;
pSprite->Begin();
pSprite->Draw(m_pPicture, NULL, NULL, NULL, 0.0f, &vec, 0xFFFFFFFF);
pSprite->End();
pSprite->Release();
显示文字:
LPD3DXFONT pFont = NULL;
if (D3DXCreateFontIndirect(m_pDevice,&m_LogFont,&pFont) == D3D_OK)
{
RECT textPos = {0,0,0,0};
if (m_pDevice->BeginScene()==D3D_OK)
{
pFont->Begin();
pFont->DrawText(pText,-1,&textPos,DT_CALCRECT|DT_TOP|DT_SINGLELINE|DT_LEFT|DT_NOCLIP,dwTextColor);
textPos.left += wXPos;
textPos.right += wXPos;
textPos.top += wYPos;
textPos.bottom += wYPos;
//清空
if (bBlackBackground) m_pDevice->Clear(1,(D3DRECT*)&textPos,D3DCLEAR_TARGET,0x000000,0.0f,0);
pFont->DrawText(pText,-1,&textPos,DT_TOP|DT_SINGLELINE|DT_LEFT|DT_NOCLIP,dwTextColor);
pFont->End();
m_pDevice->EndScene();
}
SAFERELEASE(pFont);
}