directx 透明視窗

2021-06-05 06:45:34 字數 2029 閱讀 8098

第一步

新增類class cuimagedc

hbitmap getbmphandle(void)

dword* getbits(void)

protected:

hbitmap m_hbmp;

hdc  m_hdc;

char*   m_pbits;

};cuimagedc::cuimagedc()

cuimagedc::~cuimagedc()

void cuimagedc::create(int nwidth, int nheight)

void cuimagedc::delete()

第二步建立變數

lpdirect3dsu***ce9      g_pd3dsu***ce   = null;

cuimagedc   g_dcsu***ce;

第三步在建立好裝置後新增

if (failed(g_d3ddevice->createrendertarget(640, 480,

d3dfmt_a8r8g8b8, d3dmultisample_none, 0,

/*!g_is9ex*/false, // lockable

&g_pd3dsu***ce, null)))

g_d3ddevice->setrendertarget(0, g_pd3dsu***ce);

g_d3ddevice->setrenderstate(d3drs_cullmode, d3dcull_none);

// turn off d3d lighting to use vertex colors

g_d3ddevice->setrenderstate(d3drs_lighting, false);

// enable alpha blending.

g_d3ddevice->setrenderstate(d3drs_alphablendenable, true);

g_dcsu***ce.create(640, -480);

setwindowlong(hwnd, gwl_exstyle, (getwindowlong(hwnd, gwl_exstyle) & ~ws_ex_transparent) | ws_ex_layered);

第四步在渲染函式中新增

d3dlocked_rect lockedrect;

rect rc, rcsu***ce = ;

::getwindowrect(hwnd, &rc);

point ptwinpos = ;

point ptsrc = ;

size szwin = ;

blendfunction stblend = ;

hdc  hdcwnd = getwindowdc(hwnd);

lpdirect3dsu***ce9      g_psyssu***ce   = null;

g_d3ddevice->createoffscreenplainsu***ce(640, 480,

d3dfmt_a8r8g8b8, d3dpool_systemmem, &g_psyssu***ce, null);

g_d3ddevice->getrendertargetdata(g_pd3dsu***ce, g_psyssu***ce);

g_psyssu***ce->lockrect(&lockedrect, &rcsu***ce, d3dlock_readonly);

memcpy(g_dcsu***ce.getbits(), lockedrect.pbits, 4 * 640 * 480);

updatelayeredwindow(hwnd, hdcwnd, &ptwinpos, &szwin, g_dcsu***ce.getsafehdc(), &ptsrc, rgb(255,255,255), &stblend, ulw_alpha| ulw_colorkey);

g_psyssu***ce->unlockrect();

g_psyssu***ce->release();

完成後 窗體為透明色,  可以繪製各種無窗體的圖形和文理

DirectX 之 DirectDraw 視窗模式

在視窗模式下使用 directdraw 最大的痛苦是不能使用換頁操作 flip 但是有許多人還是喜歡在視窗模式下進行遊戲.首先,視窗模式下建立乙個 idirectdraw 介面物件和全屏模式下是一樣的,不同的是,在選擇協作模式和不能改變螢幕解析度.當然你堅決要改變,一定要有禮貌的改變.設定協作級別,...

DirectX建立Windows視窗

過程 winmain 視窗類 註冊視窗類 建立視窗 顯示和更新視窗 訊息迴圈 視窗過程 include define window class ugpdx define window title demo window lresult winapi msgproc hwnd hwnd,uint ms...

使視窗透明

define lwa colorkey 0x00000001 use color as the transparency color.define ws ex layered 0x00080000 define lwa alpha 2 use balpha to determine the opac...