MaskEditT.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. #pragma once
  2. #include <afxpriv.h> // 由于用到了一个未公开的宏ON_MESSAGE_VOID
  3. // 因此需要包含这个MFC私有的头文件
  4. //////////////////////////////////////////////////////////////////////
  5. // Summary:
  6. // CMaskEditT is a template class. It allows text masking to be
  7. // applied to the control to format it for special editing restrictions.
  8. //////////////////////////////////////////////////////////////////////
  9. template <typename TBase>
  10. class CMaskEditT : public TBase
  11. {
  12. public:
  13. //-----------------------------------------------------------------------
  14. // Summary:
  15. // Constructs a CMaskEditT object
  16. //-----------------------------------------------------------------------
  17. CMaskEditT()
  18. : m_bOverType( TRUE )
  19. , m_bUseMask( TRUE )
  20. , m_bModified( FALSE )
  21. , m_strWindowText( _T( "" ) )
  22. , m_strMask( _T( "" ) )
  23. , m_strLiteral( _T( "" ) )
  24. , m_strDefault( _T( "" ) )
  25. , m_chPrompt( _T('_') )
  26. {
  27. }
  28. public:
  29. //-----------------------------------------------------------------------
  30. // Parameters:
  31. // bUseMask - TRUE to enable the mask. FALSE to disable the mask.
  32. // Summary:
  33. // Call this member function to enable or disable the mask for the mask
  34. // edit control.
  35. //-----------------------------------------------------------------------
  36. void SetUseMask(BOOL bUseMask) {
  37. m_bUseMask = bUseMask;
  38. }
  39. //-----------------------------------------------------------------------
  40. // Summary:
  41. // This member function is called to determine if the mask for the edit
  42. // control can be used.
  43. // Returns:
  44. // TRUE if the mask can be used, otherwise returns FALSE.
  45. //-----------------------------------------------------------------------
  46. BOOL CanUseMask() {
  47. return m_bUseMask && ((GetStyle() & ES_READONLY) == 0);
  48. }
  49. //-----------------------------------------------------------------------
  50. // Summary:
  51. // This member function is called to enable or disable type over, also
  52. // known as insert mode.
  53. // Parameters:
  54. // bOverType - TRUE to enable type over.
  55. //-----------------------------------------------------------------------
  56. void SetOverType(BOOL bOverType) {
  57. m_bOverType = bOverType;
  58. }
  59. //-----------------------------------------------------------------------
  60. // Summary:
  61. // This member function is called to determine if type over has been enabled.
  62. // Returns:
  63. // TRUE if type over is enabled, otherwise returns FALSE.
  64. //-----------------------------------------------------------------------
  65. BOOL CanOverType() {
  66. return m_bOverType;
  67. }
  68. //-----------------------------------------------------------------------
  69. // Summary:
  70. // This member function is called to determine if the index specified
  71. // by 'iPos' is a valid index for the currently displayed edit text.
  72. // Parameters:
  73. // iPos - Index of the character to check.
  74. // Returns:
  75. // TRUE if the index is valid, otherwise returns FALSE.
  76. //-----------------------------------------------------------------------
  77. BOOL PosInRange(int iPos) {
  78. return ( ( iPos >= 0 ) && ( iPos < m_strLiteral.GetLength( ) ) );
  79. }
  80. //-----------------------------------------------------------------------
  81. // Summary:
  82. // This member function retrieves the character that is currently used as
  83. // the mask prompt. The mask prompt indicates that the field is editable.
  84. // Returns:
  85. // A TCHAR data type.
  86. //-----------------------------------------------------------------------
  87. TCHAR GetPromptChar() {
  88. return m_chPrompt;
  89. }
  90. //-----------------------------------------------------------------------
  91. // Summary:
  92. // This member function is called to set the prompt character that is
  93. // displayed to the user that indicates the field can be edited.
  94. // Parameters:
  95. // ch - A TCHAR data type.
  96. //-----------------------------------------------------------------------
  97. void SetPromptChar(TCHAR ch)
  98. {
  99. if (m_chPrompt == ch)
  100. return;
  101. for (int i = 0; i < m_strLiteral.GetLength(); i++)
  102. if (m_strLiteral[i] == m_chPrompt) m_strLiteral.SetAt(i, ch);
  103. for (int j = 0; j < m_strWindowText.GetLength(); j++)
  104. if (m_strWindowText[j] == m_chPrompt) m_strWindowText.SetAt(j, ch);
  105. // 原来的代码没有修改m_strDefault,导致它仍然使用旧的提示字符。
  106. // 如果用户在改变提示字符后使用回退键删除字符,被删除位置出现
  107. // 的是旧的提示字符。因此对m_strDefault也要进行修改。
  108. for (int k = 0; k < m_strDefault.GetLength(); k++)
  109. if (m_strDefault[k] == m_chPrompt) m_strDefault.SetAt(k, ch);
  110. m_chPrompt = ch;
  111. SetWindowText( m_strWindowText );
  112. }
  113. //-----------------------------------------------------------------------
  114. // Summary:
  115. // This member function is called to perform a cut operation using the
  116. // currently selected text.
  117. //-----------------------------------------------------------------------
  118. void MaskCut()
  119. {
  120. if ( !CanUseMask( ))
  121. {
  122. DefWindowProc( WM_CUT, 0, 0);
  123. }
  124. else
  125. {
  126. MaskCopy( );
  127. MaskClear( );
  128. }
  129. }
  130. //-----------------------------------------------------------------------
  131. // Summary:
  132. // This member function is called to perform a copy operation using the
  133. // currently selected text.
  134. //-----------------------------------------------------------------------
  135. void MaskCopy()
  136. {
  137. if ( !CanUseMask( ))
  138. {
  139. DefWindowProc( WM_COPY, 0, 0);
  140. }
  141. else
  142. {
  143. int nStartChar, nEndChar;
  144. GetSel( nStartChar, nEndChar );
  145. if (nStartChar == nEndChar)
  146. return;
  147. CString strBuffer;
  148. int i;
  149. for ( i = nStartChar; i < nEndChar; ++i )
  150. {
  151. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  152. {
  153. if ( m_strWindowText.GetAt( i ) == m_chPrompt )
  154. continue;
  155. strBuffer += m_strWindowText.GetAt( i );
  156. }
  157. }
  158. CopyToClipboard( strBuffer );
  159. }
  160. }
  161. //-----------------------------------------------------------------------
  162. // Summary:
  163. // This member function is called to perform a paste operation using the
  164. // current clipboard text.
  165. //-----------------------------------------------------------------------
  166. void MaskPaste()
  167. {
  168. if ( !CanUseMask( ))
  169. {
  170. DefWindowProc(WM_PASTE, 0, 0);
  171. }
  172. else
  173. {
  174. if ( !OpenClipboard( ) )
  175. return;
  176. // 原来的代码在处理UNICODE时是错误的,改进如下
  177. #ifdef _UNICODE
  178. HGLOBAL hglbPaste = ::GetClipboardData( CF_UNICODETEXT );
  179. #else
  180. HGLOBAL hglbPaste = ::GetClipboardData( CF_TEXT );
  181. #endif
  182. if (hglbPaste == NULL)
  183. {
  184. ::CloseClipboard();
  185. return;
  186. }
  187. CString strPaste = (TCHAR*)::GlobalLock(hglbPaste);
  188. ::GlobalUnlock( hglbPaste );
  189. ::CloseClipboard();
  190. MaskClear( );
  191. if (strPaste.IsEmpty())
  192. return;
  193. // 原来的代码有两个Bug:
  194. // 1 如果碰到插入位置是固定字符,会导致文本顺序出现混乱。
  195. // 2 完成粘贴操作后,插入光标没有设置到新文本的后面。
  196. // 改进如下
  197. int nStartChar, nEndChar;
  198. GetSel(nStartChar, nEndChar);
  199. for (int i = 0; i < strPaste.GetLength(); i++)
  200. {
  201. if ( !CorrectPosition(nStartChar, TRUE, FALSE) )
  202. break;
  203. UINT ch = strPaste[i];
  204. if (CheckChar(nStartChar, ch, FALSE))
  205. {
  206. InsertCharAt( nStartChar, ( TCHAR )ch );
  207. nStartChar++;
  208. }
  209. }
  210. m_bModified = TRUE;
  211. SetWindowText( m_strWindowText );
  212. if ( nStartChar < m_strLiteral.GetLength() )
  213. CorrectPosition( nStartChar, TRUE, FALSE );
  214. nEndChar = nStartChar;
  215. SetSel( nStartChar, nEndChar );
  216. }
  217. }
  218. //-----------------------------------------------------------------------
  219. // Summary:
  220. // This member function is called to clear the current text selection.
  221. //-----------------------------------------------------------------------
  222. void MaskClear()
  223. {
  224. if ( !CanUseMask( ))
  225. {
  226. DefWindowProc( WM_CLEAR, 0, 0);
  227. }
  228. else
  229. {
  230. int nStartChar, nEndChar;
  231. GetSel(nStartChar, nEndChar);
  232. if ( nStartChar == nEndChar )
  233. return;
  234. // check to see if there is anything left to delete
  235. int iLength = m_strWindowText.GetLength( );
  236. int i;
  237. for ( i = nStartChar; i < iLength; ++i )
  238. {
  239. TCHAR ch1 = m_strLiteral.GetAt( i );
  240. TCHAR ch2 = m_strWindowText.GetAt( i );
  241. if ( ( ch1 == m_chPrompt ) && ( ch2 != m_chPrompt ) )
  242. {
  243. break;
  244. }
  245. }
  246. if ( i == iLength )
  247. {
  248. // 没有可删除的内容,都是提示字符或固定字符
  249. nEndChar = nStartChar;
  250. SetSel( nStartChar, nEndChar );
  251. return;
  252. }
  253. // 原来的代码没有考虑到被选中文本以固定字符开头的情况,此时调用
  254. // DeleteCharAt没有效果,因此要先找出被选中文本第一个可编辑字符
  255. // 的位置,然后在该位置上调用DeleteCharAt
  256. int iPos = nStartChar;
  257. if (!CorrectPosition(iPos))
  258. return;
  259. if (iPos >= nEndChar)
  260. {
  261. // 被选中的文本都是固定字符
  262. nEndChar = nStartChar;
  263. SetSel( nStartChar, nEndChar );
  264. return;
  265. }
  266. nStartChar = iPos;
  267. for ( i = nStartChar; i < nEndChar; ++i )
  268. {
  269. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  270. {
  271. DeleteCharAt( nStartChar );
  272. }
  273. }
  274. m_bModified = TRUE;
  275. SetWindowText( m_strWindowText );
  276. nEndChar = nStartChar;
  277. SetSel( nStartChar, nEndChar );
  278. }
  279. }
  280. //-----------------------------------------------------------------------
  281. // Summary:
  282. // This member function is called to undo the previous action.
  283. //-----------------------------------------------------------------------
  284. void MaskUndo()
  285. {
  286. if ( !CanUseMask( ))
  287. {
  288. DefWindowProc( EM_UNDO, 0, 0);
  289. }
  290. else
  291. {
  292. // 如果采用格式化输入,不支持Undo功能
  293. :: MessageBeep( ( UINT )-1 );
  294. }
  295. }
  296. //-----------------------------------------------------------------------
  297. // 用途:
  298. // 控件消息EM_CANUNDO的处理函数
  299. //-----------------------------------------------------------------------
  300. afx_msg LRESULT MaskCanUndo(WPARAM wParam, LPARAM lParam)
  301. {
  302. if ( !CanUseMask( ))
  303. {
  304. return DefWindowProc( EM_CANUNDO, wParam, wParam);
  305. }
  306. else
  307. {
  308. return FALSE; // 如果采用格式化输入,不能Undo
  309. }
  310. }
  311. //-----------------------------------------------------------------------
  312. // Summary:
  313. // This member function is called to select all text in the mask edit
  314. // control.
  315. //-----------------------------------------------------------------------
  316. void MaskSelectAll()
  317. {
  318. if ( !CanUseMask( ))
  319. {
  320. SetSel(0, -1);
  321. }
  322. else
  323. {
  324. // 原来的代码在选择全部文本时,跳过起始的固定字符。
  325. // 我觉得还是用缺省的功能更好一些。
  326. SetSel(0, -1);
  327. }
  328. }
  329. //-----------------------------------------------------------------------}
  330. // Summary:
  331. // This member function is called to determine if the text has been modified.
  332. // Returns:
  333. // TRUE if the text has changed, otherwise returns FALSE.
  334. //-----------------------------------------------------------------------
  335. BOOL IsModified() {
  336. return m_bModified;
  337. }
  338. //-----------------------------------------------------------------------
  339. // Summary:
  340. // This method called to set masked text for the edit control.
  341. // Parameters:
  342. // lpszMaskedText - Text string without mask.
  343. // Remarks:
  344. // If a mask is used, then the mask will be applied to the text
  345. // in lpszMaskedText.
  346. //-----------------------------------------------------------------------
  347. void SetMaskedText(LPCTSTR lpszMaskedText)
  348. {
  349. CString strMaskedText = lpszMaskedText;
  350. int nLen = strMaskedText.GetLength();
  351. int x = 0;
  352. m_strWindowText = m_strLiteral;
  353. for (int i = 0; (i < m_strWindowText.GetLength()) && (x < nLen); i++)
  354. {
  355. if (strMaskedText[x] == m_strWindowText[i])
  356. {
  357. x ++;
  358. } else if (m_strWindowText[i] == m_chPrompt)
  359. {
  360. m_strWindowText.SetAt(i, strMaskedText[x]);
  361. x ++;
  362. }
  363. }
  364. // set the window text for the control.
  365. m_bModified = FALSE;
  366. SetWindowText( m_strWindowText );
  367. }
  368. //-----------------------------------------------------------------------
  369. // Summary:
  370. // This member function will set the mask for the edit control.
  371. // Parameters:
  372. // lpszMask - The format for the mask field. For example, if you wanted to set
  373. // the mask for a phone number, and you only wanted digits to be entered,
  374. // your mask might look like this; _T("(000) 000-0000").
  375. // lpszLiteral - The literal format is entered here. Wherever you place an underscore
  376. // ('_') is where the user will be allowed to enter data only. Using
  377. // the phone number example; _T("(___) ___-____").
  378. // lpszDefault - Text that is to be displayed when the control is initialized. For
  379. // example; _T("(800) 555-1212"). If NULL, 'lpszLiteral' is used to initialize
  380. // the edit text.
  381. // Remarks:
  382. // The values that can be set are:
  383. // Mask Character Description
  384. // --------------------- ------------------------
  385. // 0 Numeric (0-9)
  386. // 9 Numeric (0-9) or space (' ')
  387. // # Numeric (0-9) or space (' ') or ('+') or ('-')
  388. // L Alpha (a-Z)
  389. // ? Alpha (a-Z) or space (' ')
  390. // A Alpha numeric (0-9 and a-Z)
  391. // a Alpha numeric (0-9 and a-Z) or space (' ')
  392. // & All print character only
  393. // H Hex digit (0-9 and A-F)
  394. // X Hex digit (0-9 and A-F) and space (' ')
  395. // > Forces characters to upper case (A-Z)
  396. // < Forces characters to lower case (a-z)
  397. //-----------------------------------------------------------------------
  398. virtual void SetEditMask(LPCTSTR lpszMask,LPCTSTR lpszLiteral,LPCTSTR lpszDefault=NULL)
  399. {
  400. ASSERT(lpszMask);
  401. ASSERT(lpszLiteral);
  402. // initialize the mask for the control.
  403. m_strMask = lpszMask;
  404. m_strLiteral = lpszLiteral;
  405. ASSERT(m_strMask.GetLength() == m_strLiteral.GetLength());
  406. if ( lpszDefault == NULL )
  407. {
  408. m_strWindowText = lpszLiteral;
  409. m_strDefault = lpszLiteral;
  410. }
  411. else
  412. {
  413. SetMaskedText(lpszDefault);
  414. m_strDefault = m_strWindowText;
  415. }
  416. ASSERT(m_strWindowText.GetLength() == m_strLiteral.GetLength());
  417. // set the window text for the control.
  418. m_bModified = FALSE;
  419. SetWindowText( m_strWindowText );
  420. }
  421. //-----------------------------------------------------------------------
  422. // Summary:
  423. // Converts character to Upper/Lower case.
  424. // Parameters:
  425. // nChar - Char to be converted
  426. // bUpperCase - TRUE to convert to upper case
  427. // Returns:
  428. // Converted character.
  429. //-----------------------------------------------------------------------
  430. TCHAR ConvertUnicodeAlpha(TCHAR nChar, BOOL bUpperCase)
  431. {
  432. CString strTemp(nChar);
  433. if (bUpperCase) strTemp.MakeUpper(); else strTemp.MakeLower();
  434. return strTemp[0];
  435. }
  436. //-----------------------------------------------------------------------
  437. // Summary:
  438. // This method determines if nChar is alpha character
  439. // Parameters:
  440. // nChar - Character need to test
  441. // Returns:
  442. // TRUE if nChar is alpha character.
  443. //-----------------------------------------------------------------------
  444. BOOL IsUnicodeAlpha(TCHAR nChar)
  445. {
  446. if (ConvertUnicodeAlpha(nChar, TRUE) != nChar)
  447. return TRUE;
  448. if (ConvertUnicodeAlpha(nChar, FALSE) != nChar)
  449. return TRUE;
  450. return FALSE;
  451. }
  452. //-----------------------------------------------------------------------
  453. // Summary:
  454. // This member function is used internally to validate the character indicated
  455. // by 'nChar'.
  456. // Parameters:
  457. // iPos - 字符位置
  458. // nChar - Contains the character code value of the key.
  459. // bBeep - TRUE to enable beep. FALSE to disable beep.
  460. // Returns:
  461. // TRUE if successful, otherwise returns FALSE.
  462. //-----------------------------------------------------------------------
  463. virtual BOOL CheckChar(int iPos, UINT& nChar, BOOL bBeep=TRUE)
  464. {
  465. // do not use mask
  466. if ( !CanUseMask( ) )
  467. {
  468. return FALSE;
  469. }
  470. // control character, OK
  471. if ( !_istprint( ( TCHAR )nChar ) && !IsUnicodeAlpha(( TCHAR )nChar))
  472. {
  473. // 原来的程序在这里返回TRUE,根据对上下文的理解,
  474. // 应该返回FALSE,并且发出提示音
  475. if ( bBeep )
  476. ::MessageBeep( (UINT)-1 );
  477. return FALSE;
  478. }
  479. // advance to the first character input position.
  480. int i = 0;
  481. for ( i = iPos; i < m_strLiteral.GetLength( ); ++i )
  482. {
  483. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  484. break;
  485. }
  486. // make sure the string is not longer than the mask
  487. if ( i >= m_strMask.GetLength( ) )
  488. {
  489. if ( bBeep )
  490. {
  491. :: MessageBeep( ( UINT )-1 );
  492. }
  493. return FALSE;
  494. }
  495. if ( !ProcessMask( nChar, i ) )
  496. {
  497. if ( bBeep )
  498. {
  499. :: MessageBeep( ( UINT )-1 );
  500. }
  501. return FALSE;
  502. }
  503. return TRUE;
  504. }
  505. //-----------------------------------------------------------------------
  506. // Summary:
  507. // This member function is used internally to process the character passed
  508. // in by 'nChar' whose index is specified by 'nEndPos'.
  509. // Parameters:
  510. // nChar - Contains the character code value of the key.
  511. // nEndPos - Index of character in display string.
  512. // Returns:
  513. // TRUE if successful, otherwise returns FALSE.
  514. //-----------------------------------------------------------------------
  515. virtual BOOL ProcessMask(UINT& nChar,int nEndPos)
  516. {
  517. // check the key against the mask
  518. switch ( m_strMask.GetAt( nEndPos ) )
  519. {
  520. case '0': // digit only //completely changed this
  521. {
  522. if ( _istdigit( ( TCHAR )nChar ) ) {
  523. return TRUE;
  524. }
  525. break;
  526. }
  527. case '9': // digit or space
  528. {
  529. if ( _istdigit( ( TCHAR )nChar ) ) {
  530. return TRUE;
  531. }
  532. if ( nChar == ' ' ) {
  533. return TRUE;
  534. }
  535. break;
  536. }
  537. case '#': // digit or space or '+' or '-'
  538. {
  539. if ( _istdigit( ( TCHAR )nChar ) ) {
  540. return TRUE;
  541. }
  542. if ( nChar == ' ' || nChar == '-' || nChar == '+' ) {
  543. return TRUE;
  544. }
  545. break;
  546. }
  547. case 'd': // decimal
  548. {
  549. if ( _istdigit( ( TCHAR )nChar ) ) {
  550. return TRUE;
  551. }
  552. if ( nChar == ' ' || nChar == '-' || nChar == '+' || nChar == '.' || nChar == ',') {
  553. return TRUE;
  554. }
  555. break;
  556. }
  557. case 'L': // alpha only
  558. {
  559. if ( _istalpha( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar) ) {
  560. return TRUE;
  561. }
  562. break;
  563. }
  564. case '?': // alpha or space
  565. {
  566. if ( _istalpha( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar) ) {
  567. return TRUE;
  568. }
  569. if ( nChar == ' ' ) {
  570. return TRUE;
  571. }
  572. break;
  573. }
  574. case 'A': // alpha numeric only
  575. {
  576. if ( _istalnum( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar)) {
  577. return TRUE;
  578. }
  579. break;
  580. }
  581. case 'a': // alpha numeric or space
  582. {
  583. if ( _istalnum( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar)) {
  584. return TRUE;
  585. }
  586. if ( nChar == ' ' ) {
  587. return TRUE;
  588. }
  589. break;
  590. }
  591. case '&': // all print character only
  592. {
  593. if ( _istprint( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar) ) {
  594. return TRUE;
  595. }
  596. break;
  597. }
  598. case 'H': // hex digit
  599. {
  600. if ( _istxdigit( ( TCHAR )nChar ) ) {
  601. return TRUE;
  602. }
  603. break;
  604. }
  605. case 'X': // hex digit or space
  606. {
  607. if ( _istxdigit( ( TCHAR )nChar ) ) {
  608. return TRUE;
  609. }
  610. if ( nChar == ' ' ) {
  611. return TRUE;
  612. }
  613. break;
  614. }
  615. case '>':
  616. {
  617. if ( _istalpha( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar))
  618. {
  619. nChar = ConvertUnicodeAlpha( ( TCHAR )nChar, TRUE);
  620. return TRUE;
  621. }
  622. break;
  623. }
  624. case '<':
  625. {
  626. if ( _istalpha( ( TCHAR )nChar ) || IsUnicodeAlpha(( TCHAR )nChar))
  627. {
  628. nChar = ConvertUnicodeAlpha( ( TCHAR )nChar, FALSE);
  629. return TRUE;
  630. }
  631. break;
  632. }
  633. }
  634. return FALSE;
  635. }
  636. public:
  637. //-----------------------------------------------------------------------
  638. // Summary:
  639. // Used by class CWinApp to translate window messages before they are dispatched to theTranslateMessage andDispatchMessage Windows functions.
  640. // Parameters:
  641. // pMsg - Points to a MSG structure that contains the message to process.
  642. // Returns:
  643. // Nonzero if the message was translated and should not be dispatched; 0 if the message was not translated and should be dispatched.
  644. //-----------------------------------------------------------------------
  645. virtual BOOL PreTranslateMessage(MSG* pMsg)
  646. {
  647. if (!CanUseMask())
  648. {
  649. return TBase::PreTranslateMessage(pMsg);
  650. }
  651. // intercept Ctrl+C (copy), Ctrl+V (paste), Ctrl+X (cut) and Ctrl+Z (undo)
  652. // before base class gets a hold of them.
  653. if ( pMsg->message == WM_KEYDOWN )
  654. {
  655. // 原来的代码在这里处理按键为 + 和 - 的情况,
  656. // 我觉得还是由WM_CHAR消息来处理更合适一些
  657. if ( ::GetKeyState( VK_CONTROL ) < 0 )
  658. {
  659. switch ( pMsg->wParam )
  660. {
  661. case 'X':
  662. {
  663. MaskCut( );
  664. return TRUE;
  665. }
  666. case 'C':
  667. {
  668. MaskCopy( );
  669. return TRUE;
  670. }
  671. case 'V':
  672. {
  673. MaskPaste( );
  674. return TRUE;
  675. }
  676. case 'Z':
  677. {
  678. MaskUndo( );
  679. return TRUE;
  680. }
  681. }
  682. }
  683. }
  684. return TBase::PreTranslateMessage(pMsg);
  685. }
  686. //-----------------------------------------------------------------------
  687. // 用途:
  688. // 由指定位置开始搜索并返回第一个可编辑的位置。
  689. // 参数:
  690. // iPos - 指定起始位置,如果搜索成功,返回搜索到的位置。
  691. // bForward - 标识搜索方向是往前(TRUE)还是往回(FALSE)。
  692. // bBeep - 标识在调用失败时是否发出提示音。
  693. // 返回值:
  694. // 搜索成功返回TRUE,否则返回FALSE。
  695. //-----------------------------------------------------------------------
  696. virtual BOOL CorrectPosition(int& iPos, BOOL bForward=TRUE, BOOL bBeep=TRUE) // used internally
  697. {
  698. int iLen = m_strWindowText.GetLength( );
  699. if ( iPos == iLen )
  700. {
  701. if ( bBeep )
  702. {
  703. ::MessageBeep( ( UINT )-1 );
  704. }
  705. return FALSE;
  706. }
  707. if (! PosInRange(iPos))
  708. return FALSE;
  709. if ( m_strLiteral.GetAt( iPos ) != m_chPrompt )
  710. {
  711. int i;
  712. if ( bForward )
  713. {
  714. for ( i = iPos; i < iLen; ++i )
  715. {
  716. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  717. {
  718. iPos = i;
  719. break;
  720. }
  721. }
  722. if ( i == iLen )
  723. {
  724. if ( bBeep )
  725. {
  726. ::MessageBeep( ( UINT )-1 );
  727. }
  728. return FALSE;
  729. }
  730. }
  731. else
  732. {
  733. for ( i = iPos; i >= 0; --i )
  734. {
  735. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  736. {
  737. iPos = i;
  738. break;
  739. }
  740. }
  741. if ( i == -1 )
  742. {
  743. iPos++;
  744. if ( bBeep )
  745. {
  746. ::MessageBeep( ( UINT )-1 );
  747. }
  748. return FALSE;
  749. }
  750. }
  751. }
  752. return TRUE;
  753. }
  754. //-----------------------------------------------------------------------
  755. // Summary:
  756. // Deletes character in specified position
  757. // Parameters:
  758. // iPos - Position for character to be deleted.
  759. //-----------------------------------------------------------------------
  760. virtual void DeleteCharAt(int iPos)
  761. {
  762. if (!PosInRange(iPos))
  763. return;
  764. CString strBuffer = GetMaskedText( iPos+1 );
  765. strBuffer += m_chPrompt;
  766. int x = 0;
  767. int iLen = strBuffer.GetLength( );
  768. int i;
  769. for (i = iPos; i < m_strLiteral.GetLength( ); ++i )
  770. {
  771. if ( ( m_strLiteral.GetAt( i ) == m_chPrompt ) )
  772. {
  773. m_strWindowText.SetAt( i, strBuffer.GetAt( x ) );
  774. ++x;
  775. if ( x == iLen )
  776. break;
  777. }
  778. }
  779. }
  780. //-----------------------------------------------------------------------
  781. // Summary:
  782. // Inserts character to specified position.
  783. // Parameters:
  784. // iPos - Position to insert
  785. // nChar - Character to be inserted
  786. //-----------------------------------------------------------------------
  787. virtual void InsertCharAt(int iPos, TCHAR nChar)
  788. {
  789. if (!PosInRange(iPos))
  790. {
  791. return;
  792. }
  793. UINT uChar = nChar;
  794. if ( !CheckChar( iPos, uChar, FALSE ) )
  795. {
  796. return;
  797. }
  798. CString strBuffer = GetMaskedText( iPos );
  799. CString strTemp(nChar);
  800. int i, x = 0, iLen = strBuffer.GetLength( );
  801. for ( i = 1; i < strBuffer.GetLength(); i++ )
  802. {
  803. strTemp += strBuffer.GetAt( i-1 );
  804. }
  805. strBuffer = strTemp;
  806. for ( i = iPos; i < m_strWindowText.GetLength( ); i++ )
  807. {
  808. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  809. {
  810. m_strWindowText.SetAt( i, strBuffer[ x ] );
  811. x++;
  812. if ( x == iLen )
  813. break;
  814. }
  815. }
  816. }
  817. //-----------------------------------------------------------------------
  818. // 用途:
  819. // 在指定位置插入提示字符。
  820. // 参数:
  821. // iPos - 插入位置,要求必须是有效输入位置
  822. //-----------------------------------------------------------------------
  823. void InsertPromptCharAt(int iPos)
  824. {
  825. ASSERT(m_strLiteral.GetAt(iPos) == m_chPrompt);
  826. CString strBuffer = GetMaskedText( iPos );
  827. CString strTemp;
  828. int i;
  829. for ( i = 1; i < strBuffer.GetLength(); i++ )
  830. {
  831. strTemp += strBuffer.GetAt( i-1 );
  832. }
  833. strBuffer = strTemp;
  834. m_strWindowText.SetAt(iPos, m_chPrompt);
  835. int x = 0, iLen = strBuffer.GetLength();
  836. for ( i = iPos+1; i < m_strWindowText.GetLength( ); i++ )
  837. {
  838. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  839. {
  840. m_strWindowText.SetAt( i, strBuffer[ x ] );
  841. x++;
  842. if ( x == iLen )
  843. break;
  844. }
  845. }
  846. }
  847. //-----------------------------------------------------------------------
  848. // Summary:
  849. // Copies text to system clipboard
  850. // Parameters:
  851. // lpszText - Text to be copied
  852. // Returns:
  853. // TRUE if successful; otherwise returns FALSE
  854. //-----------------------------------------------------------------------
  855. virtual BOOL CopyToClipboard(LPCTSTR lpszText)
  856. {
  857. if ( !OpenClipboard( ) )
  858. return FALSE;
  859. ::EmptyClipboard( );
  860. int iLen = (int)_tcslen( lpszText ) + 1;
  861. // 原来的代码在处理UNICODE时是错误的,改进如下
  862. HGLOBAL hglbCopy = ::GlobalAlloc( GMEM_MOVEABLE, iLen*sizeof(TCHAR) );
  863. if ( hglbCopy == NULL )
  864. {
  865. ::CloseClipboard( );
  866. return FALSE;
  867. }
  868. LPTSTR lptstrCopy = ( TCHAR* )::GlobalLock( hglbCopy );
  869. memcpy(lptstrCopy, lpszText, iLen*sizeof(TCHAR));
  870. ::GlobalUnlock( hglbCopy );
  871. #ifdef _UNICODE
  872. ::SetClipboardData( CF_UNICODETEXT, hglbCopy );
  873. #else
  874. ::SetClipboardData( CF_TEXT, hglbCopy );
  875. #endif
  876. if ( !::CloseClipboard( ) )
  877. return FALSE;
  878. return TRUE;
  879. }
  880. //-----------------------------------------------------------------------
  881. // Summary:
  882. // Retrieves masked text of the control
  883. // Parameters:
  884. // iPos - Start position
  885. // Returns:
  886. // Masked text of the control.
  887. //-----------------------------------------------------------------------
  888. virtual CString GetMaskedText(int iPos = 0)
  889. {
  890. int iWndLen = m_strWindowText.GetLength( );
  891. ASSERT( iWndLen == m_strLiteral.GetLength( ) ); // must be equal in length
  892. CString strBuffer;
  893. int i;
  894. for ( i = iPos; i < iWndLen; ++i )
  895. {
  896. if ( m_strLiteral.GetAt( i ) == m_chPrompt )
  897. {
  898. strBuffer += m_strWindowText.GetAt( i );
  899. }
  900. }
  901. return strBuffer;
  902. }
  903. protected:
  904. //{{AFX_MASKEDIT_PRIVATE
  905. // 消息WM_SETTEXT的处理函数,目的是保证成员变量m_strWindowText和控件文本的一致性
  906. afx_msg LRESULT OnSetText(WPARAM wParam, LPARAM lParam)
  907. {
  908. LRESULT result = DefWindowProc(WM_SETTEXT, wParam, lParam);
  909. if ( !CanUseMask() )
  910. return result;
  911. if ( result && (LPCTSTR)m_strWindowText != (LPCTSTR)lParam )
  912. // 如果引发消息的语句为SetWindowText(m_strWindowText),则无需处理
  913. m_strWindowText = (LPCTSTR)lParam;
  914. return result;
  915. }
  916. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
  917. {
  918. if (!CanUseMask())
  919. {
  920. TBase::OnKeyDown( nChar, nRepCnt, nFlags ); // default processing.
  921. return;
  922. }
  923. int nStartChar, nEndChar;
  924. GetSel(nStartChar, nEndChar);
  925. if ( m_strMask.IsEmpty( ) )
  926. {
  927. TBase::OnKeyDown( nChar, nRepCnt, nFlags ); // default processing.
  928. return;
  929. }
  930. BOOL bShift = (::GetKeyState(VK_SHIFT) < 0);
  931. switch ( nChar )
  932. {
  933. // 原来的代码在处理光标键及Home、End键时,要跳过固定字符。
  934. // 我觉得用缺省功能更好一些,VB6的MaskEdit控件用的也是缺省功能
  935. case VK_INSERT:
  936. {
  937. if ( bShift )
  938. {
  939. MaskPaste( );
  940. }
  941. else
  942. {
  943. m_bOverType = !m_bOverType; // set the type-over flag
  944. }
  945. }
  946. return;
  947. case VK_DELETE:
  948. {
  949. if ( nStartChar == nEndChar )
  950. {
  951. if ( CorrectPosition( nStartChar ) )
  952. {
  953. DeleteCharAt( nStartChar );
  954. m_bModified = TRUE;
  955. SetWindowText( m_strWindowText );
  956. nEndChar = nStartChar;
  957. SetSel( nStartChar, nEndChar );
  958. }
  959. }
  960. else
  961. {
  962. MaskClear( );
  963. }
  964. }
  965. return;
  966. case VK_SPACE:
  967. {
  968. int iPos = nStartChar;
  969. if ( CorrectPosition( iPos ) )
  970. {
  971. if ( ProcessMask( nChar, iPos ) )
  972. {
  973. // 空格为有效字符
  974. if ( nStartChar != nEndChar )
  975. {
  976. // 当前有文本被选中,无论控件处于插入还是
  977. // 覆写状态,均先删除被选文本,再插入空格
  978. MaskClear();
  979. InsertCharAt( iPos, ' ' );
  980. }
  981. else
  982. {
  983. // 没有文本被选中,则根据状态决定是插入还是覆写空格
  984. if ( CanOverType( ) )
  985. m_strWindowText.SetAt( iPos, ' ' );
  986. else
  987. InsertCharAt( iPos, ' ' );
  988. }
  989. }
  990. else
  991. {
  992. // 空格不是有效字符,则在当前位置输入一个提示字符
  993. if ( nStartChar != nEndChar )
  994. {
  995. // 当前有文本被选中
  996. // 如果选中的都是固定字符,则发出错误提示音并返回
  997. if (iPos >= nEndChar)
  998. {
  999. ::MessageBeep( ( UINT )-1 );
  1000. return;
  1001. }
  1002. // 删除被选文本
  1003. MaskClear();
  1004. // 插入一个提示字符
  1005. InsertPromptCharAt(iPos);
  1006. }
  1007. else
  1008. {
  1009. // 没有文本被选中,则根据状态决定是插入还是覆写提示字符
  1010. if ( CanOverType( ) )
  1011. m_strWindowText.SetAt( iPos, m_chPrompt );
  1012. else
  1013. InsertPromptCharAt(iPos);
  1014. }
  1015. }
  1016. m_bModified = TRUE;
  1017. SetWindowText( m_strWindowText );
  1018. nStartChar = iPos + 1;
  1019. if ( nStartChar < m_strLiteral.GetLength() )
  1020. CorrectPosition( nStartChar, TRUE, FALSE );
  1021. nEndChar = nStartChar;
  1022. SetSel( nStartChar, nEndChar );
  1023. }
  1024. }
  1025. return;
  1026. case VK_BACK:
  1027. {
  1028. // 如果有字符被选中,则执行Clear操作,即相当于按了Del键
  1029. if (nStartChar != nEndChar)
  1030. {
  1031. MaskClear();
  1032. return;
  1033. }
  1034. if ( nStartChar > 0 && nStartChar <= m_strLiteral.GetLength( ) )
  1035. {
  1036. nStartChar--;
  1037. if ( CorrectPosition( nStartChar, FALSE ) )
  1038. {
  1039. TCHAR ch = m_chPrompt;
  1040. // get the masked literal representation.
  1041. if ( !m_strDefault.IsEmpty( ) )
  1042. {
  1043. ch = m_strDefault.GetAt( nStartChar );
  1044. }
  1045. m_strWindowText.SetAt( nStartChar, ch );
  1046. m_bModified = TRUE;
  1047. SetWindowText( m_strWindowText );
  1048. nEndChar = nStartChar;
  1049. SetSel( nStartChar, nEndChar );
  1050. }
  1051. }
  1052. else
  1053. {
  1054. ::MessageBeep( ( UINT )-1 );
  1055. }
  1056. }
  1057. return;
  1058. }
  1059. TBase::OnKeyDown( nChar, nRepCnt, nFlags );
  1060. }
  1061. afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  1062. {
  1063. if (!CanUseMask())
  1064. {
  1065. TBase::OnChar(nChar, nRepCnt, nFlags);
  1066. return;
  1067. }
  1068. switch ( nChar )
  1069. {
  1070. case ' ' : // 空格
  1071. case '\b': // 回退键
  1072. return; // 上述字符已经在消息WM_KEYDOWN中得到了处理
  1073. }
  1074. int nStartChar, nEndChar;
  1075. GetSel(nStartChar, nEndChar);
  1076. if (!CheckChar(nStartChar, nChar))
  1077. {
  1078. return;
  1079. }
  1080. if ( nStartChar != nEndChar )
  1081. {
  1082. // 输入字符时有文本被选中,无论当前处于插入还是
  1083. // 覆写状态,均先删除被选文本,再插入新字符
  1084. MaskClear();
  1085. if ( CorrectPosition(nStartChar) )
  1086. InsertCharAt( nStartChar, ( TCHAR )nChar );
  1087. }
  1088. else
  1089. {
  1090. // 没有文本被选中,则根据状态决定是插入还是覆写
  1091. if ( CorrectPosition(nStartChar) )
  1092. {
  1093. if ( CanOverType( ) )
  1094. m_strWindowText.SetAt( nStartChar, ( TCHAR )nChar );
  1095. else
  1096. InsertCharAt( nStartChar, ( TCHAR )nChar );
  1097. }
  1098. }
  1099. m_bModified = TRUE;
  1100. SetWindowText( m_strWindowText );
  1101. nStartChar++;
  1102. if ( nStartChar < m_strLiteral.GetLength() )
  1103. CorrectPosition( nStartChar, TRUE, FALSE );
  1104. nEndChar = nStartChar;
  1105. SetSel( nStartChar, nEndChar );
  1106. }
  1107. afx_msg void OnSetFocus(CWnd* pOldWnd)
  1108. {
  1109. TBase::OnSetFocus(pOldWnd);
  1110. if (!CanUseMask())
  1111. {
  1112. return;
  1113. }
  1114. int nStartChar, nEndChar;
  1115. GetSel(nStartChar, nEndChar);
  1116. CorrectPosition( nStartChar, TRUE, FALSE );
  1117. nEndChar = nStartChar;
  1118. SetSel( nStartChar, nEndChar );
  1119. }
  1120. afx_msg void OnUpdateEditUndo(CCmdUI* pCmdUI)
  1121. {
  1122. if (!CanUseMask())
  1123. {
  1124. pCmdUI->Enable( CanUndo( ) );
  1125. }
  1126. else
  1127. {
  1128. pCmdUI->Enable( FALSE );
  1129. }
  1130. }
  1131. //}}AFX_MASKEDIT_PRIVATE
  1132. //-----------------------------------------------------------------------
  1133. // Summary:
  1134. // The framework calls this member function when the user selects an item from a menu
  1135. // Parameters:
  1136. // wParam - The low-order word of wParam identifies the command ID of the menu item, control, or accelerator. The high-order word of wParam specifies the notification message if the message is from a control. If the message is from an accelerator, the high-order word is 1. If the message is from a menu, the high-order word is 0
  1137. // lParam - Identifies the control that sends the message if the message is from a control. Otherwise, lParam is 0.
  1138. // Returns:
  1139. // An application returns nonzero if it processes this message; otherwise 0.
  1140. //-----------------------------------------------------------------------
  1141. virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam)
  1142. {
  1143. switch (LOWORD(wParam))
  1144. {
  1145. case ID_EDIT_CUT:
  1146. if ( !CanUseMask( )) TBase::Cut(); else MaskCut( );
  1147. return TRUE;
  1148. case ID_EDIT_COPY:
  1149. if ( !CanUseMask( )) TBase::Copy(); else MaskCopy( );
  1150. return TRUE;
  1151. case ID_EDIT_PASTE:
  1152. if ( !CanUseMask()) TBase::Paste(); else MaskPaste( );
  1153. return TRUE;
  1154. case ID_EDIT_CLEAR:
  1155. if ( !CanUseMask()) TBase::Clear(); else MaskClear( );
  1156. return TRUE;
  1157. case ID_EDIT_UNDO:
  1158. if ( !CanUseMask()) TBase::Undo(); else MaskUndo( );
  1159. return TRUE;
  1160. case ID_EDIT_SELECT_ALL:
  1161. MaskSelectAll( );
  1162. return TRUE;
  1163. }
  1164. return TBase::OnCommand(wParam, lParam);
  1165. }
  1166. protected:
  1167. BOOL m_bUseMask; // TRUE to use the edit mask.
  1168. BOOL m_bOverType; // TRUE to over type the text, set with VK_INSERT key press.
  1169. BOOL m_bModified; // TRUE if mask edit has been modified.
  1170. TCHAR m_chPrompt; // Prompt character used to identify the text entry.
  1171. CString m_strMask; // Buffer that holds the actual edit mask value.
  1172. CString m_strDefault; // Contains the edit controls default display text.
  1173. CString m_strWindowText; // Buffer that holds the actual edit text.
  1174. CString m_strLiteral; // Literal format that restricts where the user can enter text.
  1175. };
  1176. //{{AFX_MASKEDIT_PRIVATE
  1177. #define ON_MASKEDIT_REFLECT\
  1178. ON_MESSAGE_VOID(WM_CUT, MaskCut)\
  1179. ON_MESSAGE_VOID(WM_PASTE, MaskPaste)\
  1180. ON_MESSAGE_VOID(WM_CLEAR, MaskClear)\
  1181. ON_MESSAGE_VOID(WM_UNDO, MaskUndo)\
  1182. ON_MESSAGE_VOID(EM_UNDO, MaskUndo)\
  1183. ON_MESSAGE(EM_CANUNDO, MaskCanUndo)\
  1184. ON_MESSAGE_VOID(WM_COPY, MaskCopy)\
  1185. ON_MESSAGE(WM_SETTEXT, OnSetText)\
  1186. ON_WM_KEYDOWN()\
  1187. ON_WM_CHAR()\
  1188. ON_WM_SETFOCUS\
  1189. //}}AFX_MASKEDIT_PRIVATE