VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > vb >
  • vb教程之透明方式叠加图象

 透明叠加方式是图象处理中常用的一种处理方式,在这种处理方式中,一幅图片叠加到另 
一幅图片上,但是这幅图象不是完全将原来的图象覆盖,而是能够部分的透过叠加的图象显示 
出来,下面我就介绍如何在VB中编程实现这样的功能。 
    首先在建立两个大小相等的图象文件,将它们保存成BMP文件,VB5以上的版本支 
持JPEG显示,也可以保存为Jpg文件。在Form中加入两个PictureBox控件,将它们的大小设置成 
同图象文件相同的大小。注意这两个PictureBox控件不要相互重叠,也不要和Form中的其它控件 
产生重叠。将两幅图象分别加入到两个PictureBox控件中。然后在Form中加入一个CommandButton 
控件。最后在Form的代码窗口中加入以下代码: 

Private Type LOGBRUSH 
        lbStyle As Long 
        lbColor As Long 
        lbHatch As Long 
End Type 
Private Type RECT 
        Left As Long 
        Top As Long 
        Right As Long 
        Bottom As Long 
End Type 

Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal _ 
        hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) _ 
        As Long 
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc _ 
        As Long) As Long 
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _ 
        ByVal hObject As Long) As Long 
Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, _ 
        lpRect As RECT, ByVal hBrush As Long) As Long 
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _ 
        ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _ 
        ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc _ 
        As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long 
Private Declare Function CreateBrushIndirect Lib "gdi32" (lpLogBrush _ 
        As LOGBRUSH) As Long 
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long 

Const SRCPAINT = &HEE0086 
Const SRCAND = &H8800C6 
Const BS_SOLID = 0 
Const gColor = &HFFFFFF 

Private Sub Command1_Click() 
    Dim pDstDC As Long 
    Dim dcCompatible As Long 
    Dim pBitmapOld As Long 
    Dim bm As Long 
    Dim rectTemp As RECT 
    Dim lBrush As Long 
    Dim tLog As LOGBRUSH 
     
    dcCompatible = CreateCompatibleDC(Picture1.hdc) 
     
    bm = CreateCompatibleBitmap(Picture1.hdc, Picture1.Width \ 15, _ 
        Picture1.Height \ 15) 
     
    pBitmapOld = SelectObject(dcCompatible, bm) 
    rectTemp.Left = 0: rectTemp.Top = 0 
    rectTemp.Right = Picture1.Width \ 15: rectTemp.Bottom = _ 
        Picture1.Height \ 15 
    tLog.lbColor = gColor 
    tLog.lbStyle = BS_SOLID 
    tLog.lbHatch = 0 
    lBrush = CreateBrushIndirect(tLog) 
    FillRect dcCompatible, rectTemp, lBrush 
    BitBlt Picture1.hdc, 0, 0, Picture1.Width \ 15, _ 
        Picture1.Height \ 15, dcCompatible, 0, 0, SRCAND 
    SelectObject dcCompatible, pBitmapOld 

    BitBlt Picture1.hdc, 0, 0, Picture1.Width \ 15, _ 
        Picture1.Height \ 15, Picture2.hdc, 0, 0, SRCPAINT 
    DeleteDC dcCompatible 
End Sub 
    运行程序,点击Command1,你就可以看到Picture2上的图象叠加到了Picture1上面了。改变上面 
源程序中的 Const gColor = &HFFFFFF 一句中的gColor的值(在0到&HFFFFFFFF间变动),就可以 
使图象的叠加产生不同的透明效果。 
    以上程序在Win98、VB6.0下运行通过。

相关教程