微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

分层窗口大小大于内容大小

如何解决分层窗口大小大于内容大小

我想创建一个分层窗口,其大小大于其内容的大小 (hdcSrc)。例如,窗口是 900x900,我们可以将 300x300 的图像放在它里面我们想要的任何地方,而窗口的其余部分是透明的。

这是我目前的代码

#include <Windows.h>
#include <string>

#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment( lib,"Gdiplus.lib" )

HWND hwnd;

int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);

void updateTestwindow();

LRESULT CALLBACK windowProcedure(HWND hwnd,UINT message,WParaM wParam,LParaM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProc(hwnd,message,wParam,lParam);
    }
}

INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,PSTR,INT iCmdshow)
{
    MSG msg;

    // GDI+ elements
    GdiplusstartupInput gdiplusstartupInput;
    ULONG_PTR gdiplusToken;

    // Initialize GDI+.
    Gdiplusstartup(&gdiplusToken,&gdiplusstartupInput,NULL);

    // Register Window Class

    WNDCLASS testwindowClass;

    testwindowClass.lpszClassName = TEXT("Testwindow");
    testwindowClass.hInstance = hInstance;
    testwindowClass.style = CS_HREDRAW | CS_VREDRAW;
    testwindowClass.lpfnWndProc = windowProcedure;
    testwindowClass.hIcon = 0;
    testwindowClass.hCursor = LoadCursor(NULL,IDC_ARROW);
    testwindowClass.hbrBackground = (HBrush)GetStockObject(WHITE_Brush);
    testwindowClass.cbClsExtra = 0;
    testwindowClass.cbWndExtra = 0;
    testwindowClass.lpszMenuName = NULL;

    RegisterClass(&testwindowClass);

    // Create a layered window

    hwnd = CreateWindowEx(
        WS_EX_layered,TEXT("Testwindow"),// window class name
        TEXT("Testwindow"),// window caption
        WS_POPUP,// window style
        0,// initial x position
        0,// initial y position
        screenWidth,// initial x size
        screenHeight,// initial y size
        NULL,// parent window handle
        NULL,// window menu handle
        hInstance,// program instance handle
        NULL);              // creation parameters

    ShowWindow(hwnd,iCmdshow);

    
    // Update the layered window using a custom function
    updateTestwindow();




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

    return 0;
}


void updateTestwindow()
{
    // Let's imagine that in reality we are drawing an image,but to test it,just draw a rectangle
    int imageWidth = 300;
    int imageHeight = 300;

    HDC screenDC = GetDC(NULL);

    // Create a DC for drawing
    HDC drawingDC = CreateCompatibleDC(screenDC);

    // Create a new bitmap for drawing on it
    HBITMAP newBitmap = CreateCompatibleBitmap(screenDC,imageWidth,imageHeight);

    // Select the new bitmap to draw on it and save the old one
    HBITMAP oldBitmap = (HBITMAP)SelectObject(drawingDC,newBitmap);

    // Create graphics object
    Graphics graphics(drawingDC);

    // Draw a rectangle
    Pen redPen(Color(255,255,0),15);
    Rect windowRect(0,imageWidth);
    graphics.DrawRectangle(&redPen,windowRect);

    // The position of the layered window
    POINT windowPosition = { 0,0 };
    
    // The size of the layered window   
    SIZE windowSize = { imageWidth,imageHeight };    // <----------------------------------!!!!

    POINT drawingDCPosition = { 0,0 };

    // Create a blend function
    BLENDFUNCTION blend = { 0 };
    blend.BlendOp = AC_SRC_OVER;
    blend.sourceConstantAlpha = 255;
    blend.AlphaFormat = AC_SRC_ALPHA;

    // Call UpdatelayeredWindow
    UpdatelayeredWindow(hwnd,screenDC,&windowPosition,&windowSize,drawingDC,&drawingDCPosition,&blend,ULW_ALPHA);

    // Clean up
    SelectObject(drawingDC,oldBitmap);
    DeleteObject(newBitmap);
    DeleteDC(drawingDC);
    ReleaseDC(NULL,screenDC);
}

这将创建一个窗口,其大小与在其上绘制的图像的大小完全相同。 我们可以减小窗口大小:SIZE windowSize = { imageWidth-100,imageHeight-100 }; 并且窗口将按预期绘制 - 较小的尺寸,而图像大小相同但现在它被剪裁了(在这种情况下,矩形被剪裁了)。

但是,如果我们想要实现我在开头描述的内容SIZE windowSize = { imageWidth+100,imageHeight+100 }; 不起作用。窗口根本没有渲染,但它会打开(窗口任务栏图标可见)。

我可能还不能完全理解分层窗口是如何工作的,这就是为什么我不明白为什么在这个例子中它没有被渲染。

是否有可能创建这样一个分层窗口?如果是,那么如何创建?

也许这根本不是我应该做的事情?

就像一个说明:我想这样做的原因是将分层窗口用作一种叠加层,这将始终是屏幕的大小,但图像、图形和文本将被绘制到它,更新,移动等等。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。