示例代码
#include <windows.h>
#include <gdiplus.h>
#include <iostream>
#pragma comment(lib, "Gdiplus.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
using namespace Gdiplus;
using namespace std;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
/*开启Gdi+*/
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
WNDCLASS wndClass;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = 0;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = "app";
cout << RegisterClass(&wndClass) << endl;
HWND hWnd;
hWnd = CreateWindowEx(
WS_EX_LAYERED | WS_EX_TOOLWINDOW,
"app",
TEXT("The hello program"),
WS_POPUP,
0, 0, 196, 196,
NULL, NULL, 0, NULL
);
cout << hWnd << endl;
ShowWindow(hWnd, 1);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
static bool ldown;
static POINT point;
switch (message) {
case WM_LBUTTONDOWN:
ldown = true;
SetCapture(hWnd);
point.x = LOWORD(lParam);
point.y = HIWORD(lParam);
break;
case WM_LBUTTONUP:
ldown = false;
ReleaseCapture();
break;
case WM_MOUSEMOVE:
if (ldown) {
POINT pt;
GetCursorPos(&pt);
pt.x -= point.x;
pt.y -= point.y;
SetWindowPos(
hWnd,
NULL,
pt.x,
pt.y,
NULL, NULL,
SWP_NOREDRAW | SWP_NOSIZE | SWP_NOZORDER
);
}
break;
case WM_CREATE:
{
//在exe同级目录的图片
Image image(L"bkg.png");
int iW = image.GetWidth(),
iH = image.GetHeight();
HDC hdcScreen = GetDC(0),
hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hmap = CreateCompatibleBitmap(hdcScreen, iW, iH);
SelectObject(hdcMem, hmap);
Graphics g(hdcMem);
g.DrawImage(&image, 0, 0);
// g.Clear(Color(100, 0, 0, 0));
MoveWindow(
hWnd,
0, 0,
iW, iH,
true
);
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 = { iW, iH };
UpdateLayeredWindow(hWnd, hdcScreen, &pPos, &sizeWnd, hdcMem, &pSrc, NULL, &blend, ULW_ALPHA);
}
break;
case WM_PAINT :
break;
case WM_DESTROY :
PostQuitMessage(0);
break ;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
/*
Direct2D
{
HDC hdcScreen = GetDC(0),
hdcMem = CreateCompatibleDC(hdcScreen);
HBITMAP hmap = CreateCompatibleBitmap(hdcScreen, 196, 196);
SelectObject(hdcMem, hmap);
// d2d1 工厂
ID2D1Factory *m_pDirect2dFactory;
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pDirect2dFactory);
IWICImagingFactory *pFactory = NULL;
IWICBitmapDecoder *pDecoder = NULL;
CoInitialize(NULL);
CoCreateInstance(
CLSID_WICImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IWICImagingFactory,
(LPVOID*)&pFactory
);
// 读取图片
IWICBitmapFrameDecode *pSource = NULL;
IWICStream *pStream = NULL;
IWICFormatConverter *pConverter = NULL;
IWICBitmapScaler *pScaler = NULL;
pFactory->CreateDecoderFromFilename(
L"bkg.png",
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pDecoder
);
pDecoder->GetFrame(0, &pSource);
pFactory->CreateFormatConverter(&pConverter);
HRESULT hr = pFactory->CreateBitmapScaler(&pScaler);
if (SUCCEEDED(hr)) {
hr = pScaler->Initialize(
pSource,
196,
196,
WICBitmapInterpolationModeCubic
);
}
if (SUCCEEDED(hr)) {
hr = pConverter->Initialize(
pScaler,
GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone,
NULL,
0.f,
WICBitmapPaletteTypeMedianCut
);
}
RECT rc;
GetClientRect(hWnd, &rc);
D2D1_SIZE_U size = SizeU(
rc.right - rc.left,
rc.bottom - rc.top
);
// Create a Direct2D render target.
D2D1_RENDER_TARGET_PROPERTIES props = D2D1::RenderTargetProperties(
D2D1_RENDER_TARGET_TYPE_DEFAULT,
D2D1::PixelFormat(
DXGI_FORMAT_B8G8R8A8_UNORM,
D2D1_ALPHA_MODE_PREMULTIPLIED),
0,
0,
D2D1_RENDER_TARGET_USAGE_NONE ,
D2D1_FEATURE_LEVEL_10
);
ID2D1DCRenderTarget *m_pRenderTarget;
m_pDirect2dFactory->CreateDCRenderTarget(
&props,
&m_pRenderTarget
);
m_pRenderTarget->BindDC(hdcMem, &rc);
ID2D1Bitmap *bitmap;
m_pRenderTarget->CreateBitmapFromWicBitmap(pConverter, 0, &bitmap);
m_pRenderTarget->BeginDraw();
m_pRenderTarget->DrawBitmap(
bitmap
);
m_pRenderTarget->EndDraw();
BLENDFUNCTION blend;
blend.BlendOp = AC_SRC_OVER;
blend.SourceConstantAlpha = 255;
MoveWindow(
hWnd,
0, 0,
196, 196,
true
);
blend.AlphaFormat = AC_SRC_ALPHA;
POINT pPos { 0, 0 };
POINT pSrc { 0, 0 };
SIZE sizeWnd { 196, 196 };
UpdateLayeredWindow(hWnd, hdcScreen, &pPos, &sizeWnd, hdcMem, &pSrc, NULL, &blend, ULW_ALPHA);
}
*/
示例截图
snapshot
网友评论