Dim BitMapLong As Long
Dim TempLong As Long
TempLong = OpenClipboard(0)
TempLong = EmptyClipboard()
BitMapLong = LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, _
LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS Or LR_LOADTRANSPARENT)
TempLong = SetClipboardData(CF_BITMAP, BitMapLong)
TempLong = CloseClipboard()Set Picture = Clipboard.GetData(vbCFBitmap)
Dim TempLong As Long
TempLong = OpenClipboard(0)
TempLong = EmptyClipboard()
BitMapLong = LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, _
LR_LOADFROMFILE Or LR_LOADMAP3DCOLORS Or LR_LOADTRANSPARENT)
TempLong = SetClipboardData(CF_BITMAP, BitMapLong)
TempLong = CloseClipboard()Set Picture = Clipboard.GetData(vbCFBitmap)
TransparentBlt
The TransparentBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.BOOL TransparentBlt(
HDC hdcDest, // handle to destination DC
int nXOriginDest, // x-coord of destination upper-left corner
int nYOriginDest, // y-coord of destination upper-left corner
int nWidthDest, // width of destination rectangle
int hHeightDest, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXOriginSrc, // x-coord of source upper-left corner
int nYOriginSrc, // y-coord of source upper-left corner
int nWidthSrc, // width of source rectangle
int nHeightSrc, // height of source rectangle
UINT crTransparent // color to make transparent
);
Parameters
hdcDest
[in] Handle to the destination device context.
nXOriginDest
[in] Specifies the x-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nYOriginDest
[in] Specifies the y-coordinate, in logical units, of the upper-left corner of the destination rectangle.
nWidthDest
[in] Specifies the width, in logical units, of the destination rectangle.
hHeightDest
[in] Handle to the height, in logical units, of the destination rectangle.
hdcSrc
[in] Handle to the source device context.
nXOriginSrc
[in] Specifies the x-coordinate, in logical units, of the source rectangle.
nYOriginSrc
[in] Specifies the y-coordinate, in logical units, of the source rectangle.
nWidthSrc
[in] Specifies the width, in logical units, of the source rectangle.
nHeightSrc
[in] Specifies the height, in logical units, of the source rectangle.
crTransparent
[in] The RGB color in the source bitmap to treat as transparent.
Return Values
If the function succeeds, the return value is TRUE.If the function fails, the return value is FALSE. Windows NT/2000 or later: To get extended error information, call . Res
The TransparentBlt function supports all formats of source bitmaps. However, for 32 bpp bitmaps, it just copies the alpha value over. Use AlphaBlend to specify 32 bits-per-pixel bitmaps with transparency. If the source and destination rectangles are not the same size, the source bitmap is stretched to match the destination rectangle. When the SetStretchBltMode function is used, the iStretchMode modes of BLACKONWHITE and WHITEONBLACK are converted to COLORONCOLOR for the TransparentBlt function. The destination device context specifies the transformation type for the destination coordinates. The source device context specifies the transformation type for the source coordinates. TransparentBlt does not mirror a bitmap if either the width or height, of either the source or destination, is negative. Windows 98/Windows 2000 or later: When used in a multimonitor system, both hdcSrc and hdcDest must refer to the same device or the function will fail.
首先要准备好做为欢迎画面所需要的图片,然后对图片进行简单的处理,把需要透空的地方填上纯白色(255,255,255),然后保存为*.bmp文件,这用PhotoShop可以很容易地实现。需要注意的是,图片必须为“索引色”模式,如果不是就需用PhotoShop来修改,否则不能实现透空效果。 先建立一个标准EXE工程,在窗体上文稿放置一个Picture控件,控件名为Picture1,和一个Timer控件,控件名为Timer1,Interval属性设置为2000。
原程序如下:
Option Explicit
`定义获取桌面HDC的api函数
Private Declare Function GetDC Lib “user32” (ByVal hwnd As Long) As Long
`定义TransparentBlt函数
`实现图片的透空效果需要用上API函数:TransparentBlt,这个函数功能十分强大,而且使用方便,但不幸的
`是VB自带的API浏览器居然把它的漏掉了,所以我们只有采用人工输入的方法了
Private Declare Function TransparentBlt Lib “msimg32.dll”_
(ByVal hdcDest As Long, _
ByVal nXOriginDest As Long, _
ByVal nYOriginDest As Long, _
ByVal nWidthDest As Long, _
ByVal nHeightDest As Long, _
ByVal hdcSrc As Long, _
ByVal nXOriginSrc As Long, _
ByVal nYOriginSrc As Long, _
ByVal nWidthSrc As Long, _
ByVal nHeightSrc As Long, _
ByVal crTransparent As Long) As Long
`其中,hdcDest为目标地的HDC,nXOriginDEst和nYoriginDest分别为目标图像的起始点坐标,nWidthDesk和nHeightDest分别为目标图像的宽度和高度。与之相应的hdcSrc、nXOriginSrc、nyOriginSrc、nWidthSrc、nHeightSrc分别为原图的HDC、原图的起始X、Y坐标、原图和宽度和长度,crTransparent为需要设置成透空的颜色的RGB值。
`定义用于恢复桌面的函数
Private Declare Function InvalidateRectAsAny Lib “user32” Alias
“InvalidateRect”_ (ByVal hwnd As Long, lpRect As Any, ByVal bErase As Long) As Long
Private Sub Form_Load()
Me.Hide
Dim Pic As Long
Dim w As Long
Dim h As Long
Dim x As Long
Dim sx, sy
Picture1.AutoRedraw = True
`获取桌面的HDC
x = GetDC(0)
`计算桌面的宽度和高度
sx = Screen.Width \ Screen.TwipsPerPixelX
sy = Screen.Height \ Screen.TwipsPerPixelY
`计算图像的宽度和高度
w = Picture1.ScaleX(Picture1.Picture.Width, 8, vbPixels)
h = Picture1.ScaleY(Picture1.Picture.Height, 8, vbPixels)
picture1.picture=loadpicture(“图像文件的完整文件名称”)
`使透空的图像显示在桌面的中央
Pic = TransparentBlt(x, _
sx / 2 - w / 2, _
sy / 2 - h / 2, _
w, _
h, _
Picture1.hDC, _
0, _
0, _
w, _
h, _
RGB(255, 255, 255))
End Sub
Private Sub Timer1_Timer()
`两秒钟后恢复桌面
InvalidateRectAsAny 0, ByVal 0&, True
Load 自制程序的主窗体名
Timer1.Enabled = False
End Sub
需要注意的是程序完成后如果直接在VB环境下运行有可能会出现透空图像一闪而过的现象,这并不是你的错,只要把程序编译成*.exe的文件后运行一切都会正常的。