OLE控件在Direct3D中的渲染方法

Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题

那么, 有什么办法让GDI绘制的内容在3D中显示出来?反正都是图像, 总有办法实现的嘛!

前段时间在研究浏览器在游戏中的嵌入,基本的思路就是在后台打开一个浏览窗口, 然后把它显示的内容拷贝到一张纹理上, 再把纹理在D3D中绘制出来, 至于事件处理就要另做文章了.

所以, 其它的Windows里的GDI绘制的东西都可以这样来实现!
最初我是GetDC, 然后GetPixel逐像素拷贝, 慢得我想死.....
后来发现了BitBlt这一速度很快的复制方法, 才有了实用价值:

1. 取得控件的DC: GetDC(hWnd)
2. 取得Texture的DC: IDirect3DSurface9::GetDC
3. 用BitBlt拷贝过去

BOOLBitBlt(
HDChdcDest,
//handletodestinationDC
intnXDest,//x-coordofdestinationupper-leftcorner
intnYDest,//y-coordofdestinationupper-leftcorner
intnWidth,//widthofdestinationrectangle
intnHeight,//heightofdestinationrectangle
HDChdcSrc,//handletosourceDC
intnXSrc,//x-coordinateofsourceupper-leftcorner
intnYSrc,//y-coordinateofsourceupper-leftcorner
DWORDdwRop//rasteroperationcode
);

如果是OLE控件那就更简单啦:

WINOLEAPIOleDraw(
IUnknown
*pUnk,//Pointertotheviewobjecttobedrawn
DWORDdwAspect,//Howtheobjectistoberepresented
HDChdcDraw,//Devicecontextonwhichtodraw
LPCRECTlprcBounds//Pointertotherectangleinwhichtheobject
//isdrawn
);

比如我有一个IWebBrowser2的指针, 想把它显示的内容拷贝到纹理上, 可以这么干:

IDirect3DSurface9*pSurface=NULL;
this->mTexture->GetSurfaceLevel(0,&pSurface);
if(NULL!=pSurface)
{
HDChdcTexture;
HRESULThr
=pSurface->GetDC(&hdcTexture);
if(FAILED(hr))return;
::SetMapMode(hdcTexture,MM_TEXT);
::OleDraw(pBrowser,DVASPECT_CONTENT,hdcTexture,
&rect);
pSurface
->ReleaseDC(hdcTexture);
pSurface
->Release();
}

Show一下:
OLE控件在Direct3D中的渲染方法

不光是浏览器啦, 任何OLE控件都可以, 可以发挥你的想像力:
OLE控件在Direct3D中的渲染方法