GDI+画罗盘

6 篇文章 0 订阅
#include "gdiplus.h"
using namespace Gdiplus;  
#pragma comment(lib, "gdiplus.lib")
ULONG_PTR m_gdiplusToken;
GdiplusStartupInput gdiplusStartupInput;

SizeF GetTextBounds(const Gdiplus::Font& font,const StringFormat& strFormat,const CString& szText)
{
GraphicsPath graphicsPathObj;
FontFamily fontfamily;
font.GetFamily(&fontfamily);
graphicsPathObj.AddString(szText,-1,&fontfamily,font.GetStyle(),font.GetSize(),\


PointF(0,0),&strFormat);

RectF rcBound;

/// 获取边界范围
graphicsPathObj.GetBounds(&rcBound);
/// 返回文本的宽高
return SizeF(rcBound.Width,rcBound.Height);
}

CgdiplustestDlg::CgdiplustestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CgdiplustestDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);


GdiplusStartup(&m_gdiplusToken,&gdiplusStartupInput,NULL);
}
CgdiplustestDlg::~CgdiplustestDlg()
{
GdiplusShutdown(m_gdiplusToken);
}

void CgdiplustestDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);


// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
CDC *pDC=GetDC();
Graphics graphics(pDC->m_hDC);

//FontFamily fontFamily(L"幼圆");
//Gdiplus::Font font(&fontFamily,20,FontStyleRegular,UnitPoint);
//LinearGradientBrush linGrBrush(Point(0,0), Point(10,50), Color(255, 255, 0, 0), Color(255, 0, 0, 255) );
//WCHAR str5[256];
//PointF pointF(30,100);
//wcscpy(str5, L"渐变色刷子");
//pointF.Y+=30;
//graphics.DrawString(str5,(int)wcslen(str5),&font,pointF,&linGrBrush);

FontFamily fontFamily2(L"楷体");
Gdiplus::Font font1(&fontFamily2,10,FontStyleRegular,UnitPixel);
SolidBrush solidBrush(Color(255,255,0,255));
SolidBrush brush2(Color(255,255,0,0));

WCHAR str3[]=L"消除走样,且边做平滑处理";
graphics.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);

int R = 100;
int nLen = 5;
graphics.TranslateTransform(150,150);//将原点移动到
Pen newPen(Color(0,255,0),1);
Pen pen(Color(0,255,0),1);
int nR = R;
graphics.DrawEllipse(&pen,-nR,-nR,2*nR,2*nR);
wsprintfW(str3,L"%d°",90);
graphics.DrawString(str3,(int)wcslen(str3),&font1,PointF(0,-nR),&brush2);
nR = R*4/5;
graphics.DrawEllipse(&pen,-nR,-nR,2*nR,2*nR);
wsprintfW(str3,L"%d°",45);
graphics.DrawString(str3,(int)wcslen(str3),&font1,PointF(0,-nR),&brush2);
nR = R*2/5;
graphics.DrawEllipse(&pen,-nR,-nR,2*nR,2*nR);
wsprintfW(str3,L"%d°",0);
graphics.DrawString(str3,(int)wcslen(str3),&font1,PointF(0,-nR),&brush2);
nR = R*1/4;
graphics.DrawEllipse(&pen,-nR,-nR,2*nR,2*nR);
wsprintfW(str3,L"%d°",-45);
graphics.DrawString(str3,(int)wcslen(str3),&font1,PointF(0,-nR),&brush2);

R = 100;
graphics.DrawLine(&pen,-R,0,R,0);
graphics.DrawLine(&pen,0,R,0,-R);
//string str;
for (int i = 0 ;i < 360; i += 5 )
{
nLen = 5;
if (0 == i%15)
{
nLen = 8;
wsprintfW(str3,L"%d",i>180?i-360:i);
SizeF sf = GetTextBounds(font1,0,str3);
int nWidth = sf.Width/2 + 1;
graphics.DrawString(str3,(int)wcslen(str3),&font1,PointF(0-nWidth,-R-12),&solidBrush);

graphics.DrawLine(&newPen,0,-R,0,-(R-nLen));
}
graphics.DrawLine(&newPen,0,-R,0,-(R-nLen));
graphics.RotateTransform(5);
}
SolidBrush blueBrush(Color(255,0,0,255));
graphics.FillEllipse(&blueBrush,-5,-5,10,10);
graphics.ResetTransform();


graphics.ReleaseHDC(pDC->m_hDC);

ReleaseDC(pDC);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值