TestComboListCtrlDlg.cpp
上传用户:htjk88
上传日期:2010-03-27
资源大小:29k
文件大小:7k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. // TestComboListCtrlDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "TestComboListCtrl.h"
  5. #include "TestComboListCtrlDlg.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. // CTestComboListCtrlDlg dialog
  50. CTestComboListCtrlDlg::CTestComboListCtrlDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CTestComboListCtrlDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CTestComboListCtrlDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CTestComboListCtrlDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CTestComboListCtrlDlg)
  63. DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
  64. //}}AFX_DATA_MAP
  65. }
  66. BEGIN_MESSAGE_MAP(CTestComboListCtrlDlg, CDialog)
  67. //{{AFX_MSG_MAP(CTestComboListCtrlDlg)
  68. ON_WM_SYSCOMMAND()
  69. ON_WM_PAINT()
  70. ON_WM_QUERYDRAGICON()
  71. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  72. ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  73. //}}AFX_MSG_MAP
  74. ON_MESSAGE(WM_VALIDATE, OnEndLabelEditVariableCriteria)
  75. ON_MESSAGE(WM_SET_ITEMS, PopulateComboList)
  76. END_MESSAGE_MAP()
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CTestComboListCtrlDlg message handlers
  79. BOOL CTestComboListCtrlDlg::OnInitDialog()
  80. {
  81. CDialog::OnInitDialog();
  82. // Add "About..." menu item to system menu.
  83. // IDM_ABOUTBOX must be in the system command range.
  84. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  85. ASSERT(IDM_ABOUTBOX < 0xF000);
  86. CMenu* pSysMenu = GetSystemMenu(FALSE);
  87. if (pSysMenu != NULL)
  88. {
  89. CString strAboutMenu;
  90. strAboutMenu.LoadString(IDS_ABOUTBOX);
  91. if (!strAboutMenu.IsEmpty())
  92. {
  93. pSysMenu->AppendMenu(MF_SEPARATOR);
  94. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  95. }
  96. }
  97. // Set the icon for this dialog.  The framework does this automatically
  98. //  when the application's main window is not a dialog
  99. SetIcon(m_hIcon, TRUE); // Set big icon
  100. SetIcon(m_hIcon, FALSE); // Set small icon
  101. // TODO: Add extra initialization here
  102. //m_ListCtrl
  103. m_ListCtrl.InsertColumn(0, "编号", LVCFMT_LEFT, 60);
  104. m_ListCtrl.InsertColumn(1, "姓名", LVCFMT_LEFT, 150);
  105. m_ListCtrl.InsertColumn(2, "学号", LVCFMT_LEFT, 150);
  106. m_ListCtrl.InsertColumn(3, "性别", LVCFMT_LEFT, 150);
  107. CString strValidChars;//
  108. m_ListCtrl.SetReadOnlyColumns(0);//read only
  109. strValidChars = "";
  110. m_ListCtrl.SetColumnValidEditCtrlCharacters(strValidChars,1);//none control edit 
  111. strValidChars = "0123456789.";
  112. m_ListCtrl.SetColumnValidEditCtrlCharacters(strValidChars,2);//digital only edit
  113. m_ListCtrl.SetComboColumns(3,TRUE);
  114. m_ListCtrl.EnableVScroll(); 
  115. m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  116. CString str;
  117. for(int i=0;i<30;i++)
  118. {
  119. str.Format("%d",i+1);
  120. m_ListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE, i, 
  121. str, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED, 0, 0);
  122. m_ListCtrl.SetItemText(i,1,"张三");
  123. m_ListCtrl.SetItemText(i,2,"1234");
  124. m_ListCtrl.SetItemText(i,3,"男");
  125. }
  126. return TRUE;  // return TRUE  unless you set the focus to a control
  127. }
  128. void CTestComboListCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  129. {
  130. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  131. {
  132. CAboutDlg dlgAbout;
  133. dlgAbout.DoModal();
  134. }
  135. else
  136. {
  137. CDialog::OnSysCommand(nID, lParam);
  138. }
  139. }
  140. // If you add a minimize button to your dialog, you will need the code below
  141. //  to draw the icon.  For MFC applications using the document/view model,
  142. //  this is automatically done for you by the framework.
  143. void CTestComboListCtrlDlg::OnPaint() 
  144. {
  145. if (IsIconic())
  146. {
  147. CPaintDC dc(this); // device context for painting
  148. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  149. // Center icon in client rectangle
  150. int cxIcon = GetSystemMetrics(SM_CXICON);
  151. int cyIcon = GetSystemMetrics(SM_CYICON);
  152. CRect rect;
  153. GetClientRect(&rect);
  154. int x = (rect.Width() - cxIcon + 1) / 2;
  155. int y = (rect.Height() - cyIcon + 1) / 2;
  156. // Draw the icon
  157. dc.DrawIcon(x, y, m_hIcon);
  158. }
  159. else
  160. {
  161. CDialog::OnPaint();
  162. }
  163. }
  164. // The system calls this to obtain the cursor to display while the user drags
  165. //  the minimized window.
  166. HCURSOR CTestComboListCtrlDlg::OnQueryDragIcon()
  167. {
  168. return (HCURSOR) m_hIcon;
  169. }
  170. LRESULT CTestComboListCtrlDlg::OnEndLabelEditVariableCriteria(WPARAM wParam, LPARAM lParam) 
  171. {
  172. LV_DISPINFO* pDispInfo = (LV_DISPINFO*)lParam;
  173. // TODO: Add your control notification handler code here
  174. return 1;
  175. }
  176. LRESULT CTestComboListCtrlDlg::PopulateComboList(WPARAM wParam, LPARAM lParam)
  177. {
  178. // Get the Combobox window pointer
  179. CComboBox* pInPlaceCombo = static_cast<CComboBox*> (GetFocus());
  180. // Get the inplace combbox top left
  181. CRect obWindowRect;
  182. pInPlaceCombo->GetWindowRect(&obWindowRect);
  183. CPoint obInPlaceComboTopLeft(obWindowRect.TopLeft()); 
  184. // Get the active list
  185. // Get the control window rect
  186. // If the inplace combobox top left is in the rect then
  187. // The control is the active control
  188. m_ListCtrl.GetWindowRect(&obWindowRect);
  189. int iColIndex = (int )wParam;
  190. CStringList* pComboList = reinterpret_cast<CStringList*>(lParam);
  191. pComboList->RemoveAll(); 
  192. if (obWindowRect.PtInRect(obInPlaceComboTopLeft)) 
  193. {
  194. if(iColIndex==3)
  195. {
  196. pComboList->AddTail("男");
  197. pComboList->AddTail("女");
  198. }
  199. }
  200. return true;
  201. }
  202. void CTestComboListCtrlDlg::OnButton1() 
  203. //ADD
  204. {
  205. // TODO: Add your control notification handler code here
  206. UpdateData(TRUE);
  207. int i =m_ListCtrl.GetItemCount();
  208. CString str;
  209. str.Format("%d",i+1);
  210. m_ListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE, i+1, 
  211. str, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED, 0, 0);
  212. UpdateData(FALSE);
  213. }
  214. void CTestComboListCtrlDlg::OnButton2() 
  215. //DELETE 
  216. {
  217. // TODO: Add your control notification handler code here
  218. m_ListCtrl.DeleteItem(m_ListCtrl.GetItemCount()-1);
  219. }