分层窗口

作者: JetLu | 来源:发表于2015-12-30 12:42 被阅读228次

示例代码


#include <windows.h>
#include <gdiplus.h>
#include <iostream>

#pragma comment(lib, "Gdiplus.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")

using namespace Gdiplus;
using namespace std;

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow) {
    /*开启Gdi+*/
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);


    WNDCLASS wndClass;
    wndClass.style          = CS_HREDRAW | CS_VREDRAW;
    wndClass.lpfnWndProc    = WndProc;
    wndClass.cbClsExtra     = 0;
    wndClass.cbWndExtra     = 0;
    wndClass.hInstance      = 0;
    wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
    wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndClass.lpszMenuName   = NULL;
    wndClass.lpszClassName  = "app";
    cout << RegisterClass(&wndClass) << endl;

    HWND hWnd;
    hWnd = CreateWindowEx(
        WS_EX_LAYERED | WS_EX_TOOLWINDOW,
        "app",
        TEXT("The hello program"),
        WS_POPUP,
        0, 0, 196, 196,
        NULL, NULL, 0, NULL
    );
    cout << hWnd << endl;

    ShowWindow(hWnd, 1);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    static bool ldown;
    static POINT point;
    switch (message) {
        case WM_LBUTTONDOWN:
            ldown = true;
            SetCapture(hWnd);
            point.x = LOWORD(lParam);
            point.y = HIWORD(lParam);
            break;
        case WM_LBUTTONUP:
            ldown = false;
            ReleaseCapture();
            break;
        case WM_MOUSEMOVE:
            if (ldown) {
                POINT    pt;
                GetCursorPos(&pt);
                pt.x -= point.x;
                pt.y -= point.y;
                SetWindowPos(
                    hWnd,
                    NULL,
                    pt.x,
                    pt.y,
                    NULL, NULL,
                    SWP_NOREDRAW | SWP_NOSIZE | SWP_NOZORDER
                );
            }
            break;
        case WM_CREATE:
            {
                //在exe同级目录的图片
                Image image(L"bkg.png");
                int iW = image.GetWidth(),
                    iH = image.GetHeight();
                HDC hdcScreen = GetDC(0),
                    hdcMem    = CreateCompatibleDC(hdcScreen);
                HBITMAP hmap  = CreateCompatibleBitmap(hdcScreen, iW, iH);
                SelectObject(hdcMem, hmap);

                Graphics g(hdcMem);
                g.DrawImage(&image, 0, 0);
                // g.Clear(Color(100, 0, 0, 0));
                MoveWindow(
                    hWnd,
                    0, 0,
                    iW, iH,
                    true
                );
                BLENDFUNCTION blend = { 0 };
                blend.BlendOp = AC_SRC_OVER;
                blend.SourceConstantAlpha = 255;
                blend.AlphaFormat = AC_SRC_ALPHA;
                POINT    pPos = { 0, 0 };
                POINT    pSrc = { 0, 0 };
                SIZE    sizeWnd = { iW, iH };
                UpdateLayeredWindow(hWnd, hdcScreen, &pPos, &sizeWnd, hdcMem, &pSrc, NULL, &blend, ULW_ALPHA);
            }
            break;

        case WM_PAINT :

            break;
        case WM_DESTROY :
            PostQuitMessage(0);
            break ;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}

/*
Direct2D
{
                HDC hdcScreen = GetDC(0),
                    hdcMem    = CreateCompatibleDC(hdcScreen);
                HBITMAP hmap  = CreateCompatibleBitmap(hdcScreen, 196, 196);
                SelectObject(hdcMem, hmap);
                // d2d1 工厂
                ID2D1Factory  *m_pDirect2dFactory;
                D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);

                IWICImagingFactory *pFactory = NULL;
                IWICBitmapDecoder *pDecoder = NULL;
                CoInitialize(NULL);
                CoCreateInstance(
                    CLSID_WICImagingFactory,
                    NULL,
                    CLSCTX_INPROC_SERVER,
                    IID_IWICImagingFactory,
                    (LPVOID*)&pFactory
                );


                // 读取图片
                IWICBitmapFrameDecode *pSource = NULL;
                IWICStream *pStream = NULL;
                IWICFormatConverter *pConverter = NULL;
                IWICBitmapScaler *pScaler = NULL;
                pFactory->CreateDecoderFromFilename(
                    L"bkg.png",
                    NULL,
                    GENERIC_READ,
                    WICDecodeMetadataCacheOnLoad,
                    &pDecoder
                );
                pDecoder->GetFrame(0, &pSource);
                pFactory->CreateFormatConverter(&pConverter);

                HRESULT hr = pFactory->CreateBitmapScaler(&pScaler);
                if (SUCCEEDED(hr)) {
                    hr = pScaler->Initialize(
                        pSource,
                        196,
                        196,
                        WICBitmapInterpolationModeCubic
                    );
                }
                if (SUCCEEDED(hr)) {
                    hr = pConverter->Initialize(
                        pScaler,
                        GUID_WICPixelFormat32bppPBGRA,
                        WICBitmapDitherTypeNone,
                        NULL,
                        0.f,
                        WICBitmapPaletteTypeMedianCut
                    );
                }

                RECT rc;
                GetClientRect(hWnd, &rc);


                D2D1_SIZE_U size = SizeU(
                    rc.right - rc.left,
                    rc.bottom - rc.top
                );

                // Create a Direct2D render target.
                D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
                    D2D1_RENDER_TARGET_TYPE_DEFAULT,
                    D2D1::PixelFormat(
                        DXGI_FORMAT_B8G8R8A8_UNORM,
                        D2D1_ALPHA_MODE_PREMULTIPLIED),
                    0,
                    0,
                    D2D1_RENDER_TARGET_USAGE_NONE ,
                    D2D1_FEATURE_LEVEL_10
                );

                ID2D1DCRenderTarget  *m_pRenderTarget;
                m_pDirect2dFactory->CreateDCRenderTarget(
                    &props,
                    &m_pRenderTarget
                );
                m_pRenderTarget->BindDC(hdcMem, &rc);
                ID2D1Bitmap *bitmap;
                m_pRenderTarget->CreateBitmapFromWicBitmap(pConverter, 0, &bitmap);
                m_pRenderTarget->BeginDraw();
                m_pRenderTarget->DrawBitmap(
                    bitmap
                );

                m_pRenderTarget->EndDraw();

                BLENDFUNCTION blend;
                blend.BlendOp = AC_SRC_OVER;
                blend.SourceConstantAlpha = 255;
                MoveWindow(
                    hWnd,
                    0, 0,
                    196, 196,
                    true
                );
                blend.AlphaFormat = AC_SRC_ALPHA;
                POINT   pPos { 0, 0 };
                POINT   pSrc { 0, 0 };
                SIZE    sizeWnd { 196, 196 };
                UpdateLayeredWindow(hWnd, hdcScreen, &pPos, &sizeWnd, hdcMem, &pSrc, NULL, &blend, ULW_ALPHA);
}
*/

示例截图


snapshot

相关文章

  • 分层窗口

    示例代码 示例截图

  • 2021~02、01。  ireport 的使用

    使用ireport修改table样式,使用。窗口-report inspector-字体和边框。原理是字体边框分层...

  • WindowManagerService架构剖析之窗口分组与分层

    WindowManagerService工作方式 《WindowManagerService架构剖析之addWin...

  • 浅析在校教育班课的出路

    一、教育分层,做公立校不敢做之事;更精确的分层,更低廉的分层成本(教务方向)1、现在培训机构的分层是被动分层,培训...

  • 2020-03-23计算机网络体系结构与参考模型

    一 分层结构 ①分层的原因 ②如何分层 ③分层结构 ④概念总结 二 OSI参考模型 ①OSI的产生 ②OSI的结构...

  • 社会分层

    社会分层正在形成,体现在教育分层,收入分层,思维认知的分层。 教育分层:应试教育,穷人的教育形式;素质教育,中产阶...

  • 2021-10-26

    分层教学 分层作业 分层辅导 分层交流 分层谈心 怒不如忍,忍不如恕,恕不如爱 让孩子找到一个在学校生存下去的理由...

  • 人之间的层级分层 看十年窗口

    年底了,相信会有各种同学聚会,身边的朋友也在说,这几年,人与人之间的分层越来愈大。 这让我想到之前在网上的一个帖子...

  • 10年窗口期决定人与人的分层

    年底了,相信会有各种同学聚会,身边的朋友也在说,这几年,人与人之间的分层越来愈大。 这让我想到之前在网上的一个帖子...

  • 分层教学实施方案如何实行才最有效

    分层教学实施方案的主要模式可以分为几种: 一、班内分层目标教学模式(可称“分层教学、分类指导”教学模式)班内分层目...

网友评论

本文标题:分层窗口

本文链接:https://www.haomeiwen.com/subject/juyihttx.html