본문 바로가기

Windows/Windows API

알파 블렌딩 (alphablending)

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HINSTANCE g_hInst;
HWND hWndMain;
LPSTR lpszClass="AlphaBlend";

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance
     ,LPSTR lpszCmdParam,int nCmdShow)
{
    HWND hWnd;
    MSG Message;
    WNDCLASS WndClass;
    g_hInst=hInstance;
    
    WndClass.cbClsExtra=0;
    WndClass.cbWndExtra=0;
    WndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WndClass.hInstance=hInstance;
    WndClass.lpfnWndProc=(WNDPROC)WndProc;
    WndClass.lpszClassName=lpszClass;
    WndClass.lpszMenuName=NULL;
    WndClass.style=CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&WndClass);

    hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
        NULL,(HMENU)NULL,hInstance,NULL);
    ShowWindow(hWnd,nCmdShow);
    hWndMain=hWnd;
    
    while(GetMessage(&Message,0,0,0)) {
        TranslateMessage(&Message);
        DispatchMessage(&Message);
    }
    return Message.wParam;
}

#include "resource.h"
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HDC MemDC;
    HBITMAP MyBitmap, OldBitmap;
    BLENDFUNCTION bf;
    char Mes[]="반 투명한 비트맵을 출력합니다.";
    static int Alpha=128;
    BITMAP bit;
    int bx,by;

    switch(iMessage) {
    case WM_LBUTTONDOWN:
        Alpha-=20;
        if (Alpha < 0) Alpha=0;
        InvalidateRect(hWnd,NULL,TRUE);
        return TRUE;
    case WM_RBUTTONDOWN:
        Alpha+=20;
        if (Alpha > 255) Alpha=255;
        InvalidateRect(hWnd,NULL,TRUE);
        return TRUE;
    case WM_PAINT:
        hdc=BeginPaint(hWnd, &ps);
        TextOut(hdc,10,10,Mes,strlen(Mes));
        MemDC=CreateCompatibleDC(hdc);
        MyBitmap=LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BITMAP1));
        OldBitmap=(HBITMAP)SelectObject(MemDC, MyBitmap);
        GetObject(MyBitmap, sizeof(BITMAP), &bit);
        bx=bit.bmWidth;
        by=bit.bmHeight;

        bf.BlendOp=AC_SRC_OVER;
        bf.BlendFlags=0;
        bf.SourceConstantAlpha=Alpha;
        bf.AlphaFormat=0;
        AlphaBlend(hdc,0,0,bx,by,MemDC,0,0,bx,by,bf);
        SelectObject(MemDC,OldBitmap);
        DeleteObject(MyBitmap);
        DeleteDC(MemDC);
        EndPaint(hWnd, &ps);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return(DefWindowProc(hWnd,iMessage,wParam,lParam));
}

'Windows > Windows API' 카테고리의 다른 글

capture  (0) 2013.10.01
비트 블릿 (BitBlt)  (0) 2013.10.01
dll 이용  (0) 2013.10.01
트리뷰 (tree view)  (0) 2013.10.01
트리 뷰 (tree view)  (0) 2013.10.01