#include <Windows.h>
#include <tchar.h>
#include <dwmapi.h>
#include <GdiPlus.h>
#include <VSStyle.h>
#include <vssym32.h>
using namespace Gdiplus;
const LPCTSTR WND_CLASS_NAME = _T("DWMComposition");
HWND g_hWnd = NULL;
Bitmap *bmp = NULL;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HRESULT EnableBlurBehind(HWND hWnd, HRGN hRgn = NULL);
HRESULT RegisterThumbnail(HWND hWnd);
void DrawGlowingText(HDC hDC, LPWSTR szText, RECT &rcArea,
DWORD dwTextFlags = DT_LEFT | DT_VCENTER | DT_SINGLELINE, int iGlowSize = 10);
int WINAPI _tWinMain( HINSTANCE, HINSTANCE, LPTSTR, int )
{
//初始化GDI+
ULONG_PTR token;
GdiplusStartupInput input;
GdiplusStartup(&token, &input, NULL);
//加载图片
bmp = Bitmap::FromFile(L"Ferrari.png", false);
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.lpszClassName = WND_CLASS_NAME;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = wcex.hIcon;
RegisterClassEx(&wcex);
g_hWnd = CreateWindow(WND_CLASS_NAME, _T("This is 标题"), WS_OVERLAPPEDWINDOW, 100, 100,
800, 450, NULL, NULL, GetModuleHandle(NULL), NULL);
ShowWindow(g_hWnd, SW_SHOW);
UpdateWindow(g_hWnd);
MSG msg = {0};
while(msg.message != WM_QUIT)
{
if(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
UnregisterClass(WND_CLASS_NAME, GetModuleHandle(NULL));
//释放图片
delete bmp;
//关闭GDI+
GdiplusShutdown(token);
return 0;
}
HRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
//不要直接使用窗口句柄创建Graphics,会导致闪烁
Graphics graph(hDC);
//首先清除客户区域
RECT rcClient;
GetClientRect(hWnd, &rcClient);
BOOL bCompEnabled;
DwmIsCompositionEnabled(&bCompEnabled);
SolidBrush br(bCompEnabled? Color::Black : Color::DarkGray);
graph.FillRectangle(&br, Rect(rcClient.left, rcClient.top,
rcClient.right, rcClient.bottom));
//绘制图形
int width = bmp->GetWidth();
int height = bmp->GetHeight();
Rect rc(30, 30, width, height);
graph.DrawImage(bmp, rc, 0, 0, width, height, UnitPixel);
//绘制文本
RECT rcText = {10, 10, 300, 40};
DrawGlowingText(hDC, L" 一点点中文 and some english", rcText);
EndPaint(hWnd, &ps);
}
break;
case WM_CHAR:
switch(wParam)
{
case 'q': //开启客户区毛玻璃效果
{
RECT rect;
GetWindowRect(hWnd, &rect);
int width = 300, height = 200;
//居中椭圆形
HRGN hRgn = CreateEllipticRgn((rect.right - rect.left)/2 - width/2,
(rect.bottom - rect.top)/2 - height/2, (rect.right - rect.left)/2 + width/2,
(rect.bottom - rect.top)/2 + height/2);
EnableBlurBehind(hWnd, hRgn);
DeleteObject(hRgn);
}
break;
case 'w': //窗口边框向客户区扩展
{
MARGINS margins = {50, 50, 50, 50};
MARGINS margins2 = {-1}; //将扩展到整个客户区
DwmExtendFrameIntoClientArea(hWnd, &margins2);
}
break;
case 'e': //注册缩略图关联
RegisterThumbnail(hWnd);
break;
case 'x': //重新绘制窗口,测试用
InvalidateRect(hWnd, NULL, false);
break;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
HRESULT EnableBlurBehind( HWND hWnd, HRGN hRgn )
{
DWM_BLURBEHIND bb = {0};
bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
bb.fEnable = true;
bb.hRgnBlur = hRgn;
return DwmEnableBlurBehindWindow(hWnd, &bb);
}
HRESULT RegisterThumbnail( HWND hWnd )
{
HRESULT hr = S_OK;
HTHUMBNAIL thumbnail = NULL;
HWND hWndSrc = FindWindow(_T("QQPlayer Window"), NULL);
hr = DwmRegisterThumbnail(hWnd, hWndSrc, &thumbnail);
if (SUCCEEDED(hr))
{
RECT rc;
GetClientRect(hWnd, &rc);
DWM_THUMBNAIL_PROPERTIES dskThumbProps;
dskThumbProps.dwFlags = DWM_TNP_RECTDESTINATION | DWM_TNP_VISIBLE | DWM_TNP_OPACITY ;
dskThumbProps.fVisible = TRUE;
dskThumbProps.opacity = 200;
dskThumbProps.rcDestination = rc;
hr = DwmUpdateThumbnailProperties(thumbnail,&dskThumbProps);
}
return hr;
}
//绘制发光文字
void DrawGlowingText( HDC hDC, LPWSTR szText, RECT &rcArea, DWORD dwTextFlags , int iGlowSize )
{
//获取主题句柄
HTHEME hThm = OpenThemeData(GetDesktopWindow(), L"TextStyle");
//创建DIB
HDC hMemDC = CreateCompatibleDC(hDC);
BITMAPINFO bmpinfo = {0};
bmpinfo.bmiHeader.biSize = sizeof(bmpinfo.bmiHeader);
bmpinfo.bmiHeader.biBitCount = 32;
bmpinfo.bmiHeader.biCompression = BI_RGB;
bmpinfo.bmiHeader.biPlanes = 1;
bmpinfo.bmiHeader.biWidth = rcArea.right - rcArea.left;
bmpinfo.bmiHeader.biHeight = -(rcArea.bottom - rcArea.top);
HBITMAP hBmp = CreateDIBSection(hMemDC, &bmpinfo, DIB_RGB_COLORS, 0, NULL, 0);
if (hBmp == NULL) return;
HGDIOBJ hBmpOld = SelectObject(hMemDC, hBmp);
//绘制选项
DTTOPTS dttopts = {0};
dttopts.dwSize = sizeof(DTTOPTS);
dttopts.dwFlags = DTT_GLOWSIZE | DTT_COMPOSITED;
dttopts.iGlowSize = iGlowSize; //发光的范围大小
//绘制文本
RECT rc = {0, 0, rcArea.right - rcArea.left, rcArea.bottom - rcArea.top};
HRESULT hr = DrawThemeTextEx(hThm, hMemDC, TEXT_LABEL, 0, szText, -1, dwTextFlags , &rc, &dttopts);
if(FAILED(hr)) return;
BitBlt(hDC, rcArea.left, rcArea.top, rcArea.right - rcArea.left,
rcArea.bottom - rcArea.top, hMemDC, 0, 0, SRCCOPY | CAPTUREBLT);
//Clear
SelectObject(hMemDC, hBmpOld);
DeleteObject(hBmp);
DeleteDC(hMemDC);
CloseThemeData(hThm);
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页