使用UpdateLayeredWindow窗口不能显示。

平罗勒斯第四维度 2019-06-20 06:06:02
updateLayeredWindow的最后一个参数改为ULW_ALPHA后窗口无法显示,win10和win7都试了。我用的的确是png32位的图片,是有alpha通道的,可是还是透明。lend.AlphaFormat不设置AC_SRC_ALPHA这个参数可以实现半透明,但是不是我想要的根据alpha通道灵活实现透明效果。求知道的大神指点一二,看了一天也没弄好。

#include <windows.h>
#include <GdiPlus.h>
//本代码用于建立特殊形状窗口
#pragma comment (lib, "GdiPlus.lib")

using namespace Gdiplus;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int

nCmdShow)
{
TCHAR szAppName[] = TEXT("Windows程序设计");
WNDCLASSEX wndClass;
MSG msg;
HWND hwnd;
ULONG_PTR token;
GdiplusStartupInput gin;
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpszClassName = TEXT("AppTest");
wndClass.lpszMenuName = NULL;
wndClass.lpfnWndProc = WndProc;
wndClass.style = CS_HREDRAW | CS_VREDRAW | CS_IME | CS_DBLCLKS;

GdiplusStartup(&token, &gin, NULL);
RegisterClassEx(&wndClass);
hwnd = CreateWindowEx(WS_EX_LAYERED, TEXT("AppTest"), szAppName, WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_POPUP/*无边框风格*/
, 0, 0, 100, 100,
NULL, NULL, hInstance, NULL);
//设置本窗口为分层窗口支持透明
//分层窗口没有WM_PAINT消息
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GdiplusShutdown(token);
return 0;
}
void CreateRoundRectangle(Rect rc, int Round, GraphicsPath * gp)
{
gp->AddArc(0, 0, Round, Round, 180, 90);
gp->AddLine(Round, 0, rc.Width - Round, 0);
gp->AddArc(rc.Width - Round, 0, Round, Round, 270, 90);
gp->AddLine(rc.Width, Round, rc.Width, rc.Height - Round);
gp->AddArc(rc.Width - Round, rc.Height - Round, Round, Round, 0, 90);
gp->AddLine(rc.Width - Round, rc.Height, Round, rc.Height);
gp->AddArc(0, rc.Height - Round, Round, Round, 90, 90);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static BOOL ldown;
static POINT TheFirstPoint;
static int cxClient, cyClient;

switch (msg)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
break;
case WM_LBUTTONDOWN:
ldown = TRUE;
SetCapture(hwnd);//这句代码可以进行容错处理,谁知道那次window响应速度慢了你,不信?'-_-'去掉这句代码试一试
TheFirstPoint.x = LOWORD(lParam);
TheFirstPoint.y = HIWORD(lParam);
break;
case WM_LBUTTONUP:
ldown = FALSE;
ReleaseCapture();
break;
case WM_MOUSEMOVE:
if (ldown)
{
POINT pt;
GetCursorPos(&pt);
pt.x -= TheFirstPoint.x;
pt.y -= TheFirstPoint.y;
SetWindowPos(hwnd, NULL, pt.x, pt.y, NULL, NULL, SWP_NOREDRAW |

SWP_NOSIZE | SWP_NOZORDER);
}
break;
case WM_LBUTTONDBLCLK:
DestroyWindow(hwnd);
case WM_CREATE:
{
Image image(TEXT("bianping-christmas-2014-12.png"));
//加载窗口图形
int iWidth = image.GetWidth();
int iHeight = image.GetHeight();
HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, iWidth, iHeight);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
Graphics gp(hdcMem);
//在这里建立窗口的图形,可以用代码来生成
GraphicsPath graphicspath;
SolidBrush brush(Color(255, 175, 0, 0));
CreateRoundRectangle(Rect(0, 0, iWidth, iHeight), 20, &graphicspath);//创建圆角矩形路径
// gp.FillPath(&brush, &graphicspath);//填充路径 ----去掉注释查看效果
gp.DrawImage(&image, 5, 5, 90, 90);//将png图像绘制到后台DC中

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 = { iWidth, iHeight };
UpdateLayeredWindow(hwnd, hdcScreen, &pPos, &sizeWnd, hdcMem, &pSrc, NULL, &blend, ULW_ALPHA);//更新分层窗口
//收尾清理工作
SelectObject(hdcMem, hBitmapOld);
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdcScreen);
}
break;
case WM_PAINT:
MessageBox(NULL, NULL, NULL, NULL);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
...全文
163 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhm520bn 2019-07-16
  • 打赏
  • 举报
回复



可以呀

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧