MFC 프로그래밍을 활용한 시간/날짜 프로그램 2

  • 최초 작성일: 2022년 2월 7일 (월)

목차

[TOC]

목적

다음의 조건을 만족시키는 프로젝트를 만들어보자.

조건 1. 다이얼로그를 생성하고 오늘날짜의 년, 월, 일을 나타낸다.

조건 2. 시간을 오전과 오후로 나누어서 나타낸다.

조건 3. gct.GetDayOfWeek() 함수를 이용하여 요일을 나타낸다.

조건 4. (gct.GetHour() > 12) 일 때, gct.GetHour()-12 로 빼고, 오전/오후로 나누어서 시간을 표시한다.


결과 화면은 다음과 같다.

image


실습

프로젝트 생성

프로젝트는 전 게시물과 똑같이 생성한다.

프로젝트명은 MClock2 라고 했다.


다이얼로그 편집/디자인

image

Static Text를 11개 알맞게 배치하여 생성해준다.

임의로 각각 알맞는 Caption과 ID를 설정해주고, 날짜 데이터를 출력할 칸들은 Client Edge를 True로 설정해준다.


코드 작성

‘MClock2.Dlg.h’ 헤더 파일에 변수 선언을 위해 [Solution Explorer]-[MClock2]-[Header Files]에서 ‘MClock2Dlg.h’ 을 더블클릭하고 다음과 같이 코드를 추가한다.

image

// CMClock2Dlg dialog
class CMClock2Dlg : public CDialogEx
{
// Construction
public:
	CMClock2Dlg(CWnd* pParent = nullptr);	// standard constructor

	CRect screen;
	int vsize, hsize;

	UINT htimer;

// Dialog Data


다음으로 OnInitDialog() 함수의 변수를 초기화 시켜준다.

[Class View]-[MClock2]-[CMClock2Dlg]-[OnInitDialog()]를 더블클릭한 후 다음과 같이 코드를 추가한다.

image

BOOL CMClock2Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.
	CRect rect;
	screen.top = 0;
	screen.left = 0;
	screen.bottom = ::GetSystemMetrics(SM_CYSCREEN);
	screen.right = ::GetSystemMetrics(SM_CXSCREEN);		// 화면 크기

	htimer = SetTimer(1, 1000, NULL);

	GetWindowRect(rect);
	vsize = rect.Width();		// 프로그램의 가로 크기
	hsize = rect.Height();		// 프로그램의 세로 크기

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


이제는 날짜와 시간을 출력하기 위해 ‘WM_TIMER’ 메시지를 추가한다.

[Ctrl+Shift+X] 키 혹은 [Menu]-[Project]-[Class Wizard] 를 클릭해서 클래스 마법사를 실행시켜 다음과 같이 WM_TIMER 메시지를 더블클릭해 ‘OnTimer’ 함수를 추가하고, OnTimer()에 다음과 같이 코드를 추가한다.

void CMClock2Dlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CTime gct = CTime::GetCurrentTime();

	CString strYear;
	CString strMonth;
	CString strDay;
	CString strTime;
	CString strYoil;

	UINT DayOfWeek[] =
	{
	   LOCALE_SDAYNAME7,   // Sunday
	   LOCALE_SDAYNAME1,
	   LOCALE_SDAYNAME2,
	   LOCALE_SDAYNAME3,
	   LOCALE_SDAYNAME4,
	   LOCALE_SDAYNAME5,
	   LOCALE_SDAYNAME6   // Saturday
	};

	strYear.Format(_T("%d 년 "), gct.GetYear());
	GetDlgItem(IDC_STATIC_YEAR)->SetWindowText((LPCTSTR)strYear);

	strMonth.Format(_T("%d 월 "), gct.GetMonth());
	GetDlgItem(IDC_STATIC_MONTH)->SetWindowText((LPCTSTR)strMonth);

	strDay.Format(_T("%d 일 "), gct.GetDay());
	GetDlgItem(IDC_STATIC_DAY)->SetWindowText((LPCTSTR)strDay);

	if (gct.GetHour() > 12) 
	{
		strTime.Format(_T("오후"));
		GetDlgItem(IDC_STATIC_AMPM)->SetWindowTextW((LPCTSTR)strTime);

		strTime.Format(_T("%d 시 %d 분 %d 초 "), gct.GetHour()-12, gct.GetMinute(), gct.GetSecond());
		GetDlgItem(IDC_STATIC_TIME)->SetWindowText((LPCTSTR)strTime);
	}
	else
	{
		strTime.Format(_T("오전"));
		GetDlgItem(IDC_STATIC_AMPM)->SetWindowTextW((LPCTSTR)strTime);

		strTime.Format(_T("%d 시 %d 분 %d 초 "), gct.GetHour(), gct.GetMinute(), gct.GetSecond());
		GetDlgItem(IDC_STATIC_TIME)->SetWindowText((LPCTSTR)strTime);
	}

	TCHAR strWeekday[256];

	::GetLocaleInfo(LOCALE_USER_DEFAULT, DayOfWeek[gct.GetDayOfWeek() - 1], strWeekday, sizeof(strWeekday));

	strYoil.Format(_T("%s "), strWeekday);
	GetDlgItem(IDC_STATIC_YOIL)->SetWindowText((LPCTSTR)strYoil);

	Invalidate();

	CDialogEx::OnTimer(nIDEvent);
}