Windows上的圖形繪制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中顯示一些Windows中的控件會有很多問題
那么, 有什么辦法讓GDI繪制的內容在3D中顯示出來?反正都是圖像, 總有辦法實現的嘛!
前段時間在研究瀏覽器在游戲中的嵌入, 基本的思路就是在后臺打開一個瀏覽窗口, 然后把它顯示的內容拷貝到一張紋理上, 再把紋理在D3D中繪制出來, 至于事件處理就要另做文章了.
所以, 其它的Windows里的GDI繪制的東西都可以這樣來實現!
最初我是GetDC, 然后GetPixel逐像素拷貝, 慢得我想死.....
后來發現了BitBlt這一速度很快的復制方法, 才有了實用價值:
1. 取得控件的DC: GetDC(hWnd)
2. 取得Texture的DC: IDirect3DSurface9::GetDC
3. 用BitBlt拷貝過去
BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);
如果是OLE控件那就更簡單啦:
WINOLEAPI OleDraw(
IUnknown * pUnk, //Pointer to the view object to be drawn
DWORD dwAspect, //How the object is to be represented
HDC hdcDraw, //Device context on which to draw
LPCRECT lprcBounds //Pointer to the rectangle in which the object
// is drawn
);
比如我有一個IWebBrowser2的指針, 想把它顯示的內容拷貝到紋理上, 可以這么干:
IDirect3DSurface9* pSurface = NULL;
this->mTexture->GetSurfaceLevel(0, &pSurface);
if (NULL != pSurface)
{
HDC hdcTexture;
HRESULT hr = pSurface->GetDC(&hdcTexture);
if(FAILED(hr)) return;
::SetMapMode(hdcTexture, MM_TEXT);
::OleDraw(pBrowser, DVASPECT_CONTENT, hdcTexture, &rect);
pSurface->ReleaseDC(hdcTexture);
pSurface->Release();
}
Show一下:

不光是瀏覽器啦, 任何OLE控件都可以, 可以發揮你的想像力:
