TestComboListCtrlDlg.cpp
上传用户:htjk88
上传日期:2010-03-27
资源大小:29k
文件大小:7k
源码类别:
ListView/ListBox
开发平台:
Visual C++
- // TestComboListCtrlDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "TestComboListCtrl.h"
- #include "TestComboListCtrlDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestComboListCtrlDlg dialog
- CTestComboListCtrlDlg::CTestComboListCtrlDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTestComboListCtrlDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTestComboListCtrlDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CTestComboListCtrlDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTestComboListCtrlDlg)
- DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTestComboListCtrlDlg, CDialog)
- //{{AFX_MSG_MAP(CTestComboListCtrlDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_VALIDATE, OnEndLabelEditVariableCriteria)
- ON_MESSAGE(WM_SET_ITEMS, PopulateComboList)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTestComboListCtrlDlg message handlers
- BOOL CTestComboListCtrlDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- // TODO: Add extra initialization here
- //m_ListCtrl
- m_ListCtrl.InsertColumn(0, "编号", LVCFMT_LEFT, 60);
- m_ListCtrl.InsertColumn(1, "姓名", LVCFMT_LEFT, 150);
- m_ListCtrl.InsertColumn(2, "学号", LVCFMT_LEFT, 150);
- m_ListCtrl.InsertColumn(3, "性别", LVCFMT_LEFT, 150);
- CString strValidChars;//
- m_ListCtrl.SetReadOnlyColumns(0);//read only
- strValidChars = "";
- m_ListCtrl.SetColumnValidEditCtrlCharacters(strValidChars,1);//none control edit
- strValidChars = "0123456789.";
- m_ListCtrl.SetColumnValidEditCtrlCharacters(strValidChars,2);//digital only edit
- m_ListCtrl.SetComboColumns(3,TRUE);
- m_ListCtrl.EnableVScroll();
- m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);
- CString str;
- for(int i=0;i<30;i++)
- {
- str.Format("%d",i+1);
- m_ListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE, i,
- str, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED, 0, 0);
- m_ListCtrl.SetItemText(i,1,"张三");
- m_ListCtrl.SetItemText(i,2,"1234");
- m_ListCtrl.SetItemText(i,3,"男");
- }
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTestComboListCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CTestComboListCtrlDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CTestComboListCtrlDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- LRESULT CTestComboListCtrlDlg::OnEndLabelEditVariableCriteria(WPARAM wParam, LPARAM lParam)
- {
- LV_DISPINFO* pDispInfo = (LV_DISPINFO*)lParam;
- // TODO: Add your control notification handler code here
- return 1;
- }
- LRESULT CTestComboListCtrlDlg::PopulateComboList(WPARAM wParam, LPARAM lParam)
- {
- // Get the Combobox window pointer
- CComboBox* pInPlaceCombo = static_cast<CComboBox*> (GetFocus());
- // Get the inplace combbox top left
- CRect obWindowRect;
- pInPlaceCombo->GetWindowRect(&obWindowRect);
- CPoint obInPlaceComboTopLeft(obWindowRect.TopLeft());
- // Get the active list
- // Get the control window rect
- // If the inplace combobox top left is in the rect then
- // The control is the active control
- m_ListCtrl.GetWindowRect(&obWindowRect);
- int iColIndex = (int )wParam;
- CStringList* pComboList = reinterpret_cast<CStringList*>(lParam);
- pComboList->RemoveAll();
- if (obWindowRect.PtInRect(obInPlaceComboTopLeft))
- {
- if(iColIndex==3)
- {
- pComboList->AddTail("男");
- pComboList->AddTail("女");
- }
- }
- return true;
- }
- void CTestComboListCtrlDlg::OnButton1()
- //ADD
- {
- // TODO: Add your control notification handler code here
- UpdateData(TRUE);
- int i =m_ListCtrl.GetItemCount();
- CString str;
- str.Format("%d",i+1);
- m_ListCtrl.InsertItem(LVIF_TEXT|LVIF_STATE, i+1,
- str, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED, 0, 0);
- UpdateData(FALSE);
- }
- void CTestComboListCtrlDlg::OnButton2()
- //DELETE
- {
- // TODO: Add your control notification handler code here
- m_ListCtrl.DeleteItem(m_ListCtrl.GetItemCount()-1);
- }