VB無所不能之四 製作透明和半透明窗體

2021-08-25 03:53:12 字數 4089 閱讀 7041

vb無所不能之四:製作透明和半透明窗體

我們經常可以看到這樣的窗體,覺得很炫,如圖所示:

同樣,對windows系統方面的程式設計似乎首先想到的絕對不是vb,而大部分程式設計師想到的一定是vc。

其實,vb對於這個實現非常方便且簡單,用到了「user32」中的setlayeredwindowattributes()函式。

setlayeredwindowattributes()函式介紹:

函式宣告:

declare

function setlayeredwindowattributes lib

"user32" () declare

function setlayeredwindowattributes lib

"user32" (byval hwnd

as long, byval crkey

as long, byval balpha

as byte, byval dwflags

as long)

as long

hwnd是透明窗體的控制代碼,

crkey為顏色值,

balpha是透明度,取值範圍是[0,255],

dwflags是透明方式,可以取兩個值:當取值為lwa_alpha時,crkey引數無效,balpha引數有效;

當取值為lwa_colorkey時,balpha引數有效而窗體中的所有顏色為crkey的地方將變為透明。

下面我們做兩個實驗:

第乙個:做乙個半透明窗體

步驟一:開啟vb建立乙個窗體form

步驟二:將窗體背景顏色設為

:&hff0000

步驟三:將下面**貼上到程式中:

declare

function getwindowlong lib

"user32" alias

"getwindowlonga" () declare

function getwindowlong lib

"user32" alias

"getwindowlonga" (byval hwnd

as long, byval nindex

as long)

as long

declare

function setwindowlong lib

"user32" alias

"setwindowlonga" () declare

function setwindowlong lib

"user32" alias

"setwindowlonga" (byval hwnd

as long, byval nindex

as long, byval dwnewlong

as long)

as long

declare

function setlayeredwindowattributes lib

"user32" () declare

function setlayeredwindowattributes lib

"user32" (byval hwnd

as long, byval crkey

as long, byval balpha

as byte, byval dwflags

as long)

as long

private

const ws_ex_layered = &h80000

const gwl_exstyle = ()

const gwl_exstyle = (-20)

private

const lwa_alpha = &h2

private

const lwa_colorkey = &h1

sub form_load()

sub form_load()

dim rtn

as long

rtn = getwindowlong(hwnd, gwl_exstyle)

rtn = rtn

or ws_ex_layered

setwindowlong hwnd, gwl_exstyle, rtn

setlayeredwindowattributes hwnd, 0, 100, lwa_alpha

endsub

執行結果如下:

第乙個:做乙個異型窗體

將下面**貼上到程式中:

getwindowlong lib

"user32" alias

"getwindowlonga" () getwindowlong lib

"user32" alias

"getwindowlonga" ( getwindowlong lib

"user32" alias

"getwindowlonga" () getwindowlong lib

"user32" alias

"getwindowlonga" (byval hwnd

as long, byval nindex

as long)

as long

setwindowlong lib

"user32" alias

"setwindowlonga" () setwindowlong lib

"user32" alias

"setwindowlonga" ( setwindowlong lib

"user32" alias

"setwindowlonga" () setwindowlong lib

"user32" alias

"setwindowlonga" (byval hwnd

as long, byval nindex

as long, byval dwnewlong

as long)

as long

setlayeredwindowattributes lib

"user32" () setlayeredwindowattributes lib

"user32" ( setlayeredwindowattributes lib

"user32" () setlayeredwindowattributes lib

"user32" (byval hwnd

as long, byval crkey

as long, byval balpha

as byte, byval dwflags

as long)

as long

private

const ws_ex_layered = &h80000

const gwl_exstyle = ()

const gwl_exstyle = (-20)

private

const lwa_alpha = &h2

private

const lwa_colorkey = &h1

sub form_load()

sub form_load()

dim rtn

as long

borderstyler = 0

rtn = getwindowlong(hwnd, gwl_exstyle)

rtn = rtn

or ws_ex_layered

setwindowlong hwnd, gwl_exstyle, rtn

setlayeredwindowattributes hwnd, &hff0000, 0, lwa_colorkey

'將扣去視窗中的藍色

endsub

執行結果如下所示:

VB無所不能之四 製作透明和半透明窗體

vb無所不能之四 製作透明和半透明窗體 我們經常可以看到這樣的窗體,覺得很炫,如圖所示 同樣,對windows系統方面的程式設計似乎首先想到的絕對不是vb,而大部分程式設計師想到的一定是vc。其實,vb對於這個實現非常方便且簡單,用到了 user32 中的setlayeredwindowattrib...

警惕「無所不能症」

近期工作極其繁忙,忙到來不及思考。因為偶然的原因打斷了工作狀態,才發現自己的心態隱隱有些問題。無所不能 症 解決問題的感覺會讓人上癮,學術點講,正向反饋。解決了乙個問題,感覺不錯,解決了10個問題呢?做了一件事,感覺不錯,做了10件呢?1個人覺得你很行,10個人呢?1個月幹的不錯,1年呢?持續成功的...

警惕「無所不能症」

近期工作極其繁忙,忙到來不及思考。因為偶然的原因打斷了工作狀態,才發現自己的心態隱隱有些問題。無所不能 症 解決問題的感覺會讓人上癮,學術點講,正向反饋。解決了乙個問題,感覺不錯,解決了10個問題呢?做了一件事,感覺不錯,做了10件呢?1個人覺得你很行,10個人呢?1個月幹的不錯,1年呢?持續成功的...