画像透過転送(GdiAlphaBlend)             <TOP>


GdiAlphaBlendを使って画像を転送します。

あらかじめ120×80ピクセルのBMPファイルをALPHATEST.BMPとして用意してください。

GdiAlphaBlend 画像転送

RtlMoveMemory メモリブロックのコピー

GetDC コンテキスト取得

ReleaseDC コンテキスト解放

 

 

'================================================================
'= 透過転送(GdiAlphaBlend)
'=    (GdiAlphaBlend.bas)
'================================================================
#include "Windows.bi"

Type BLENDFUNCTION
    BlendOp As Byte
    BlendFlags As Byte
    SourceConstantAlpha As Byte
    AlphaFormat As Byte
End Type

' 画像を透過転送
Declare Function Api_GdiAlphaBlend& Lib "gdi32" Alias "GdiAlphaBlend" (ByVal hDC&, ByVal lInt&, ByVal lInt&, ByVal lInt&, ByVal lInt&, ByVal hDC&, ByVal lInt&, ByVal lInt&, ByVal lInt&, ByVal lInt&, ByVal BLENDFUNCT&)

' ある位置から別の位置にメモリブロックを移動する関数の宣言
Declare Sub CopyMemory Lib "Kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length&)

' 指定されたウィンドウのクライアント領域または画面全体を表すディスプレイデバイスコンテキストのハンドルを取得
Declare Function Api_GetDC& Lib "user32" Alias "GetDC" (ByVal hWnd&)

' デバイスコンテキストを解放
Declare Function Api_ReleaseDC& Lib "user32" Alias "ReleaseDC" (ByVal hWnd&, ByVal hDC&)

Var shared HScroll1 As Object
Var shared Picture1 As Object
Var shared Picture2 As Object
Var shared Text4 As Object
Var shared Bitmap As Object
BitmapObject Bitmap

HScroll1.Attach GetDlgItem("HScroll1")
Picture1.Attach GetDlgItem("Picture1")
Picture2.Attach GetDlgItem("Picture2")
Text4.Attach GetDlgItem("Text4")

Var shared hDC1 As Long
Var shared hDC2 As Long

'================================================================
'=
'================================================================
Declare Sub Mainform_Start edecl ()
Sub Mainform_Start()
    HScroll1.SetScrollRange 0,255
    HScroll1.SetScrollStep 5, 1
    HScroll1.SetScrollPos 0

    Bitmap.LoadFile "AlphaTest.Bmp"
    Picture1.DrawBitmap Bitmap, 0, 0
    Bitmap.DeleteObject

    hDC1 = Api_GetDC(Picture1.GethWnd)
    hDC2 = Api_GetDC(Picture2.GethWnd)
End Sub

'================================================================
'=
'================================================================
Declare Sub HScroll1_Change edecl ()
Sub HScroll1_Change()
    Var BF As BLENDFUNCTION
    Var Alpha As Long
    Var lBF As Long
    Var Res As Long

    Picture2.Cls
    Alpha = HScroll1.GetScrollPos

    BF.BlendOp = AC_SRC_OVER
    BF.BlendFlags = 0
    BF.SourceConstantAlpha = Alpha
    BF.AlphaFormat = 0

    '構造体のコピー
    CopyMemory lBF, BF, 4

    '画像透過転送
    Res = Api_GdiAlphaBlend(hDC2, 0, 0, Picture2.GetWidth, Picture2.GetHeight, hDC1, 0, 0, Picture1.GetWidth, Picture1.GetHeight, lBF)

    Text4.SetWindowText Trim$(Str$(Alpha))
End Sub

'================================================================
'= 
'================================================================
Declare Sub Mainform_QueryClose edecl (Cancel%, ByVal Mode%)
Sub Mainform_QueryClose(Cancel%, ByVal Mode%)
    Var Res As Long

    If Cancel% = 0 Then
        Res = Api_ReleaseDC(Picture1.GethWnd, hDC1)
        Res = Api_ReleaseDC(Picture2.GethWnd, hDC2)
        End
    End If
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End