这种进度条怎么做?

danxuezx 2018-07-29 08:38:35

如图,上面这种进度条怎么做?
...全文
1094 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
longyinfengwu 2018-09-01
  • 打赏
  • 举报
回复
背景成渐变色了 进度条没变 也没有圆角
yzhfirst1 2018-08-09
  • 打赏
  • 举报
回复
http://www.codeproject.com/
oblivi 2018-08-01
  • 打赏
  • 举报
回复
引用 12 楼 danxuezx 的回复:
d

[quote=引用 5 楼 oblivi 的回复:]

Direct2D

赞,示例源码有不[/quote]

引用 8 楼 oblivi 的回复:
百分号打反了
Direct2D代码
#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;
}
schlafenhamster 2018-08-01
  • 打赏
  • 举报
回复
8楼 不是 代码吗 ? 没看 ?
danxuezx 2018-08-01
  • 打赏
  • 举报
回复
d

引用 5 楼 oblivi 的回复:

Direct2D

赞,示例源码有不
schlafenhamster 2018-07-30
  • 打赏
  • 举报
回复
英文 叫 某某 percent(%)
schlafenhamster 2018-07-30
  • 打赏
  • 举报
回复
因为 叫 某某 percent(%)
Eleven 2018-07-30
  • 打赏
  • 举报
回复
GDI贴图实现~
xiao___sun 2018-07-29
  • 打赏
  • 举报
回复
自绘或者贴Bitmap
oblivi 2018-07-29
  • 打赏
  • 举报
回复
百分号打反了
Direct2D代码
#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;
}
schlafenhamster 2018-07-29
  • 打赏
  • 举报
回复
可用的代码

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);
}
}

schlafenhamster 2018-07-29
  • 打赏
  • 举报
回复
1 两头 圆
void CExProgressCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CRect rc;
GetWindowRect(rc);
rc.OffsetRect(-rc.left,-rc.top);
CRgn rg;
rg.CreateRoundRectRgn(rc.left, rc.top,rc.right,rc.bottom,
rc.Height(),rc.Height());
//
SetWindowRgn(rg,TRUE);
//
CProgressCtrl::PreSubclassWindow();
}
2 渐变 条 (GDI)
void CCWinListBox::DrawGradientBackground( CDC* pDC, CRect rRect, 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)) / iWidth;// 20
int idG = (256*(GetGValue(crTo)-iG)) / iWidth;// 8
int idB = (256*(GetBValue(crTo)-iB)) / iWidth;// -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 ) );
}
}
oblivi 2018-07-29
  • 打赏
  • 举报
回复

Direct2D
oblivi 2018-07-29
  • 打赏
  • 举报
回复
从CWnd派生
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
oblivi 2018-07-29
  • 打赏
  • 举报
回复

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧