15,979
社区成员
发帖
与我相关
我的任务
分享
#pragma once
#include <d2d1.h>
#include <dwrite.h>
#pragma comment(lib,"d2d1.lib")
#pragma comment(lib,"dwrite.lib")
using namespace D2D1;
// CProgressEx
class CProgressEx : public CWnd
{
DECLARE_DYNAMIC(CProgressEx)
public:
CProgressEx();
virtual ~CProgressEx();
ID2D1Factory *m_pFactory;
ID2D1HwndRenderTarget *m_pRT;
IDWriteTextFormat *m_pFormat;
IDWriteFactory *m_pWriteFactory;
ID2D1GradientStopCollection *m_pCollection;
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
// CProgressEx 消息处理程序
void CProgressEx::OnPaint()
{
CPaintDC dc(this);
CRect rect, rt;
GetClientRect(rect);
rt = rect;
ID2D1SolidColorBrush *pSolidBrush = nullptr;
ID2D1LinearGradientBrush *pLinearBrush = nullptr;
m_pRT->CreateSolidColorBrush(ColorF(GetSysColor(COLOR_3DFACE)), &pSolidBrush);
m_pRT->BeginDraw();
m_pRT->FillRectangle(RectF(rect.left, rect.top, rect.right, rect.bottom), pSolidBrush);
pSolidBrush->Release();
m_pRT->CreateSolidColorBrush(ColorF(ColorF::DarkSlateBlue), &pSolidBrush);
m_pRT->FillRoundedRectangle(RoundedRect(RectF(rect.left, rect.top, rect.right, rect.bottom), 8.f, 8.f), pSolidBrush);
pSolidBrush->Release();
m_pRT->CreateSolidColorBrush(ColorF(ColorF::Azure), &pSolidBrush);
rect.DeflateRect(10, 8, 10, 8);
m_pRT->CreateLinearGradientBrush(LinearGradientBrushProperties(Point2F(0.f, 0.f),
Point2F(rect.right, rect.bottom)), m_pCollection, &pLinearBrush);
int num = 72;
m_pRT->FillRoundedRectangle(RoundedRect(RectF(rect.left, rect.top, rect.right, rect.bottom), 4, 4), pSolidBrush);
m_pRT->FillRoundedRectangle(RoundedRect(RectF(rect.left, rect.top, rect.right * num/ 100, rect.bottom), 4, 4), pLinearBrush);
CString str;
str.Format(_T("%d%%"), num);
pSolidBrush->Release();
m_pRT->CreateSolidColorBrush(ColorF(ColorF::Snow), &pSolidBrush);
m_pRT->DrawText(str, str.GetLength(), m_pFormat, RectF(rt.left, rt.top, rt.right, rt.bottom), pSolidBrush);
m_pRT->EndDraw();
pSolidBrush->Release();
pLinearBrush->Release();
}
int CProgressEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
CRect rect;
GetClientRect(rect);
HRESULT res = D2D1CreateFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, &m_pFactory);
res = m_pFactory->CreateHwndRenderTarget(RenderTargetProperties(),
HwndRenderTargetProperties(m_hWnd, SizeU(rect.Width(),rect.Height())), &m_pRT);
res = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), (IUnknown**)&m_pWriteFactory);
res = m_pWriteFactory->CreateTextFormat(L"Times New Roman", nullptr, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 15.f, L"", &m_pFormat);
m_pFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
m_pFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
D2D1_GRADIENT_STOP stops[7];
stops[0].color = ColorF(ColorF::Red);
stops[0].position = 0.0f;
stops[1].color = ColorF(ColorF::Orange);
stops[1].position = 0.17f;
stops[2].color = ColorF(ColorF::Yellow);
stops[2].position = 0.35f;
stops[3].color = ColorF(ColorF::Green);
stops[3].position = 0.52f;
stops[4].color = ColorF(ColorF::Cyan);
stops[4].position = 0.67f;
stops[5].color = ColorF(ColorF::Blue);
stops[5].position = 0.83f;
stops[6].color = ColorF(ColorF::Purple);
stops[6].position = 1.0f;
res = m_pRT->CreateGradientStopCollection(stops, 7, &m_pCollection);
return 0;
}
void CTextProgressCtrl::DrawGradientBackground(CDC* pDC,CRect rRect,int TotalWidth,
COLORREF crFrom,COLORREF crTo)
{
int iHeight = rRect.Height();
int iWidth = rRect.Width();
// m_FR_Background1.SetColor(RGB(183,235,255));
// m_FR_Background2.SetColor(RGB(255,255,11));
int iR = GetRValue( crFrom );//183
int iG = GetGValue( crFrom );//235
int iB = GetBValue( crFrom );//255
//
int idR = (256*(GetRValue(crTo)-iR)) / TotalWidth;// 20
int idG = (256*(GetGValue(crTo)-iG)) / TotalWidth;// 8
int idB = (256*(GetBValue(crTo)-iB)) / TotalWidth;// -97
iR *= 256;
iG *= 256;
iB *= 256;
// ->
for ( int i = rRect.left; i <= iWidth; i++, iR += idR, iG += idG, iB += idB )
{
pDC->FillSolidRect(i, rRect.top, 1 , iHeight, RGB( iR / 256, iG / 256, iB / 256 ) );
}
}
//
void CTextProgressCtrl::OnPaint()
{
// if (m_nMin >= m_nMax) return;
// double Fraction = (double)(m_nPos - m_nMin) / ((double)(m_nMax - m_nMin));
double Fraction = (double)(m_nPos - m_nMin) /100.0;
// m_nPos 进度条 位置
CRect LeftRect, RightRect, ClientRect;
GetClientRect(ClientRect);
//
CPaintDC dc(this); // device context for painting (if not double buffering)
LeftRect = RightRect = ClientRect;
//
LeftRect.right = LeftRect.left + (int)((LeftRect.right - LeftRect.left)*Fraction);
DrawGradientBackground(&dc,LeftRect,ClientRect.Width(),RGB(0,0,255),RGB(0,255,0));
RightRect.left = LeftRect.right;
dc.FillSolidRect(RightRect, ::GetSysColor(COLOR_WINDOW));
// m_bShowText=FALSE;
if (m_bShowText)
{
CString str;
// if (m_strText.GetLength())
// str = m_strText;
// else
str.Format("%d%%", (int)(Fraction*100.0));
dc.SetBkMode(TRANSPARENT);
// left text
CRgn rgn;
rgn.CreateRectRgn(LeftRect.left, LeftRect.top, LeftRect.right, LeftRect.bottom);
dc.SelectClipRgn(&rgn);
dc.SetTextColor(::GetSysColor(COLOR_WINDOW));
dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
// right text
rgn.DeleteObject();
rgn.CreateRectRgn(RightRect.left, RightRect.top, RightRect.right, RightRect.bottom);
dc.SelectClipRgn(&rgn);
dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHT));
dc.DrawText(str, ClientRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
void CProgressEx::OnPaint()
{
CPaintDC dc(this);
CMemDC mdc(dc, this);
CDC &memDC = mdc.GetDC();
CRect rect;
GetClientRect(rect);
memDC.FillSolidRect(rect, GetSysColor(COLOR_3DFACE));
Graphics *graphics = new Graphics(memDC);
CRgn rgn;
rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, 6, 6);
Gdiplus::Region *frgn = Region::FromHRGN(rgn);
graphics->FillRegion(&SolidBrush(Color::PaleGreen), frgn);
rect.DeflateRect(10, 8, 10, 8);
LinearGradientBrush brush(RectF(rect.left, rect.top, rect.Width(), rect.Height()),
Color::Blue, Color::Red, LinearGradientModeHorizontal);
int colors[] = { Color::Red ,Color::Orange ,Color::Yellow ,Color::Green ,Color::Cyan,Color::Blue ,Color::Purple };
REAL positions[] = { 0.0f,0.16f,0.33f,0.50f,0.67f,0.83f,1.0f };
brush.SetInterpolationColors((Color*)colors, positions, 7);
rgn.DeleteObject();
rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, 5, 5);
frgn->~Region();
frgn = Region::FromHRGN(rgn);
graphics->FillRegion(&SolidBrush(Color::Azure), frgn);
int num = 68;
rgn.DeleteObject();
rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right*num / 100, rect.bottom, 5, 5);
frgn->~Region();
frgn = Region::FromHRGN(rgn);
graphics->FillRegion(&brush, frgn);
GetClientRect(rect);
CString str;
str.Format(_T("%%%d"), num);
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextColor(RGB(255,100,100));
memDC.DrawText(str, rect, DT_CENTER | DT_SINGLELINE | DT_VCENTER);
delete graphics;
}
Gdiplus