home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / CTRLTEST / DERTEST.CP_ / DERTEST.CP
Encoding:
Text File  |  1993-02-08  |  4.6 KB  |  157 lines

  1. // deredit.cpp : C++ derived Edit control example
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 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 Microsoft
  9. // WinHelp 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.  
  20. class CDerEditDlg : public CDialog
  21. {
  22. protected:
  23.     CFont*  m_pFont;
  24.     // construct
  25.     CParsedEdit m_edit1, m_edit2, m_edit3, m_edit4;
  26.     CStatic m_static1, m_static2, m_static3, m_static4;
  27. public:
  28.     //{{AFX_DATA(CDerEditDlg)
  29.         enum { IDD = IDD_DERIVED_EDIT };
  30.     //}}AFX_DATA
  31.     CDerEditDlg()
  32.         : CDialog(CDerEditDlg::IDD)
  33.         { }
  34.  
  35.     void OnSetFont(CFont* pFont)
  36.             { m_pFont = pFont; }
  37.  
  38.     BOOL OnInitDialog();
  39.     //{{AFX_MSG(CDerEditDlg)
  40.         virtual void OnOK();
  41.     //}}AFX_MSG
  42.     DECLARE_MESSAGE_MAP()
  43. };
  44.  
  45. BEGIN_MESSAGE_MAP(CDerEditDlg, CDialog)
  46.     //{{AFX_MSG_MAP(CDerEditDlg)
  47.     //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49.  
  50.  
  51. BOOL CDerEditDlg::OnInitDialog()
  52.     // create children on InitDialog 
  53.     //  (not in CDerEditDlg constructor since the dialog has
  54.     //    no attached HWND in the constructor)
  55. {
  56.     // This is an example of the _incorrect_ way to create a dialog
  57.     // The following code show you what you should _not_ do:
  58.     //    1) do not use hard coded numbers for coordinates and sizes
  59.     //     (these will break when the font sizes changes and are
  60.     //     hard to edit and maintain).
  61.     //    2) do not put strings in code, they should be in resources.
  62.     //    3) as you can see below the programming steps required
  63.     //     to create controls, pass the correct creation parameters,
  64.     //     and set the appropriate font, is complicated and error prone.
  65.  
  66.     const int yStart = 8;
  67.     const int height = 30;
  68.  
  69.     CPoint whereLabel(10, yStart);
  70.     CSize sizeLabel(80, 24);
  71.  
  72.     CPoint whereEdit(90, yStart);
  73.     CSize sizeEdit(140, 24);
  74.  
  75.     m_static1.Create("Letters:", WS_VISIBLE | WS_CHILD | SS_LEFT,
  76.         CRect(whereLabel, sizeLabel), this, -1);
  77.     m_static1.SetFont(m_pFont);
  78.     whereLabel.y += height;
  79.     m_edit1.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER |
  80.         PES_LETTERS,
  81.         CRect(whereEdit, sizeEdit), this, IDC_EDIT1);
  82.     m_edit1.SetFont(m_pFont);
  83.     whereEdit.y += height;
  84.  
  85.     m_static2.Create("Numbers:", WS_VISIBLE | WS_CHILD | SS_LEFT,
  86.         CRect(whereLabel, sizeLabel), this, -1);
  87.     m_static2.SetFont(m_pFont);
  88.     whereLabel.y += height;
  89.     m_edit2.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER |
  90.         PES_NUMBERS,
  91.         CRect(whereEdit, sizeEdit), this, IDC_EDIT2);
  92.     m_edit2.SetFont(m_pFont);
  93.     whereEdit.y += height;
  94.  
  95.     m_static3.Create("Either:", WS_VISIBLE | WS_CHILD | SS_LEFT,
  96.         CRect(whereLabel, sizeLabel), this, -1);
  97.     m_static3.SetFont(m_pFont);
  98.     whereLabel.y += height;
  99.     m_edit3.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER |
  100.         PES_LETTERS | PES_NUMBERS,
  101.         CRect(whereEdit, sizeEdit), this, IDC_EDIT3);
  102.     m_edit3.SetFont(m_pFont);
  103.     whereEdit.y += height;
  104.  
  105.     m_static4.Create("Anything:", WS_VISIBLE | WS_CHILD | SS_LEFT,
  106.         CRect(whereLabel, sizeLabel), this, -1);
  107.     m_static4.SetFont(m_pFont);
  108.     whereLabel.y += height;
  109.     m_edit4.Create(WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER |
  110.         PES_ALL,
  111.         CRect(whereEdit, sizeEdit), this, IDC_EDIT4);
  112.     m_edit4.SetFont(m_pFont);
  113.     whereEdit.y += height;
  114.  
  115.     // change the dialog height so everything fits
  116.     int yBottom = whereEdit.y + height * 2; // extra space
  117.     CRect rect;
  118.     GetWindowRect(rect);
  119.     VERIFY(SetWindowPos(NULL, -1, -1, rect.Width(), yBottom,
  120.         SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
  121.  
  122.     // set focus to first one
  123.     m_edit1.SetFocus();
  124.     return FALSE;   // focus set
  125. }
  126.  
  127. void CDerEditDlg::OnOK()
  128. {
  129. #ifdef _DEBUG
  130.     // dump results, normally you would do something with these
  131.     CString s;
  132.     m_edit1.GetWindowText(s);
  133.     TRACE("edit1 = '%s'\n", (const char*) s);
  134.     m_edit2.GetWindowText(s);
  135.     TRACE("edit2 = '%s'\n", (const char*) s);
  136.     m_edit3.GetWindowText(s);
  137.     TRACE("edit3 = '%s'\n", (const char*) s);
  138.     m_edit4.GetWindowText(s);
  139.     TRACE("edit4 = '%s'\n", (const char*) s);
  140. #endif
  141.  
  142.     EndDialog(IDOK);
  143. }
  144.  
  145. /////////////////////////////////////////////////////////////////////////////
  146. // Run the test
  147.  
  148. void CTestWindow::OnTestDerivedEdit()
  149. {
  150.     TRACE("running dialog with special derived CParsedEdit controls in it\n");
  151.     CDerEditDlg dlg;
  152.     dlg.DoModal();
  153. }
  154.  
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157.