Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.6 KiB

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