포스트

(MFC) 시간, 날짜 프로그램2

MFC Programming

(MFC) 시간, 날짜 프로그램2

MFC / 2

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

##

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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()에 다음과 같이 코드를 추가한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
}


이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.