본문 바로가기

프로그램언어/C++

ROP모드로 이미지 마스크 설정

ROP모드

dwRop는 현재 화면에 출력되는 이미지와 출력하고자 하는 이미지를 조합하는 연산을 말한다.

 

매크로 상수 설명
NOTSRCERASE 화면색과 비트맵 색을 or 연산후 반전
NOTSRCCOPY 비트맵을 반전
PATCOPY 현재 선택된 브러시로 칠함
PATNVERT 브러시와 화면색을 XOR 연산
SRCAND 비트맵과 화면색을 AND 연산
SRCCOPY 비트맵을 그대로 화면으로 출력
SRCPAINT 비트맵과 화면을 OR연산
WHITENESS 무조건 흰색으로 칠함
BLACKNESS 무조건 검정색으로 칠함
DSTINVERT 회면색을 반전
MERGECOPY 브러시와 비트맵의 색을 AND 연산

 

타이머를 이용하여 0.1초마다 함수를 호출하여 움직이는 이미지를 만들어 보자.

#include <windows.h>

#include <TCHAR.H>

#include "resource.h"

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

HINSTANCE hInst;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

{

    HWND hwnd;

    MSG msg;

    WNDCLASS WndClass;

    hInst = hInstance;

    WndClass.style = CS_HREDRAW | CS_VREDRAW;

    WndClass.lpfnWndProc = WndProc;

    WndClass.cbClsExtra = 0;

    WndClass.cbWndExtra = 0;

    WndClass.hInstance = hInstance;

    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);

    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

    WndClass.lpszMenuName = NULL;

    WndClass.lpszClassName = _T("Window Class Name");

    RegisterClass(&WndClass);

    hwnd = CreateWindow(_T("Window Class Name"), _T("Window Title Name"), WS_OVERLAPPEDWINDOW,

        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    ShowWindow(hwnd, nCmdShow);

    UpdateWindow(hwnd);

    while (GetMessage(&msg, NULL, 0, 0))

    {

        TranslateMessage(&msg);

        DispatchMessage(&msg);

    }

    return (int)msg.wParam;

}

void Animation(int xPos, int yPos, HDC hdc)

{

    HDC memdc;

    HBITMAP RunBit[10], hBit, oldBit;

    static int count;

    int i;

    count++;

    count = count % 10;

    RunBit[0] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R1)); 

    RunBit[1] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R2)); 
    RunBit[2] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R3)); 

    RunBit[3] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R4)); 

    RunBit[4] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R5)); 

    RunBit[5] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R5)); 

    RunBit[6] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R7)); 

    RunBit[7] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R8)); 

    RunBit[8] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R9));
    RunBit[9] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R10));

    memdc = CreateCompatibleDC(hdc);

    hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP5_6));  //배경이미지 불러오기

    oldBit = (HBITMAP)SelectObject(memdc, hBit);

    BitBlt(hdc, 0, 0, 819, 614, memdc, 0, 0, SRCCOPY);   // 배경이미지 출력

    SelectObject(memdc, RunBit[count]);

    BitBlt(hdc, xPos, yPos, 180, 240, memdc, 0, 0, SRCCOPY);   // SRCCOPY 화면에 그대로 출력

    SelectObject(memdc, oldBit);

    for (i = 0; i < 10; i++)

        DeleteObject(RunBit[i]);

    DeleteDC(memdc);

    DeleteObject(hBit);

}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)

{

    HDC hdc;

    PAINTSTRUCT ps;

    static int xPos;

 

    switch (iMsg)

    {

    case WM_CREATE:

        xPos = -100;

        SetTimer(hwnd, 1, 100, NULL);    // ID가 1인 타이머 생성,  0.1초마다 WM_TIMER 호출  

        break;

    case WM_TIMER:

        xPos += 10;

        if (xPos > 819) xPos = -100;

        InvalidateRgn(hwnd, NULL, true);

        return 0;

    case WM_PAINT:

        hdc = BeginPaint(hwnd, &ps);

        Animation(xPos, 300, hdc);

        EndPaint(hwnd, &ps);

        break;

    case WM_DESTROY:

        KillTimer(hwnd, 1);

        PostQuitMessage(0);

        break;

    }

    return DefWindowProc(hwnd, iMsg, wParam, lParam);

}

실행결과]

 

마스크를 이용한 애니메이션 프로그램

void Animation(int xPos, int yPos, HDC hdc)

{

    HDC memdc;

    HBITMAP RunBit[10], hBit, oldBit, Mask[10];

    static int count;

    int i;

    count++;

    count = count % 10;

    RunBit[0] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R1));

    RunBit[1] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R2));

    RunBit[2] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R3)); //

    RunBit[3] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R4)); //

    RunBit[4] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R5)); //

    RunBit[5] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R5)); //

    RunBit[6] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R7)); //

    RunBit[7] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R8)); //

    RunBit[8] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R9)); //

    RunBit[9] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_R10)); //

    Mask[0] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M1));

    Mask[1] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M2));

    Mask[2] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M3));

    Mask[3] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M4));

    Mask[4] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M5));

    Mask[5] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M6));

    Mask[6] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M7));

    Mask[7] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M8));

    Mask[8] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M9));

    Mask[9] = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP_M10));

 

    memdc = CreateCompatibleDC(hdc);

    hBit = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP5_6));

    oldBit = (HBITMAP)SelectObject(memdc, hBit);

    BitBlt(hdc, 0, 0, 819, 614, memdc, 0, 0, SRCCOPY);

    SelectObject(memdc, Mask[count]);

    BitBlt(hdc, xPos, yPos, 180, 240, memdc, 0, 0, SRCAND);  // AND연산 이미지의 배경이 사라지고 캐릭터만 보임

    SelectObject(memdc, RunBit[count]);

    BitBlt(hdc, xPos, yPos, 180, 240, memdc, 0, 0, SRCPAINT); // 캐리터가 OR연산으로 칼러 캐릭터만 보임

    SelectObject(memdc, oldBit);

    for (i = 0; i < 10; i++)

    {

        DeleteObject(RunBit[i]);

        DeleteObject(Mask[i]);

    }

    DeleteDC(memdc);

    DeleteObject(hBit);

}

실행 결과]

 

'프로그램언어 > C++' 카테고리의 다른 글

대화상자만들기2  (0) 2020.12.28
대화상자만들기  (0) 2020.12.23
API 비트맵2  (0) 2020.09.28
API 비트맵  (0) 2020.09.28
API 단축키  (0) 2020.09.28