基于MFC的时钟绘制

#include <afxwin.h>
#include <math.h>

class CMyWnd : public CFrameWnd
{
public:
CMyWnd();

protected:
afx_msg void OnPaint();
afx_msg void OnSize(UINT uType, int cx, int cy);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnTimer(UINT_PTR nIdEvent);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()

private:
CDC m_memDC;
};


class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance()
{
m_pMainWnd = new CMyWnd;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
m_pMainWnd->SetTimer(10, 1000, NULL);
return TRUE;
}
};

CMyApp myApp;

BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_TIMER()
END_MESSAGE_MAP()

int CMyWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
CDC * pDC = GetDC();
this->m_memDC.CreateCompatibleDC(pDC);
ReleaseDC(pDC);
return 0;
}

BOOL CMyWnd::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}

void CMyWnd::OnSize(UINT uType, int cx, int cy)
{
CBitmap tmp;
CDC * pDC = GetDC();

tmp.CreateCompatibleBitmap(pDC, cx, cy);
m_memDC.SelectObject(&tmp);
tmp.DeleteObject();
CBrush brush(RGB(255,255,255));
m_memDC.FillRect(CRect(0, 0, cx, cy),&brush);
ReleaseDC(pDC);
}

CMyWnd::CMyWnd()
{
Create(NULL, _T ("MFC时钟"));
}

void CMyWnd::OnTimer(UINT_PTR nIdEvent)
{
Invalidate();
}


void CMyWnd::OnPaint()
{
RECT rect;
GetClientRect(&rect);

POINT mid;
mid.x = rect.right / 2;
mid.y = rect.bottom / 2;
int l, t, r, b;
double rr;
if(rect.right > rect.bottom) {
l = (rect.right - rect.bottom) / 2;
t = 0;
r = rect.bottom + l;
b = rect.bottom;
rr = b/2;
} else {
l = 0;
t = (rect.bottom - rect.right) / 2;
r = rect.right;
b = rect.right + t;
rr = r/2;
}

m_memDC.Ellipse(l, t, r, b);
m_memDC.SetPixel(mid.x, mid.y, RGB(0, 0, 0));

const double PI = 4.0 * atan( 1.0 );
const double AG = 180.0 / PI;
POINT p1, p2, p3;
int len;

#pragma warning(push)
#pragma warning(disable:4244)

for(int i = 0; i < 360; i += 6)
{
if(i % 30 == 0)
len = rr/10;
else
len = rr/20;
p1.x = (rr-len)*cos(i/AG) + mid.x;
p1.y = (rr-len)*sin(i/AG) + mid.y;
p2.x = rr*cos(i/AG) + mid.x;
p2.y = rr*sin(i/AG) + mid.y;

m_memDC.MoveTo(p1.x, p1.y);
m_memDC.LineTo(p2.x, p2.y);
}

SYSTEMTIME st;
GetLocalTime(&st);

double h, m, s;
s = st.wSecond;
m = st.wMinute + s/60;
h = st.wHour + m/60;

p1.x = 0.8*rr*cos((s*6-90)/AG) + mid.x;
p1.y = 0.8*rr*sin((s*6-90)/AG) + mid.y;
m_memDC.MoveTo(p1.x, p1.y);
m_memDC.LineTo(mid.x, mid.y);

CRgn rgn1, rgn2;
CBrush br(RGB(0, 0, 0));

p1.x = 0.8*rr*cos((m*6-90)/AG) + mid.x;
p1.y = 0.8*rr*sin((m*6-90)/AG) + mid.y;
p3.x = -0.1*rr*cos((m*6-90)/AG) + mid.x;
p3.y = -0.1*rr*sin((m*6-90)/AG) + mid.y;

p2.x = 0.1*rr*cos((m*6-75)/AG) + mid.x;
p2.y = 0.1*rr*sin((m*6-75)/AG) + mid.y;
m_memDC.BeginPath();
m_memDC.MoveTo(p3.x, p3.y);
m_memDC.LineTo(p2.x, p2.y);
m_memDC.LineTo(p1.x, p1.y);
p2.x = 0.1*rr*cos((m*6-105)/AG) + mid.x;
p2.y = 0.1*rr*sin((m*6-105)/AG) + mid.y;
m_memDC.MoveTo(p3.x, p3.y);
m_memDC.LineTo(p2.x, p2.y);
m_memDC.LineTo(p1.x, p1.y);
m_memDC.EndPath();
rgn1.CreateFromPath(&m_memDC);


p1.x = 0.7*rr*cos((h*30-90)/AG) + mid.x;
p1.y = 0.7*rr*sin((h*30-90)/AG) + mid.y;
p3.x = -0.1*rr*cos((h*30-90)/AG) + mid.x;
p3.y = -0.1*rr*sin((h*30-90)/AG) + mid.y;

p2.x = 0.1*rr*cos((h*30-60)/AG) + mid.x;
p2.y = 0.1*rr*sin((h*30-60)/AG) + mid.y;
m_memDC.BeginPath();
m_memDC.MoveTo(p3.x, p3.y);
m_memDC.LineTo(p2.x, p2.y);
m_memDC.LineTo(p1.x, p1.y);
p2.x = 0.1*rr*cos((h*30-120)/AG) + mid.x;
p2.y = 0.1*rr*sin((h*30-120)/AG) + mid.y;
m_memDC.MoveTo(p3.x, p3.y);
m_memDC.LineTo(p2.x, p2.y);
m_memDC.LineTo(p1.x, p1.y);
m_memDC.EndPath();
rgn2.CreateFromPath(&m_memDC);
rgn2.CombineRgn(&rgn1, &rgn2, RGN_OR);
m_memDC.FillRgn(&rgn2, &br);

#pragma warning(pop)
CPaintDC(this).BitBlt(rect.left, rect.top, rect.right, rect.bottom, &m_memDC, 0, 0, SRCCOPY); 

}


来源:http://wenwen.soso.com/z/q166042317.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值