TestDlg.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // TestDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "Test.h"
  5. #include "TestDlg.h"
  6. #include "CalendarEx.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. #if 1
  11. typedef INT (*GetLunarDate)(OUT LPTSTR ,IN const int &, IN const int &, IN const int &);
  12. typedef BOOL (*LuanrToSolarDate)(OUT LPTSTR ,IN const int &, IN const int &, IN const int &, BOOL);
  13. #else
  14. typedef INT (__stdcall *GetLunarDate)(OUT LPTSTR ,IN const int &, IN const int &, IN const int &);
  15. typedef BOOL (__stdcall *LuanrToSolarDate)(OUT LPTSTR ,IN const int &, IN const int &, IN const int &, BOOL);
  16. #endif
  17. GetLunarDate g_GetLunarDate;
  18. LuanrToSolarDate g_LuanrToSolarDate;
  19. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  20. class CAboutDlg : public CDialog
  21. {
  22. public:
  23. CAboutDlg();
  24. // 对话框数据
  25. enum { IDD = IDD_ABOUTBOX };
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  28. // 实现
  29. protected:
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. }
  35. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  36. {
  37. CDialog::DoDataExchange(pDX);
  38. }
  39. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  40. END_MESSAGE_MAP()
  41. // CTestDlg 对话框
  42. CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
  43. : CDialog(CTestDlg::IDD, pParent)
  44. {
  45. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  46. }
  47. void CTestDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49. CDialog::DoDataExchange(pDX);
  50. DDX_Control(pDX, IDC_DATETIMEPICKER1, m_ctlDT);
  51. }
  52. BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
  53. ON_WM_SYSCOMMAND()
  54. ON_WM_PAINT()
  55. ON_WM_QUERYDRAGICON()
  56. //}}AFX_MSG_MAP
  57. ON_BN_CLICKED(IDC_BUTTON1, &CTestDlg::OnBnClickedButton1)
  58. ON_BN_CLICKED(IDC_BUTTON2, &CTestDlg::OnBnClickedButton2)
  59. ON_BN_CLICKED(IDC_CHECK1, &CTestDlg::OnBnClickedCheck1)
  60. ON_BN_CLICKED(BT_GETAGE, &CTestDlg::OnBnClickedGetage)
  61. END_MESSAGE_MAP()
  62. // CTestDlg 消息处理程序
  63. BOOL CTestDlg::OnInitDialog()
  64. {
  65. CDialog::OnInitDialog();
  66. // 将“关于...”菜单项添加到系统菜单中。
  67. // IDM_ABOUTBOX 必须在系统命令范围内。
  68. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  69. ASSERT(IDM_ABOUTBOX < 0xF000);
  70. CMenu* pSysMenu = GetSystemMenu(FALSE);
  71. if (pSysMenu != NULL)
  72. {
  73. BOOL bNameValid;
  74. CString strAboutMenu;
  75. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  76. ASSERT(bNameValid);
  77. if (!strAboutMenu.IsEmpty())
  78. {
  79. pSysMenu->AppendMenu(MF_SEPARATOR);
  80. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  81. }
  82. }
  83. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  84. // 执行此操作
  85. SetIcon(m_hIcon, TRUE); // 设置大图标
  86. SetIcon(m_hIcon, FALSE); // 设置小图标
  87. SetDlgItemText(ET_LUNAR_BRITHDAY,_T("2014-11-22"));
  88. // TODO: 在此添加额外的初始化代码
  89. LoadMyDllLibrary();
  90. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  91. }
  92. void CTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  93. {
  94. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  95. {
  96. CAboutDlg dlgAbout;
  97. dlgAbout.DoModal();
  98. }
  99. else
  100. {
  101. CDialog::OnSysCommand(nID, lParam);
  102. }
  103. }
  104. // 如果向对话框添加最小化按钮,则需要下面的代码
  105. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  106. // 这将由框架自动完成。
  107. void CTestDlg::OnPaint()
  108. {
  109. if (IsIconic())
  110. {
  111. CPaintDC dc(this); // 用于绘制的设备上下文
  112. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  113. // 使图标在工作区矩形中居中
  114. int cxIcon = GetSystemMetrics(SM_CXICON);
  115. int cyIcon = GetSystemMetrics(SM_CYICON);
  116. CRect rect;
  117. GetClientRect(&rect);
  118. int x = (rect.Width() - cxIcon + 1) / 2;
  119. int y = (rect.Height() - cyIcon + 1) / 2;
  120. // 绘制图标
  121. dc.DrawIcon(x, y, m_hIcon);
  122. }
  123. else
  124. {
  125. CDialog::OnPaint();
  126. }
  127. }
  128. //当用户拖动最小化窗口时系统调用此函数取得光标
  129. //显示。
  130. HCURSOR CTestDlg::OnQueryDragIcon()
  131. {
  132. return static_cast<HCURSOR>(m_hIcon);
  133. }
  134. void CTestDlg::OnBnClickedButton1() // 公历转农历;
  135. {
  136. // TODO: 在此添加控件通知处理程序代码
  137. COleDateTime oleDT;
  138. m_ctlDT.GetTime(oleDT);
  139. int nYear = oleDT.GetYear();
  140. int nMonth = oleDT.GetMonth();
  141. int nDay = oleDT.GetDay();
  142. CString strLunarDate;
  143. TCHAR szLunarDate[11] = {0};
  144. //if(g_GetLunarDate(szLunarDate, nYear,nMonth,nDay) != 0)
  145. if ( !CalendarEx::SolarToLuanrDate(nYear, nMonth, nDay, szLunarDate))
  146. {
  147. AfxMessageBox(_T("出错"));
  148. return;
  149. }
  150. SetDlgItemText(Edit_Lunar,szLunarDate);
  151. }
  152. void CTestDlg::OnBnClickedButton2() // 农历转公历;
  153. {
  154. // TODO: 在此添加控件通知处理程序代码
  155. CString strLunarDate;
  156. GetDlgItemText(Edit_LunarDate,strLunarDate);
  157. int nYear = _ttoi(strLunarDate.Left(4));
  158. int nMonth = _ttoi(strLunarDate.Mid(5, 2));
  159. int nDay = _ttoi(strLunarDate.Mid(8, 2));
  160. TCHAR szSolarDate[11] = {0};
  161. //if(g_LuanrToSolarDate(szSolarDate,nYear,nMonth,nDay,m_IsLeapMonth) == FALSE)
  162. if ( !CalendarEx::LuanrToSolarDate(nYear, nMonth, nDay, szSolarDate, m_IsLeapMonth))
  163. {
  164. AfxMessageBox(_T("出错"));
  165. return;
  166. }
  167. SetDlgItemText(Edit_Solar,szSolarDate);
  168. }
  169. void CTestDlg::LoadMyDllLibrary()
  170. {
  171. m_MyDllModule = LoadLibrary(_T("LunarCalendar.dll"));
  172. DWORD dwEr = 0;
  173. if (m_MyDllModule == NULL)
  174. {
  175. dwEr = GetLastError();
  176. CString strShow;
  177. strShow.Format(_T(".初始化动态加失败(%d),请检查文件是否存在或损坏..."), dwEr);
  178. return;
  179. }
  180. // 2.加载全部需要的方法;
  181. g_GetLunarDate = (GetLunarDate)::GetProcAddress(m_MyDllModule, "GetLunarDate");
  182. g_LuanrToSolarDate = (LuanrToSolarDate)::GetProcAddress(m_MyDllModule, "LuanrToSolarDate");
  183. }
  184. void CTestDlg::FreeMyDllLibrary()
  185. {
  186. if (m_MyDllModule != NULL)
  187. {
  188. FreeLibrary(m_MyDllModule);
  189. }
  190. }
  191. void CTestDlg::OnBnClickedCheck1()
  192. {
  193. // TODO: 在此添加控件通知处理程序代码
  194. if ( ((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck())
  195. {
  196. m_IsLeapMonth = TRUE;
  197. }
  198. else
  199. m_IsLeapMonth = FALSE;
  200. }
  201. void CTestDlg::OnBnClickedGetage()
  202. {
  203. // TODO: 在此添加控件通知处理程序代码
  204. CString strLunarBrithday;
  205. GetDlgItemText(ET_LUNAR_BRITHDAY, strLunarBrithday);
  206. INT nAge = CalendarEx::GetAge(strLunarBrithday.GetString(), m_IsLeapMonth);
  207. if ( nAge != -1 )
  208. {
  209. SetDlgItemInt(ET_AGE, nAge);
  210. }
  211. }