calendarbar.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面
  2. // (“Fluent UI”)。该示例仅供参考,
  3. // 用以补充《Microsoft 基础类参考》和
  4. // MFC C++ 库软件随附的相关电子文档。
  5. // 复制、使用或分发 Fluent UI 的许可条款是单独提供的。
  6. // 若要了解有关 Fluent UI 许可计划的详细信息,请访问
  7. // https://go.microsoft.com/fwlink/?LinkId=238214.
  8. //
  9. // 版权所有(C) Microsoft Corporation
  10. // 保留所有权利。
  11. #include "stdafx.h"
  12. #include "CalendarBar.h"
  13. #include "IDE.h"
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19. const int nBorderSize = 10;
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CCalendarBar
  22. CCalendarBar::CCalendarBar()
  23. {
  24. m_nMyCalendarsY = 0;
  25. }
  26. CCalendarBar::~CCalendarBar()
  27. {
  28. }
  29. BEGIN_MESSAGE_MAP(CCalendarBar, CWnd)
  30. ON_WM_CREATE()
  31. ON_WM_ERASEBKGND()
  32. ON_WM_SIZE()
  33. ON_WM_PAINT()
  34. ON_WM_SETFOCUS()
  35. ON_WM_SETTINGCHANGE()
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CCalendarBar 消息处理程序
  39. int CCalendarBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
  40. {
  41. if (CWnd::OnCreate(lpCreateStruct) == -1)
  42. return -1;
  43. CRect rectDummy(0, 0, 0, 0);
  44. m_wndCalendar.Create(WS_CHILD | WS_VISIBLE, rectDummy, this, 1);
  45. CBitmap bmp;
  46. bmp.LoadBitmap(IDB_PAGES_SMALL_HC);
  47. m_Images.Create(16, 16, ILC_COLOR24 | ILC_MASK, 0, 0);
  48. m_Images.Add(&bmp, RGB(255, 0, 255));
  49. return 0;
  50. }
  51. BOOL CCalendarBar::PreTranslateMessage(MSG *pMsg)
  52. {
  53. if (pMsg->message == WM_LBUTTONDOWN)
  54. {
  55. // 确保将键盘焦点设置到日历上
  56. m_wndCalendar.SetFocus();
  57. }
  58. return CWnd::PreTranslateMessage(pMsg);
  59. }
  60. BOOL CCalendarBar::OnEraseBkgnd(CDC* /*pDC*/)
  61. {
  62. return TRUE;
  63. }
  64. void CCalendarBar::OnSetFocus(CWnd *pOldWnd)
  65. {
  66. CWnd::OnSetFocus(pOldWnd);
  67. m_wndCalendar.SetFocus();
  68. }
  69. void CCalendarBar::OnSize(UINT nType, int cx, int cy)
  70. {
  71. CWnd::OnSize(nType, cx, cy);
  72. int nMyCalendarsHeight = 70;
  73. if (m_wndCalendar.GetSafeHwnd() != nullptr)
  74. {
  75. m_wndCalendar.SetWindowPos(nullptr, nBorderSize, nBorderSize, cx - 2 * nBorderSize, cy - 2 * nBorderSize - nMyCalendarsHeight - 10, SWP_NOZORDER | SWP_NOACTIVATE);
  76. }
  77. m_nMyCalendarsY = cy - nMyCalendarsHeight;
  78. }
  79. BOOL CCalendarBar::Create(const RECT& rect, CWnd* pParentWnd, UINT nID)
  80. {
  81. return CWnd::Create(nullptr, _T(""), WS_CHILD | WS_VISIBLE, rect, pParentWnd, nID);
  82. }
  83. void CCalendarBar::OnPaint()
  84. {
  85. CPaintDC dc(this); // 用于绘制的设备上下文
  86. CRect rectClient;
  87. GetClientRect(rectClient);
  88. dc.FillRect(rectClient, &afxGlobalData.brWindow);
  89. if (rectClient.bottom - m_nMyCalendarsY > 0)
  90. {
  91. CRect rectMyCalendarsCaption = rectClient;
  92. rectMyCalendarsCaption.top = m_nMyCalendarsY;
  93. rectMyCalendarsCaption.bottom = rectMyCalendarsCaption.top + afxGlobalData.GetTextHeight(TRUE) * 3 / 2;
  94. COLORREF clrText = CMFCVisualManager::GetInstance()->OnDrawPaneCaption(&dc, nullptr, FALSE, rectMyCalendarsCaption, CRect(0, 0, 0, 0));
  95. CPen* pOldPen = dc.SelectObject(&afxGlobalData.penBarShadow);
  96. dc.MoveTo(rectMyCalendarsCaption.left - 1, rectMyCalendarsCaption.top);
  97. dc.LineTo(rectMyCalendarsCaption.right, rectMyCalendarsCaption.top);
  98. dc.SelectStockObject(BLACK_PEN);
  99. dc.MoveTo(rectMyCalendarsCaption.left - 1, rectMyCalendarsCaption.bottom);
  100. dc.LineTo(rectMyCalendarsCaption.right, rectMyCalendarsCaption.bottom);
  101. dc.SelectObject(pOldPen);
  102. CRect rectText = rectMyCalendarsCaption;
  103. rectText.DeflateRect(10, 0);
  104. dc.SetBkMode(TRANSPARENT);
  105. dc.SetTextColor(clrText);
  106. CFont* pOldFont = dc.SelectObject(&afxGlobalData.fontRegular);
  107. BOOL bNameValid;
  108. CString str;
  109. bNameValid = str.LoadString(IDS_MYCALENDARS);
  110. ASSERT(bNameValid);
  111. dc.DrawText(str, rectText, DT_VCENTER | DT_LEFT | DT_SINGLELINE);
  112. CRect rectCalendar = rectClient;
  113. rectCalendar.top = rectMyCalendarsCaption.bottom + 5;
  114. rectCalendar.bottom = rectCalendar.top + afxGlobalData.GetTextHeight(TRUE) * 3 / 2 - 5;
  115. dc.FillSolidRect(rectCalendar, RGB(255, 255, 213));
  116. rectCalendar.DeflateRect(20, 0);
  117. m_Images.Draw(&dc, 3, rectCalendar.TopLeft(), 0);
  118. rectCalendar.left += 20;
  119. bNameValid = str.LoadString(IDS_CALENDAR);
  120. ASSERT(bNameValid);
  121. dc.SetTextColor(afxGlobalData.clrHotLinkNormalText);
  122. dc.DrawText(str, rectCalendar, DT_VCENTER | DT_LEFT | DT_SINGLELINE);
  123. dc.SelectObject(pOldFont);
  124. }
  125. }