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);
}
uj5u.com熱心網友回復:

可以呀
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/25969.html
