MaskEdit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. #include "MaskEditT.h"
  3. //===========================================================================
  4. // Summary:
  5. // CMaskEdit is a CEdit derived class. It allows text masking to be
  6. // applied to the control to format it for special editing restrictions.
  7. //===========================================================================
  8. class CMaskEdit : public CMaskEditT<CEdit>
  9. {
  10. DECLARE_DYNAMIC(CMaskEdit)
  11. public:
  12. //-----------------------------------------------------------------------
  13. // Summary:
  14. // Constructs a CMaskEdit object
  15. //-----------------------------------------------------------------------
  16. CMaskEdit();
  17. public:
  18. //-----------------------------------------------------------------------
  19. // Summary:
  20. // This member function will set the mask for the edit control.
  21. // Parameters:
  22. // lpszMask - The format for the mask field. For example, if
  23. // you wanted to set the mask for a phone number,
  24. // and you only wanted digits to be entered, your
  25. // mask might look like this; _T("(000) 000-0000").
  26. // lpszLiteral - The literal format is entered here. Wherever you
  27. // place an underscore ('_') is where the user will
  28. // be allowed to enter data only. Using the phone
  29. // number example; _T("(___) ___-____").
  30. // lpszDefault - Text that is to be displayed when the control
  31. // is initialized. For example; _T("(800) 555-1212").
  32. // If NULL, 'lpszLiteral' is used to initialize the
  33. // edit text.
  34. // Remarks:
  35. // The values that can be set are:
  36. // Mask Character Description
  37. // --------------------- ------------------------
  38. // 0 Numeric (0-9)
  39. // 9 Numeric (0-9) or space (' ')
  40. // # Numeric (0-9) or space (' ') or ('+') or ('-')
  41. // L Alpha (a-Z)
  42. // ? Alpha (a-Z) or space (' ')
  43. // A Alpha numeric (0-9 and a-Z)
  44. // a Alpha numeric (0-9 and a-Z) or space (' ')
  45. // & All print character only
  46. // H Hex digit (0-9 and A-F)
  47. // X Hex digit (0-9 and A-F) and space (' ')
  48. // > Forces characters to upper case (A-Z)
  49. // < Forces characters to lower case (a-z)
  50. //-----------------------------------------------------------------------
  51. virtual void SetEditMask(LPCTSTR lpszMask,LPCTSTR lpszLiteral,LPCTSTR lpszDefault=NULL);
  52. protected:
  53. DECLARE_MESSAGE_MAP()
  54. };
  55. //===========================================================================
  56. // Summary:
  57. // CDateEdit is a CMaskEdit derived class. It is specifically
  58. // geared toward editing date fields.
  59. //===========================================================================
  60. class CDateEdit : public CMaskEdit
  61. {
  62. DECLARE_DYNAMIC(CDateEdit)
  63. public:
  64. //-----------------------------------------------------------------------
  65. // Summary:
  66. // Constructs a CDateEdit object
  67. //-----------------------------------------------------------------------
  68. CDateEdit();
  69. //-----------------------------------------------------------------------
  70. // 用途:
  71. // 把COleDateTime对象中的日期部分转换成文本
  72. // 参数:
  73. // dt - COleDateTime对象
  74. // 返回值:
  75. // 日期文本,例如“2005-09-06”。如果参数dt不是合法的对象,则返回缺省
  76. // 值“1899-12-30”
  77. //-----------------------------------------------------------------------
  78. static CString DateToString(COleDateTime dt);
  79. //-----------------------------------------------------------------------
  80. // 用途:
  81. // 把日期/时间文本转换成COleDateTime对象
  82. // 参数:
  83. // strText - 日期/时间文本
  84. // chPrompt - 格式化输入的提示字符
  85. // 返回值:
  86. // COleDateTime对象。如果文本格式不对,返回的COleDateTime对象不是一个
  87. // 合法的对象。
  88. //-----------------------------------------------------------------------
  89. static COleDateTime StringToOleDateTime(CString strText, TCHAR chPrompt = _T('_'));
  90. //-----------------------------------------------------------------------
  91. // 用途:
  92. // 设置控件的日期。
  93. // 参数:
  94. // dt - COleDateTime对象,只用到日期部分。构造对象时要注意,虽然没用到
  95. // 时间部分,也要赋一个合法的时间,否则得到的对象是不合法的。
  96. //-----------------------------------------------------------------------
  97. virtual void SetDateTime(COleDateTime dt);
  98. //-----------------------------------------------------------------------
  99. // 用途:
  100. // 从控件文本获取日期/时间。
  101. // 返回值:
  102. // COleDateTime对象。如果文本格式不对,返回的COleDateTime对象不是一个
  103. // 合法的对象。
  104. //-----------------------------------------------------------------------
  105. COleDateTime GetDateTime();
  106. //-----------------------------------------------------------------------
  107. // Summary:
  108. // This member function is used internally to process the character passed
  109. // in by 'nChar' whose index is specified by 'nEndPos'.
  110. // Parameters:
  111. // nChar - Contains the character code value of the key.
  112. // nEndPos - Index of character in display string.
  113. // Returns:
  114. // TRUE if successful, otherwise returns FALSE.
  115. //-----------------------------------------------------------------------
  116. virtual BOOL ProcessMask(UINT& nChar, int nEndPos);
  117. };
  118. // ----------------------------------------------------------------------------
  119. // Summary:
  120. // The DDX_OleDateTime function manages the transfer of
  121. // integer data between a date edit control, in a dialog box, form
  122. // view, or control view object, and a COleDateTime data member of
  123. // the dialog box, form view, or control view object.
  124. //
  125. // When DDX_OleDateTime is called, 'value' is set to
  126. // the current state of the date edit control.
  127. // Parameters:
  128. // pDX - A pointer to a CDataExchange object. The framework supplies
  129. // this object to establish the context of the data exchange,
  130. // including its direction.
  131. // nIDC - The resource ID of the date edit control associated with the
  132. // control property.
  133. // rDateTime - A reference to a member variable of the dialog box, form
  134. // view, or control view object with which data is exchanged.
  135. // See Also:
  136. // CDateEdit
  137. // ----------------------------------------------------------------------------
  138. void AFXAPI DDX_OleDateTime(CDataExchange* pDX, int nIDC, COleDateTime& rDateTime);
  139. //===========================================================================
  140. // Summary:
  141. // CTimeEdit is a CDateEdit derived class. It is specifically geared
  142. // toward editing time fields.
  143. //===========================================================================
  144. class CTimeEdit : public CDateEdit
  145. {
  146. DECLARE_DYNAMIC(CTimeEdit)
  147. public:
  148. //-----------------------------------------------------------------------
  149. // 用途:
  150. // 构造函数。
  151. // 参数:
  152. // bHasSecond - 表明是否包含“秒”
  153. //-----------------------------------------------------------------------
  154. CTimeEdit(BOOL bHasSecond = FALSE);
  155. public:
  156. //-----------------------------------------------------------------------
  157. // 用途:
  158. // 把COleDateTime对象中的时间部分转换成文本
  159. // 参数:
  160. // dt - COleDateTime对象
  161. // bHasSecond - 表明是否包含“秒”
  162. // 返回值:
  163. // 时间文本,例如“15:39”。如果参数dt不是合法的对象,则返回缺省值
  164. // “00:00”
  165. //-----------------------------------------------------------------------
  166. static CString TimeToString(COleDateTime dt, BOOL bHasSecond = FALSE);
  167. //-----------------------------------------------------------------------
  168. // 用途:
  169. // 设置控件的时间。
  170. // 参数:
  171. // dt - COleDateTime对象,只用到时间部分。构造对象时要注意,虽然没用到
  172. // 日期部分,也要赋一个合法的年月日,否则得到的对象是不合法的。
  173. //-----------------------------------------------------------------------
  174. virtual void SetDateTime(COleDateTime dt);
  175. //-----------------------------------------------------------------------
  176. // Summary:
  177. // This member function is used internally to process the character
  178. // passed in by 'nChar' whose index is specified by 'nEndPos'.
  179. // Parameters:
  180. // nChar - Contains the character code value of the key.
  181. // nEndPos - Index of the character in the display string.
  182. // Returns:
  183. // TRUE if successful, otherwise returns FALSE.
  184. //-----------------------------------------------------------------------
  185. virtual BOOL ProcessMask(UINT& nChar, int nEndPos);
  186. protected:
  187. BOOL m_bHasSecond; // 表明是否包含“秒”
  188. };
  189. //===========================================================================
  190. // 用途:
  191. // CDegreeEdit 派生自 CMaskEdit,以度分秒的形式输入度,秒的小数点后的位数
  192. // 在构造对象时确定。有效输入范围是-999度至999度
  193. // 作者:
  194. // 祝晓鹰
  195. //===========================================================================
  196. class CDegreeEdit: public CMaskEdit
  197. {
  198. DECLARE_DYNAMIC(CDegreeEdit)
  199. public:
  200. //-----------------------------------------------------------------------
  201. // 用途:
  202. // 构造函数
  203. // 参数:
  204. // nSecondPrecision - 指示秒的小数点后保留几位,范围0-3,缺省为两位
  205. //-----------------------------------------------------------------------
  206. CDegreeEdit(int nSecondPrecision = 2);
  207. public:
  208. //-----------------------------------------------------------------------
  209. // 用途:
  210. // 以浮点数形式设置度,控件将用度分秒的形式表示出来
  211. // 参数:
  212. // fDegree - 浮点数形式的度
  213. //-----------------------------------------------------------------------
  214. void SetDegree(double fDegree);
  215. //-----------------------------------------------------------------------
  216. // 用途:
  217. // 以浮点数的形式返回用户在控件中输入的度
  218. // 返回值:
  219. // 浮点数形式的度
  220. //-----------------------------------------------------------------------
  221. double GetDegree();
  222. //-----------------------------------------------------------------------
  223. // 用途:
  224. // 把浮点数形式的度转换成度分秒格式的文本
  225. // 参数:
  226. // fDegree - 浮点数形式的度
  227. // nSecondPrecision - 指示秒的小数点后保留几位,范围0-3
  228. // 返回值:
  229. // 度分秒格式的文本。例如当秒保留两位小数时,-10.13度转换成文本后内容
  230. // 为“- 10°07'48.00"”。注意符号位和度之间可能存在空格,如果打算用
  231. // 诸如sscanf这样的函数从转换后的文本中提取数据,需要先删除空格。
  232. //-----------------------------------------------------------------------
  233. static CString DegreeToString(double fDegree, int nSecondPrecision = 2);
  234. //-----------------------------------------------------------------------
  235. // 用途:
  236. // 把度分秒格式的文本转换成浮点数形式的度
  237. // 参数:
  238. // strText - 度分秒格式的文本
  239. // 返回值:
  240. // 浮点数形式的度
  241. //-----------------------------------------------------------------------
  242. static double StringToDegree(CString strText);
  243. protected:
  244. int m_nSecondPrecision; // 秒的小数部分的位数,范围0-3
  245. };
  246. // ----------------------------------------------------------------------------
  247. // 用途:
  248. // 函数DDX_Degree用来在一个浮点型的对话框成员变量和度编辑控件之间交换数据。
  249. // 参数:
  250. // pDX - 由系统提供的一个CDataExchange对象指针
  251. // nIDC - 控件ID
  252. // value - 以浮点数的形式返回控件中的度
  253. // 作者:
  254. // 祝晓鹰
  255. // ----------------------------------------------------------------------------
  256. void AFXAPI DDX_Degree(CDataExchange* pDX, int nIDC, double& value);