MCComboBoxSampleDlg.cpp
上传用户:hzw123hzw
上传日期:2007-01-01
资源大小:18k
文件大小:7k
源码类别:

组合框控件

开发平台:

Visual C++

  1. // MCComboBoxSampleDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MCComboBoxSample.h"
  5. #include "MCComboBoxSampleDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CMCComboBoxSampleDlg dialog
  50. CMCComboBoxSampleDlg::CMCComboBoxSampleDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CMCComboBoxSampleDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CMCComboBoxSampleDlg)
  54. m_ComboBoxString1 = _T("");
  55. m_ComboBoxString2 = _T("");
  56. m_ComboBoxString3 = _T("");
  57. m_ComboBoxString4 = _T("");
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CMCComboBoxSampleDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CMCComboBoxSampleDlg)
  66. DDX_Control(pDX, IDC_COMBO4, m_ComboBoxControl4);
  67. DDX_Control(pDX, IDC_COMBO3, m_ComboBoxControl3);
  68. DDX_Control(pDX, IDC_COMBO2, m_ComboBoxControl2);
  69. DDX_Control(pDX, IDC_COMBO1, m_ComboBoxControl1);
  70. DDX_CBString(pDX, IDC_COMBO1, m_ComboBoxString1);
  71. DDX_CBString(pDX, IDC_COMBO2, m_ComboBoxString2);
  72. DDX_CBString(pDX, IDC_COMBO3, m_ComboBoxString3);
  73. DDX_CBString(pDX, IDC_COMBO4, m_ComboBoxString4);
  74. //}}AFX_DATA_MAP
  75. }
  76. BEGIN_MESSAGE_MAP(CMCComboBoxSampleDlg, CDialog)
  77. //{{AFX_MSG_MAP(CMCComboBoxSampleDlg)
  78. ON_WM_SYSCOMMAND()
  79. ON_WM_PAINT()
  80. ON_WM_QUERYDRAGICON()
  81. ON_BN_CLICKED(IDC_ENABLEDDISABLED, OnEnableddisabled)
  82. ON_CBN_DROPDOWN(IDC_COMBO1, OnDropdownCombo1)
  83. //}}AFX_MSG_MAP
  84. END_MESSAGE_MAP()
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CMCComboBoxSampleDlg message handlers
  87. BOOL CMCComboBoxSampleDlg::OnInitDialog()
  88. {
  89. CDialog::OnInitDialog();
  90. // Add "About..." menu item to system menu.
  91. // IDM_ABOUTBOX must be in the system command range.
  92. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  93. ASSERT(IDM_ABOUTBOX < 0xF000);
  94. CMenu* pSysMenu = GetSystemMenu(FALSE);
  95. CString strAboutMenu;
  96. strAboutMenu.LoadString(IDS_ABOUTBOX);
  97. if (!strAboutMenu.IsEmpty())
  98. {
  99. pSysMenu->AppendMenu(MF_SEPARATOR);
  100. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  101. }
  102. // Set the icon for this dialog.  The framework does this automatically
  103. //  when the application's main window is not a dialog
  104. SetIcon(m_hIcon, TRUE); // Set big icon
  105. SetIcon(m_hIcon, FALSE); // Set small icon
  106. // TODO: Add extra initialization here
  107. CString ColumnString0, ColumnString1, ColumnString2, ColumnString3;
  108. int Index;
  109. m_ComboBoxControl1.FormatComboBox();
  110. m_ComboBoxControl1.SetColumnWidth(30, 100);
  111. for (Index = 0; Index < 20; Index++)
  112. {
  113. ColumnString0.Format("ID%u", Index);
  114. ColumnString1.Format("Description%u", Index);
  115. m_ComboBoxControl1.AddRow(ColumnString0, ColumnString1);
  116. }
  117. m_ComboBoxControl2.FormatComboBox(2, 1);
  118. m_ComboBoxControl2.SetColumnWidth(150, 0);
  119. m_ComboBoxControl2.SetColumnAlignStyle(DT_LEFT, DT_CENTER);
  120. for (Index = 0; Index < 20; Index++)
  121. {
  122. ColumnString0.Format("ID%u", Index);
  123. ColumnString1.Format("Description%u", Index);
  124. m_ComboBoxControl2.AddRow(ColumnString1, ColumnString0);
  125. }
  126. m_ComboBoxControl3.FormatComboBox(3);
  127. m_ComboBoxControl3.SetColumnWidth(30, 90, 100);
  128. m_ComboBoxControl3.SetColumnAlignStyle(DT_CENTER, DT_CENTER, DT_RIGHT);
  129. for (Index = 0; Index < 20; Index++)
  130. {
  131. ColumnString0.Format("ID%u", Index);
  132. ColumnString1.Format("Description%u", Index);
  133. ColumnString2.Format("Remarks%u", Index);
  134. m_ComboBoxControl3.AddRow(ColumnString0, ColumnString1, ColumnString2);
  135. }
  136.    m_ComboBoxControl4.FormatComboBox(4);
  137. m_ComboBoxControl4.SetColumnWidth(40, 50, 50, 50);
  138. m_ComboBoxControl4.SetColumnAlignStyle(DT_LEFT, DT_RIGHT, DT_RIGHT, DT_RIGHT);
  139. for (Index = 0; Index < 20; Index++)
  140. {
  141. ColumnString0.Format("Rate%u", Index);
  142. ColumnString1.Format("%.2f", Index / 2.0);
  143. ColumnString2.Format("%.2f", Index / 3.0);
  144. ColumnString3.Format("%.2f", Index / 4.0);
  145. m_ComboBoxControl4.AddRow(ColumnString0, ColumnString1, ColumnString2, ColumnString3);
  146. }
  147. return TRUE;  // return TRUE  unless you set the focus to a control
  148. }
  149. void CMCComboBoxSampleDlg::OnSysCommand(UINT nID, LPARAM lParam)
  150. {
  151. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  152. {
  153. CAboutDlg dlgAbout;
  154. dlgAbout.DoModal();
  155. }
  156. else
  157. {
  158. CDialog::OnSysCommand(nID, lParam);
  159. }
  160. }
  161. // If you add a minimize button to your dialog, you will need the code below
  162. //  to draw the icon.  For MFC applications using the document/view model,
  163. //  this is automatically done for you by the framework.
  164. void CMCComboBoxSampleDlg::OnPaint() 
  165. {
  166. if (IsIconic())
  167. {
  168. CPaintDC dc(this); // device context for painting
  169. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  170. // Center icon in client rectangle
  171. int cxIcon = GetSystemMetrics(SM_CXICON);
  172. int cyIcon = GetSystemMetrics(SM_CYICON);
  173. CRect rect;
  174. GetClientRect(&rect);
  175. int x = (rect.Width() - cxIcon + 1) / 2;
  176. int y = (rect.Height() - cyIcon + 1) / 2;
  177. // Draw the icon
  178. dc.DrawIcon(x, y, m_hIcon);
  179. }
  180. else
  181. {
  182. CDialog::OnPaint();
  183. }
  184. }
  185. // The system calls this to obtain the cursor to display while the user drags
  186. //  the minimized window.
  187. HCURSOR CMCComboBoxSampleDlg::OnQueryDragIcon()
  188. {
  189. return (HCURSOR) m_hIcon;
  190. }
  191. void CMCComboBoxSampleDlg::OnEnableddisabled() 
  192. {
  193. // TODO: Add your control notification handler code here
  194. BOOL ControlEnabled = GetDlgItem(IDC_COMBO3)->IsWindowEnabled();
  195. GetDlgItem(IDC_COMBO1)->EnableWindow(!ControlEnabled);
  196. GetDlgItem(IDC_COMBO2)->EnableWindow(!ControlEnabled);
  197. GetDlgItem(IDC_COMBO3)->EnableWindow(!ControlEnabled);
  198. GetDlgItem(IDC_COMBO4)->EnableWindow(!ControlEnabled);
  199. GetDlgItem(IDC_ENABLEDDISABLED)->SetWindowText(ControlEnabled ? "Enable All ComboBox Controls" : "Disable All ComboBox Controls");
  200. }
  201. void CMCComboBoxSampleDlg::OnOK() 
  202. {
  203. CString TextString;
  204. UpdateData(TRUE);
  205. TextString.Format("Text in combo box 1 is %snText in combo box 2 is %snText in combo box 3 is %snText in combo box 4 is %sn",
  206. m_ComboBoxString1, m_ComboBoxString2, m_ComboBoxString3, m_ComboBoxString4);
  207. MessageBox(TextString);
  208. CDialog::OnOK();
  209. }
  210. void CMCComboBoxSampleDlg::OnDropdownCombo1() 
  211. {
  212. m_ComboBoxControl2.ShowDropDown(FALSE);
  213. }