⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 MFC编写的一个小时钟程序
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "My.h"

#include "MainFrm.h"
//-------------------------------------------------------------
//---------------------------------------------------------------
#define M_PI 3.141592655

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	//-------------------------------------------------------------
    //------------------------------------------------------
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
//-------------------------------------------------------------
//------------------------------------------------------
    SetTimer(100,1000,NULL);
    IsOnTop=false;

	EnableDocking(CBRS_ALIGN_ANY);

	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	//-------------------------------------------------------------
//------------------------------------------------------
    cs.hMenu=LoadMenu(NULL,MAKEINTRESOURCE(IDR_MAINFRAME));
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	
	// Do not call CFrameWnd::OnPaint() for painting messages
//-------------------------------------------------------------
//-----------------------------------------------------
	DrawClock(&dc);

}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	
	CFrameWnd::OnTimer(nIDEvent);
	if(nIDEvent==100)
	{ 
		CClientDC dc(this);
        DrawClock(&dc);
	}
}

void CMainFrame::OnDestroy() 
{
	CFrameWnd::OnDestroy();
	//--------------------------------------
	//-----------------------------------------------
	KillTimer(100);
	
	// TODO: Add your message handler code here
	
}

	//----------------------------------------------------------------
	//-----------------------------------------------
static CSize operator* (const CSize &sz,float scalar)
{

	CSize new_sz;
    new_sz.cx=int(sz.cx*scalar);
    new_sz.cy=int(sz.cy*scalar);
    return new_sz;

}

CSize PositionOnCircle(const CSize& radius,double angle)
{
	CSize result;
    result.cx=(int) (sin ( angle * 2 * M_PI ) * radius.cx);
    result.cy=(int) (-cos (angle * 2 * M_PI ) * radius.cy);
    return result;

}

const static COLORREF pieColorLookup[]=
{
 	RGB(170,251,210),
    RGB(235,252,169),
    RGB(215,245,255)
};

void CMainFrame::DrawClock(CDC *pDC)
{

	CRect clientArea;
    GetClientRect(&clientArea);
    CRect theClockFace(CPoint(5,5),clientArea.Size()-CSize(10,10));
    CPen pen;
    CBrush brush;
    int piePart;
    CPoint center,p1,p2;
    CSize radius;
    SYSTEMTIME st;
    GetLocalTime(&st);
    radius=theClockFace.Size()*0.5;
    center=theClockFace.CenterPoint();
    pDC->SetArcDirection(AD_CLOCKWISE);
    for(piePart=0;piePart<12;piePart++)
	{
		COLORREF pieColor;
        pieColor = pieColorLookup[piePart%3];

        pen.CreatePen(PS_SOLID,1,pieColor);
        brush.CreateSolidBrush(pieColor);

        pDC->SelectObject(&pen);
        pDC->SelectObject(&brush);

        p1=center+PositionOnCircle(radius,piePart/12.0);
        p2=center+PositionOnCircle(radius,(piePart+1)/12.0);
        pDC->Pie(&theClockFace,p1,p2);
        pen.DeleteObject();
        brush.DeleteObject();
 }

   pen.CreatePen(PS_SOLID,4,RGB(180,0,0));
   pDC->SelectObject(&pen);
   pDC->MoveTo(center);
   pDC->LineTo(center+PositionOnCircle(radius,st.wHour/12.0+st.wMinute/720.0)*0.4);
   pen.DeleteObject();

   pen.CreatePen(PS_SOLID,4,RGB(0,180,0));
   pDC->SelectObject(&pen);
   pDC->MoveTo(center);
   pDC->LineTo(center+PositionOnCircle(radius,st.wMinute/60.0)*0.8);
   pen.DeleteObject();

   pen.CreatePen(PS_SOLID,1,RGB(0,0,0));
   pDC->SelectObject(&pen);
   pDC->MoveTo(center);
   pDC->LineTo(center+PositionOnCircle(radius,st.wSecond/60.0)*0.95);

}

void CMainFrame::OnUpdateAlwaysOnTop(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(IsOnTop);
}

void CMainFrame::OnFileAlwaysOnTop()
{
	IsOnTop = IsOnTop ? FALSE : TRUE;
	SetWindowPos(IsOnTop ? &wndTopMost : &wndNoTopMost,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -