home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / ctrltest / paredit.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  4KB  |  131 lines

  1. // paredit.cpp: C++ derived edit control for numbers/letters etc
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "ctrltest.h"
  15.  
  16. #include "paredit.h"
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // ParsedEdit
  20.  
  21. CParsedEdit::CParsedEdit()
  22. {
  23.     m_wParseStyle = 0;
  24. }
  25.  
  26. BEGIN_MESSAGE_MAP(CParsedEdit, CEdit)
  27.     //{{AFX_MSG_MAP(CParsedEdit)
  28.     ON_WM_CHAR()
  29.     ON_WM_VSCROLL()     // for associated spin controls
  30.     //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // Creating from C++ code
  35.  
  36. BOOL CParsedEdit::Create(DWORD dwStyle, const RECT& rect,
  37.         CWnd* pParentWnd, UINT nID)
  38. {
  39.     m_wParseStyle = LOWORD(dwStyle);
  40.     // figure out edit control style
  41.     DWORD dwEditStyle = MAKELONG(ES_LEFT, HIWORD(dwStyle));
  42.     return CWnd::Create(_T("EDIT"), NULL, dwEditStyle, rect, pParentWnd, nID);
  43. }
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // Aliasing on top of an existing Edit control
  47.  
  48. BOOL CParsedEdit::SubclassEdit(UINT nID, CWnd* pParent, WORD wParseStyle)
  49. {
  50.     m_wParseStyle = wParseStyle;
  51.     return SubclassDlgItem(nID, pParent);
  52. }
  53.  
  54. /////////////////////////////////////////////////////////////////////////////
  55. // Input character filter
  56.  
  57. void CParsedEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  58. {
  59.     WORD type;
  60.  
  61.     if (nChar < 0x20)
  62.         type = PES_ALL;                         // always allow control chars
  63.     else if (IsCharAlphaNumeric((TCHAR)nChar) && !IsCharAlpha((TCHAR)nChar))
  64.         type = PES_NUMBERS;
  65.     else if (IsCharAlpha((TCHAR)nChar))
  66.         type = PES_LETTERS;
  67.     else
  68.         type = PES_OTHERCHARS;
  69.  
  70.     if (m_wParseStyle & type)
  71.     {
  72.         CEdit::OnChar(nChar, nRepCnt, nFlags);  // permitted
  73.     }
  74.     else
  75.     {
  76.         // illegal character - inform parent
  77.         OnBadInput();
  78.     }
  79. }
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // Spin controls will send scroll messages
  83.  
  84. void CParsedEdit::OnVScroll(UINT nSBCode, UINT, CScrollBar*)
  85. {
  86.     int nDelta = 0;
  87.     if (nSBCode == SB_LINEDOWN)
  88.         nDelta = -1;
  89.     else if (nSBCode == SB_LINEUP)
  90.         nDelta = +1;
  91.     else
  92.         return; // nothing special
  93.  
  94.     // set the focus to this edit item and select it all
  95.     SetFocus();
  96.  
  97.     //Get the number in the control.
  98.     BOOL bOk;
  99.     int nOld = GetParent()->GetDlgItemInt(GetDlgCtrlID(), &bOk);
  100.     if (bOk)
  101.     {
  102.         // The MuScrl32 control also supports range checking
  103.         // for this example, we just prevent overflow
  104.         int nNew = nOld + nDelta;
  105.         if (nNew >= 0 && nNew <= 32767)
  106.             GetParent()->SetDlgItemInt(GetDlgCtrlID(), nNew);
  107.         else
  108.             bOk = FALSE;
  109.     }
  110.  
  111.     if (!bOk)
  112.         OnBadInput();
  113.     SetSel(0, -1);
  114. }
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // default bad input handler, beep (unless parent notification
  118. //    returns -1.  Most parent dialogs will return 0 or 1 for command
  119. //    handlers (i.e. Beep is the default)
  120.  
  121. void CParsedEdit::OnBadInput()
  122. {
  123.     if (GetParent()->SendMessage(WM_COMMAND,
  124.         MAKELONG(GetDlgCtrlID(), PEN_ILLEGALCHAR), (LPARAM)m_hWnd) != -1)
  125.     {
  126.         MessageBeep((UINT)-1);
  127.     }
  128. }
  129.  
  130. /////////////////////////////////////////////////////////////////////////////
  131.