毛玻璃

//贝神杰作,仅作记录,以后学习

#define GDIPVER 0x0110 //定义高版本的GDI+(1.1)
#include <tchar.h>
#include <windows.h>
#include <ObjIdl.h>
#include <stdio.h>
#include <GdiPlus.h>
#pragma comment(lib,"GdiPlus.lib")
using namespace Gdiplus;

#include <dwmapi.h>
#pragma comment(lib,"dwmapi.lib")

//Aero效果是否已启用
BOOL IsCompositionEnabled()
{
BOOL bEnabled,bResult;
bResult = (SUCCEEDED(DwmIsCompositionEnabled(&bEnabled)) && bEnabled);
return bResult;
}
//对已分层的窗口启动玻璃效果
HRESULT EnableBlurBehindWindow(HWND hWnd, //窗口句柄
BOOL bEnable = TRUE, //启用或禁用
HRGN hRgn = 0, //模糊窗体中某个区域
BOOL bTransitionOnMaximized = FALSE) //最大化时是否启用
{
DWM_BLURBEHIND blurBehind = { 0 };
blurBehind.dwFlags = DWM_BB_ENABLE | DWM_BB_TRANSITIONONMAXIMIZED;
blurBehind.fEnable = bEnable;
blurBehind.fTransitionOnMaximized = bTransitionOnMaximized;
if (bEnable && hRgn != NULL)
{
blurBehind.dwFlags |= DWM_BB_BLURREGION;
blurBehind.hRgnBlur = hRgn;
}
return DwmEnableBlurBehindWindow(hWnd,&blurBehind);
}
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HDC hDC = GetDC(hWnd);

//获得窗口宽高
RECT rcClient;
GetClientRect(hWnd,&rcClient);
UINT uWidth,uHeight;
uWidth = rcClient.right - rcClient.left;
uHeight = rcClient.bottom - rcClient.top;

//创建一个和窗口宽高相同的内存DC并创建相应位图
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC,uWidth,uHeight);
HBITMAP hBitmapOld = (HBITMAP)SelectObject(hMemDC,hBitmap);
SetBkMode(hMemDC, TRANSPARENT);
//对该内存DC进绘制Alpha图像
{
Gdiplus::Graphics grap(hMemDC);
Gdiplus::LinearGradientBrush lgb(
Point(0,0),
Point(0,uHeight),
Color(0,255,0,255),
Color(255,0,255,0));
grap.FillRectangle(&lgb,0,0,uWidth,uHeight);

SolidBrush b(Color(0xFF,0x00,0x00,0xFF)); //构造纯色画刷
FontFamily ff(L"Times New Roman"); //构造字体族
Font f( //构造字体
&ff, //字体族
24, //字体尺寸
FontStyleRegular, //字体样式
UnitPixel); //大小单元(像素)
PointF p(10.0F,20.0F); //构造精度点

grap.DrawString( //绘制字符串
L"Hello World!", //字符串
-1, //字符串长度(-1为以Null为结束符)
&f, //字体
p, //位置
&b); //颜色
}

{
LPCTSTR lpstrOut = _T("Hello World!!!");
RECT rc = { 100,100,300,120 };
SetTextColor( hMemDC, RGB(0xFF,0x00,0x00) | 0xFE000000 );
DrawText( hMemDC, lpstrOut, _tcslen(lpstrOut), &rc, DT_CENTER | DT_SINGLELINE | DT_VCENTER );
}
//设置透明效果
SIZE size = {uWidth,uHeight};
POINT point = {0,0};
BLENDFUNCTION BlendFunc;
BlendFunc.BlendOp = AC_SRC_OVER;
BlendFunc.BlendFlags = 0;
BlendFunc.SourceConstantAlpha = 0xFF;
BlendFunc.AlphaFormat = AC_SRC_ALPHA;
UpdateLayeredWindow(
hWnd,
NULL,
NULL,
&size,
hMemDC,
&point,
RGB(0,0,0),
&BlendFunc,
ULW_ALPHA);

if(IsCompositionEnabled() == TRUE)
{
EnableBlurBehindWindow(hWnd,TRUE);
}

//释放资源
SelectObject(hMemDC,hBitmapOld);
DeleteDC(hMemDC);
ReleaseDC(hWnd,hDC);
}
break;
case WM_PAINT:
{
PAINTSTRUCT PaintStruct;
HDC hDC = BeginPaint(hWnd,&PaintStruct);
//在此处添加重绘代码
EndPaint(hWnd,&PaintStruct);
}
break;
case WM_LBUTTONDOWN:
{
PostMessage( hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0 );
}
break;
case WM_DWMCOMPOSITIONCHANGED:
EnableBlurBehindWindow(hWnd,IsCompositionEnabled());
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}

INT WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE, LPTSTR, INT)
{
//初使化GDI+ ----------------------------------------
ULONG_PTR GdiplusToken;
GdiplusStartupInput GdiplusStartupInput;
Status sResult = GdiplusStartup(&GdiplusToken, &GdiplusStartupInput, NULL);
if(sResult != Ok)return 0;

WNDCLASSEX WndClassEx = 
{
sizeof(WNDCLASSEX), //窗口类大小 
CS_HREDRAW | CS_VREDRAW, //水平和垂直重绘
WinProc, //窗口过程
0L, //窗口类扩展
0L, //窗口扩展
GetModuleHandle(NULL), //实例句柄
LoadIcon(NULL,IDI_APPLICATION), //默认的窗口大图标
LoadCursor(NULL,IDC_ARROW), //默认的窗口光标
(HBRUSH)GetStockObject(WHITE_BRUSH),//默认的窗口背景画刷 
NULL, //默认的窗口菜单名称
_T("SimpleWindowClass"), //窗口类名
LoadIcon(NULL,IDI_APPLICATION) //默认的窗口小图像
};
RegisterClassEx(&WndClassEx);

HWND hWnd = CreateWindowEx(
WS_EX_LAYERED,
_T("SimpleWindowClass"), //使用的窗口类名
_T("Simple Window"), //窗口标题
WS_OVERLAPPEDWINDOW, //窗口样式
CW_USEDEFAULT, //默认的X坐标
CW_USEDEFAULT, //默认的Y坐标
CW_USEDEFAULT, //默认的宽度
CW_USEDEFAULT, //默认的高度
GetDesktopWindow(), //父窗口句柄 HWND_MESSAGE为隐藏窗口
NULL, //菜单句柄
WndClassEx.hInstance, //主模块句柄
NULL); //参数.

ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);

MSG Msg;
do
{
GetMessage(&Msg, NULL, 0U, 0U);
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}while(Msg.message != WM_QUIT);

UnregisterClass(
_T("SimpleWindowClass"),
WndClassEx.hInstance);

//关闭GDI+ ----------------------------------------
GdiplusShutdown(GdiplusToken);
return 0;
}

实现效果:


原文地址:https://www.cnblogs.com/lgh1992314/p/5835318.html