zoukankan      html  css  js  c++  java
  • win32-UpdateLayeredWindow

    创建半透明的窗口,以及在窗口上绘制不透明的文本

    方法:

    1. 创建Bitmap具有PixelFormat32bppPARGB像素格式的GDI + 对象
    2. 创建一个Graphics对象以绘制该Bitmap对象。
    3. 使用GDI +将所有图形绘制到该对象中。
    4. 销毁Graphics在步骤2中创建对象。
    5. GetHBITMAPBitmap对象调用方法以获取Windows HBITMAP
    6. 销毁Bitmap对象。
    7. 使用创建内存DC,CreateCompatibleDC然后HBITMAP从步骤5中选择
    8. 使用内存DC作为源调用UpdateLayeredWindow。
    9. 选择以前的位图并删除内存DC。
    10. 销毁HBITMAP在步骤5中创建的。

    代码示例:

    #include <Windows.h>
    #include <stdio.h>
    #include <iostream>
    #include <ObjIdl.h>
    #include <gdiplus.h>
    #include <gdiplusheaders.h>
    using namespace Gdiplus;
    #pragma comment (lib,"Gdiplus.lib")
    
    #define MAX_WIDTH 800
    #define MAX_HEIGHT 600
    
    using namespace std;
    
    void Drawtext(HWND hwnd, HDC hdc);
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
        if (message == WM_DESTROY) {
    
            PostQuitMessage(0);
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    };
    
    HINSTANCE hinst;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevinstance, PSTR szCmdLine, int iCmdShow) {
        HWND hWnd;
        GdiplusStartupInput gdiplusStartupInput;
        ULONG_PTR gdiplusToken;
    
        //Initialize GDI+
        GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
        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 = CreateWindowExW(
            WS_EX_LAYERED | WS_EX_TOPMOST,
            L"win32",
            L"WinSoup",
            WS_POPUP,
            0, 0, 1000, 500,
            nullptr,
            nullptr,
            hInstance,
            nullptr
        );
    
        if (hWnd == NULL) {
            return 0;
        }
    
        Drawtext(hWnd, GetDC(hWnd));
    
        ShowWindow(hWnd, SW_SHOW);
    
    
        MSG msg = {};
    
        while (GetMessage(&msg, NULL, 0, 0)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
    }
    
    void Drawtext(HWND hwnd, HDC hdc)
    {
        FontFamily  fontFamily(L"Times New Roman");
        Font        font(&fontFamily, 32, FontStyleRegular, UnitPixel);
        PointF      pointF(30.0f, 10.0f);
        SolidBrush  solidBrush(Color(255, 0, 0, 0));
    
        Bitmap softwareBitmap(MAX_WIDTH, MAX_HEIGHT, PixelFormat32bppARGB);
        Graphics g(&softwareBitmap);
    
        g.Clear(Gdiplus::Color(30, 0, 0, 0));  // 30: alpha value 
    
        g.DrawString(L"Hello Hello Hello Hello Hello Hello Hello Hello", -1, &font, pointF, &solidBrush);
    
        HBITMAP bmp;
        softwareBitmap.GetHBITMAP(Color(0, 0, 0, 0), &bmp);
    
        HDC memdc = CreateCompatibleDC(hdc);
        HGDIOBJ original = SelectObject(memdc, bmp);
    
    
        BLENDFUNCTION blend = { 0 };
        blend.BlendOp = AC_SRC_OVER;
        blend.SourceConstantAlpha = 255;
        blend.AlphaFormat = AC_SRC_ALPHA;
        POINT ptLocation = { 200, 300 };
        SIZE szWnd = { MAX_WIDTH, MAX_HEIGHT };
        POINT ptSrc = { 0, 0 };
        BOOL l = UpdateLayeredWindow(hwnd, hdc, &ptLocation, &szWnd, memdc, &ptSrc, 0, &blend, ULW_ALPHA);
        int err = GetLastError();
        SelectObject(hdc, original);
    
        DeleteObject(bmp);
        DeleteObject(memdc);
    }
  • 相关阅读:
    [题解](组合数/二位前缀和)luogu_P2822组合数问题
    [题解](tarjan割点/点双)luogu_P3225_矿场搭建
    [题解](树形dp/换根)小x游世界树
    [題解](DP)CF713C_Sonya and Problem Wihtout a Legend
    [題解]hdu_6412公共子序列
    [題解](最小生成樹)luogu_P2916安慰奶牛
    [题解](堆)luogu_P1631序列合并
    [题解](最短路)luogu_P5122 Fine Dining
    [题解](次短路)luogu_P2865路障(未)
    [题解](最短路(树))luogu_P5201_short cut
  • 原文地址:https://www.cnblogs.com/strive-sun/p/13073015.html
Copyright © 2011-2022 走看看