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

📄 0106230226dlg.cpp

📁 MFC做的时钟程序
💻 CPP
字号:
// 0106230226Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "0106230226.h"
#include "0106230226Dlg.h"
#include <math.h>
#include <wingdi.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ID_TIMER 1
#define TWOPI (2*3.14159)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy0106230226Dlg dialog

CMy0106230226Dlg::CMy0106230226Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy0106230226Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy0106230226Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy0106230226Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy0106230226Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy0106230226Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy0106230226Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_ABTATH, OnAbtath)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy0106230226Dlg message handlers
void CMy0106230226Dlg::RotatePoint(POINT pt[],int iNum,int iAngle)//设定指针末端位置
{
	int i;
	POINT ptTemp;

	for(i=0;i<iNum;i++)
	{
		ptTemp.x = (int)(pt[i].x * cos(TWOPI * iAngle /360) + pt[i].y * sin(TWOPI * iAngle/360));
		ptTemp.y = (int)(pt[i].y * cos(TWOPI * iAngle /360) - pt[i].x * sin(TWOPI * iAngle/360));
		pt[i] = ptTemp;
	}
}

BOOL CMy0106230226Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy0106230226Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CMy0106230226Dlg::SetIsotropic(HDC hdc,int cxClient,int cyClient)//设定X正方向为右,Y正方向为上
{
	::SetMapMode(hdc,MM_ISOTROPIC);
	::SetWindowExtEx(hdc,1000,1000,NULL);
	::SetViewportExtEx(hdc,cxClient/2,-cyClient/2,NULL);
	::SetViewportOrgEx(hdc,cxClient/2,cyClient/2,NULL);
}
void CMy0106230226Dlg::DrawClock(HDC hdc)//画钟面
{
	int iAngle;
	POINT pt[3];
	//CBrush brush(RGB(0,0,255));
	HBRUSH hBrush;
	hBrush = CreateSolidBrush(RGB(255,255,255));
	SelectObject(hdc,hBrush);
	//SetDCBrushColor(hdc,RGB(0,0,255));
	
	Ellipse(hdc,cxClient/2-110,cyClient/2-110,cxClient/2+110,cyClient/2+110);
	for(iAngle =0;iAngle<360;iAngle +=6)
	{
		pt[0].x = 0;
		pt[0].y = 100;

		RotatePoint(pt,1,iAngle);

		pt[2].x = pt[2].y = iAngle %5?3:10;

		pt[0].x -=pt[2].x/2;
		pt[0].y -= pt[2].y/2;

		pt[1].x = pt[0].x + pt[2].x;
		pt[1].y = pt[0].y + pt[2].y;
		//SelectObject(hdc,GetStockObject(BLACK_BRUSH));
		//Ellipse(hdc,pt[0].x+cxClient/2,pt[0].y+cyClient/2,pt[1].x+cxClient/2,pt[1].y+cyClient/2);

		SetBkMode(hdc,TRANSPARENT);
		CString s;
		int m=(int)(180-iAngle)/30;
		s.Format("%d",m>0?m:12+m);
		if(pt[2].x == 10) TextOut(hdc,pt[0].x+cxClient/2-pt[2].x/2+20*sin(TWOPI*iAngle/360),pt[0].y+cyClient/2-pt[2].y/2+20*cos(TWOPI*iAngle/360),s,s.GetLength());

	}
	DeleteObject(hBrush);
}
void CMy0106230226Dlg::DrawHands(HDC hdc,SYSTEMTIME *pst,bool fChange)//设定指针
{
	static POINT pt[3][5] = {0,-15,10,0,0,60,-10,0,0,-15,
		0,-20,5,0,0,80,-5,0,0,-20,
		0,0,0,0,0,0,0,0,0,80};
	int i,iAngle[3];
	POINT ptTemp[3][5];
	CPen hPen;		
	CDC *dc;
	dc= this->GetDC();
	
	hPen.CreatePen(PS_SOLID,5,RGB(200,100,0));	
	dc->SelectObject(&hPen);

	iAngle[0] = (pst->wHour * 30) % 360 + pst->wMinute /2;
	iAngle[1] = pst->wMinute *6;
	iAngle[2] = pst->wSecond * 6;

	memcpy(ptTemp,pt,sizeof(pt));

	for(i =fChange ? 0:2;i<3;i++)
	{
		RotatePoint(ptTemp[i],5,iAngle[i]);
		for(int j=0;j<5;j++)
		{
			ptTemp[i][j].x += cxClient/2;
			ptTemp[i][j].y *= -1;
			ptTemp[i][j].y += cyClient/2;
		}		
		if(i<2){
		dc->MoveTo(ptTemp[i][0]);
		dc->LineTo(ptTemp[i][2]);
		}
		else{
			dc->MoveTo(ptTemp[i][0]);
			dc->LineTo(ptTemp[i][4]);
		}
	}
	DeleteObject(hPen);

	GetLocalTime(&st);	
	CString s;
	s.Format("%d:%d:%d",st.wHour,st.wMinute,st.wSecond);
	SetBkMode(::GetDC(this->m_hWnd),OPAQUE);
	SetBkColor(::GetDC(this->m_hWnd),RGB(255,0,0));
	TextOut(::GetDC(this->m_hWnd),210,300,s,s.GetLength());
}
// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMy0106230226Dlg::OnPaint() //设置背景
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
				CRect rc;
		CPaintDC dc(this);
		GetClientRect(&rc);
		dc.FillSolidRect(rc,RGB(100,100,250));

		CDialog::OnPaint();
	}
		SetIsotropic(::GetDC(this->m_hWnd),cxClient,cyClient);
	DrawClock(::GetDC(this->m_hWnd));
	DrawHands(::GetDC(this->m_hWnd),&stPrevious,true);

}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMy0106230226Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

int CMy0106230226Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct) //计时
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(ID_TIMER,1000,NULL);
	GetLocalTime(&st);
	stPrevious = st;

	return 0;
}

void CMy0106230226Dlg::OnSize(UINT nType, int cx, int cy)//设定钟面位置 
{
	CDialog::OnSize(nType, cx, cy);
	cxClient = cx;
	cyClient = cy;
	// TODO: Add your message handler code here
	
}

void CMy0106230226Dlg::OnTimer(UINT nIDEvent) //刷新
{
	// TODO: Add your message handler code here and/or call default
	GetLocalTime(&st);	
	
	Invalidate(true);	

	fChange = st.wHour != stPrevious.wHour || st.wMinute != stPrevious.wMinute;

	SetIsotropic(::GetDC(this->m_hWnd),cxClient,cyClient);

	SelectObject(::GetDC(this->m_hWnd),GetStockObject(WHITE_PEN));
	DrawHands(::GetDC(this->m_hWnd),&stPrevious,fChange);

	SelectObject(::GetDC(this->m_hWnd),GetStockObject(BLACK_PEN));
	DrawHands(::GetDC(this->m_hWnd),&st,true);

	stPrevious = st;

	CDialog::OnTimer(nIDEvent);
}

void CMy0106230226Dlg::OnDestroy()//停止 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	KillTimer(ID_TIMER);
}

void CMy0106230226Dlg::OnAbtath()//关于 
{
	// TODO: Add your control notification handler code here
	
	MessageBox("作者:戚昱阳,学号:0106230226");
}




void CMy0106230226Dlg::OnButton2()//退出 
{
	// TODO: Add your control notification handler code here
	KillTimer(ID_TIMER);
	exit(0);
}

⌨️ 快捷键说明

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