问题描述
我想创建一个分层窗口,其大小大于其内容的大小 (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 (将#修改为@)