2011/05/26

[vc6]視窗透明化

最近在製作產品,剛好需要這個功能,所以把參考網址跟code貼出來,當作筆記。


<Code>
//設定半透明效果
//1.加入WS_EX_LAYERED擴展屬性
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
//2.載入User32.dll
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{  //3.先定義一個函數,等一下要拿來接User32.dll內的"SetLayeredWindowAttributes" function point
    typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
    //4.宣告一個型態為MYFUNC的變數fun
    MYFUNC fun = NULL;
    //5.取得SetLayeredWindowAttributes函數指針
    fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
    //6.如果有找到"SetLayeredWindowAttributes"這個function的位址,就開使用吧~
    if(fun)fun(this->GetSafeHwnd(),0,160,2);//param 3:0->全透明,255->不透明
    //7.用完記得釋放Library
    FreeLibrary(hInst);
}
</Code>
SetLayeredWindowAttributes 的用法
第一個參數:要透明化的那個視窗的 window  handle
第二個參數:可以用RGB(255,255,255),則視窗上白色的地方會變透明
第三個參數:0(全透明,會完全看不到)~255(不透明)
第四個參數:LWA_ALPHA(0x00000002)跟參數3搭配使用
LWA_COLORKEY(0x00000001)跟參數2搭配使用-->針對特定顏色的透明化

詳細請參考 msdn說明

以上~

沒有留言:

張貼留言