我刚刚完成了一个初步版本,不确定这是否符合您的要求,仅供参考。
最终效果
![输入图像描述](https://istack.dev59.com/KtaQu.gif)
编程步骤:
- 创建圆的轮廓(可以通过组合两个椭圆构建几何图形组来完成)。
- 创建半径渐变画刷,使用此画刷填充圆。
- 在每帧中旋转渐变画刷,以实现进度条的旋转效果。
完整代码:
注意,您需要链接d2d1.lib和winmm.lib才能使其正常工作。
由于您熟悉Direct2D,因此我不会详细介绍代码的细节,请在需要进一步讨论时留下评论。谢谢。您也可以在GitHub上下载完整项目此处。
#include <D2D1.h>
#define SAFE_RELEASE(P) if(P){P->Release() ; P = NULL ;}
const int GEOMETRY_COUNT = 2;
ID2D1Factory* g_pD2DFactory = NULL;
ID2D1HwndRenderTarget* g_pRenderTarget = NULL;
ID2D1SolidColorBrush* g_pBlackBrush = NULL;
ID2D1RadialGradientBrush* g_pRadialGradientBrush = NULL ;
D2D1_ELLIPSE g_Ellipse0 = D2D1::Ellipse(D2D1::Point2F(300, 300), 150, 150);
D2D1_ELLIPSE g_Ellipse1 = D2D1::Ellipse(D2D1::Point2F(300, 300), 200, 200);
D2D1_ELLIPSE g_Ellipse[GEOMETRY_COUNT] =
{
g_Ellipse0,
g_Ellipse1,
};
ID2D1EllipseGeometry* g_pEllipseArray[GEOMETRY_COUNT] = { NULL };
ID2D1GeometryGroup* g_pGeometryGroup = NULL;
VOID CreateD2DResource(HWND hWnd)
{
if (!g_pRenderTarget)
{
HRESULT hr ;
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pD2DFactory) ;
if (FAILED(hr))
{
MessageBox(hWnd, "Create D2D factory failed!", "Error", 0) ;
return ;
}
RECT rc ;
GetClientRect(hWnd, &rc) ;
hr = g_pD2DFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hWnd,
D2D1::SizeU(rc.right - rc.left,rc.bottom - rc.top)
),
&g_pRenderTarget
) ;
if (FAILED(hr))
{
MessageBox(hWnd, "Create render target failed!", "Error", 0) ;
return ;
}
hr = g_pRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&g_pBlackBrush
) ;
if (FAILED(hr))
{
MessageBox(hWnd, "Create outline brush(black) failed!", "Error", 0) ;
return ;
}
D2D1_GRADIENT_STOP gradientStops[2] ;
gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Blue) ;
gradientStops[0].position = 0.f ;
gradientStops[1].color = D2D1::ColorF(D2D1::ColorF::White) ;
gradientStops[1].position = 1.f ;
ID2D1GradientStopCollection* pGradientStops = NULL ;
hr = g_pRenderTarget->CreateGradientStopCollection(
gradientStops,
2,
D2D1_GAMMA_2_2,
D2D1_EXTEND_MODE_CLAMP,
&pGradientStops
) ;
if (FAILED(hr))
{
MessageBox(NULL, "Create gradient stops collection failed!", "Error", 0);
}
hr = g_pRenderTarget->CreateRadialGradientBrush(
D2D1::RadialGradientBrushProperties(
D2D1::Point2F(170, 170),
D2D1::Point2F(0, 0),
150,
150),
pGradientStops,
&g_pRadialGradientBrush
) ;
if (FAILED(hr))
{
MessageBox(hWnd, "Create linear gradient brush failed!", "Error", 0) ;
return ;
}
for (int i = 0; i < GEOMETRY_COUNT; ++i)
{
hr = g_pD2DFactory->CreateEllipseGeometry(g_Ellipse[i], &g_pEllipseArray[i]);
if (FAILED(hr))
{
MessageBox(hWnd, "Create Ellipse Geometry failed!", "Error", 0);
return;
}
}
hr = g_pD2DFactory->CreateGeometryGroup(
D2D1_FILL_MODE_ALTERNATE,
(ID2D1Geometry**)&g_pEllipseArray,
ARRAYSIZE(g_pEllipseArray),
&g_pGeometryGroup
);
}
}
VOID Render(HWND hwnd)
{
static float totalAngle = 0.0f;
static DWORD lastTime = timeGetTime();
DWORD currentTime = timeGetTime();
float timeDelta = (float)(currentTime - lastTime) * 0.1;
totalAngle += timeDelta;
CreateD2DResource(hwnd) ;
g_pRenderTarget->BeginDraw() ;
g_pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
g_pRenderTarget->DrawGeometry(g_pGeometryGroup, g_pBlackBrush);
D2D1_MATRIX_3X2_F rotMatrix = D2D1::Matrix3x2F::Rotation(totalAngle, D2D1::Point2F(300, 300));
g_pRadialGradientBrush->SetTransform(rotMatrix);
g_pRenderTarget->FillGeometry(g_pGeometryGroup, g_pRadialGradientBrush);
HRESULT hr = g_pRenderTarget->EndDraw() ;
if (FAILED(hr))
{
MessageBox(NULL, "Draw failed!", "Error", 0) ;
return ;
}
lastTime = currentTime;
}
VOID Cleanup()
{
SAFE_RELEASE(g_pRenderTarget) ;
SAFE_RELEASE(g_pBlackBrush) ;
SAFE_RELEASE(g_pGeometryGroup);
SAFE_RELEASE(g_pRadialGradientBrush);
for (int i = 0; i < GEOMETRY_COUNT; ++i)
{
SAFE_RELEASE(g_pEllipseArray[i]);
g_pEllipseArray[i] = NULL;
}
SAFE_RELEASE(g_pD2DFactory) ;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
Render(hwnd) ;
return 0 ;
case WM_KEYDOWN:
{
switch( wParam )
{
case VK_ESCAPE:
SendMessage( hwnd, WM_CLOSE, 0, 0 );
break ;
default:
break ;
}
}
break ;
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )
{
WNDCLASSEX winClass ;
winClass.lpszClassName = "Direct2D";
winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WndProc;
winClass.hInstance = hInstance;
winClass.hIcon = NULL ;
winClass.hIconSm = NULL ;
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground = NULL ;
winClass.lpszMenuName = NULL;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
if (!RegisterClassEx (&winClass))
{
MessageBox ( NULL, TEXT( "This program requires Windows NT!" ), "error", MB_ICONERROR) ;
return 0 ;
}
HWND hwnd = CreateWindowEx(NULL,
"Direct2D",
"Circular Progressbar",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
600,
600,
NULL,
NULL,
hInstance,
NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
MSG msg ;
ZeroMemory(&msg, sizeof(msg)) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}