码迷,mamicode.com
首页 > Windows程序 > 详细

win32-Transparent的使用

时间:2020-06-22 11:03:01      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:dcl   drawing   desktop   lap   fromfile   ret   while   win32   str   

这个api的功能主要是实现“透明”

原理: Transparent将hdc中bmp的特定颜色“透明化”

#include <Windows.h>
#pragma comment(lib,"Msimg32.lib")

using namespace std;

HBITMAP hBitmap;
HBITMAP hBitmap3;

void LoadMyImage(void) {

    hBitmap = (HBITMAP)LoadImage(NULL, L"C:\\Users\\xx\\Desktop\\PGUfb.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    if (hBitmap == NULL) {
        MessageBox(NULL, L"Error while loading image", L"Error", MB_OK | MB_ICONERROR);
    }
}

void LoadMyImage3(void) {

    hBitmap3 = (HBITMAP)LoadImage(NULL, L"C:\\Users\\xx\\Desktop\\QZLMJ.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    if (hBitmap3 == NULL) {
        MessageBox(NULL, L"Error while loading image", L"Error", MB_OK | MB_ICONERROR);
    }
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC memdc;
    HDC dcSkin;
    HBITMAP hOldMemBmp;
    HBITMAP hOldSkinBmp;
    switch (message)
    {   
    case WM_CREATE:
    {
        LoadMyImage();
        LoadMyImage3();
    }
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        // TODO: Add any drawing code that uses hdc here...
        memdc = CreateCompatibleDC(hdc);
        dcSkin = CreateCompatibleDC(hdc);
        hOldMemBmp = (HBITMAP)SelectObject(memdc, hBitmap);
        hOldSkinBmp = (HBITMAP)SelectObject(dcSkin, hBitmap3);
        TransparentBlt(memdc, 200, 100, 38,38, dcSkin, 0, 0, 38, 38, RGB(255, 255, 255));
        BitBlt(hdc, 0, 0, 988, 562, memdc, 0, 0, SRCCOPY);    
        DeleteObject(hOldSkinBmp);
        DeleteObject(hOldMemBmp);
        DeleteDC(memdc);
        DeleteDC(dcSkin);
        EndPaint(hwnd, &ps);
    }
    break;
    case WM_DESTROY:
        DeleteObject(hBitmap);
        DeleteObject(hBitmap3);
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
};

HINSTANCE hinst;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
    HWND hwnd;

    hinst = GetModuleHandle(NULL);
    // create a window class:
    WNDCLASS wc = {};
    wc.lpfnWndProc = WndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = L"win32";

    // register class with operating system:
    RegisterClass(&wc);

    // create and show window:
    hwnd = CreateWindow(L"win32", L"My program", WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL, 0, 0, 1000, 800, NULL, NULL, hinst, NULL);

    if (hwnd == NULL) {
        return 0;
    }

    ShowWindow(hwnd, SW_SHOW);


    MSG msg = {};

    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

 

win32-Transparent的使用

标签:dcl   drawing   desktop   lap   fromfile   ret   while   win32   str   

原文地址:https://www.cnblogs.com/strive-sun/p/13175520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!