ウールガラス

4730 ワード

//    ,    ,    

#define GDIPVER 0x0110 //      GDI+(1.1)
#include 
#include 
#include 
#include 
#include 
#pragma comment(lib,"GdiPlus.lib")
using namespace Gdiplus;

#include 
#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;
}

効果: