www.pudn.com > ListCtrl功能扩展.rar > ListCtrlDlg.cpp
// ListCtrlDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "ListCtrl.h"
#include "ListCtrlDlg.h"
#include ".\listctrldlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CListCtrlDlg 对话框
CListCtrlDlg::CListCtrlDlg(CWnd* pParent /*=NULL*/)
: CDialog(CListCtrlDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CListCtrlDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_ListCtrl);
}
BEGIN_MESSAGE_MAP(CListCtrlDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_SET_ITEMS, PopulateComboList)
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
END_MESSAGE_MAP()
// CListCtrlDlg 消息处理程序
BOOL CListCtrlDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 将\“关于...\”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
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);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
//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; // 除非设置了控件的焦点,否则返回 TRUE
}
void CListCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CListCtrlDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CListCtrlDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
LRESULT CListCtrlDlg::PopulateComboList(WPARAM wParam, LPARAM lParam)
{
// Get the Combobox window pointer
CComboBox* pInPlaceCombo = static_cast (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(lParam);
pComboList->RemoveAll();
if (obWindowRect.PtInRect(obInPlaceComboTopLeft))
{
if(iColIndex==3)
{
pComboList->AddTail("男");
pComboList->AddTail("女");
pComboList->AddTail("中性");
}
}
return true;
}
void CListCtrlDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString Name;
CString Age;
CString Sex;
Name = m_ListCtrl.GetItemText(1,1);
Age = m_ListCtrl.GetItemText(1,2);
Sex = m_ListCtrl.GetItemText(1,3);
MessageBox(Sex);
}
void CListCtrlDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
int nIndex = 0;
/* nIndex = m_ListCtrl.GetSelectedCount();
m_ListCtrl.DeleteItem(nIndex);*/
nIndex = m_ListCtrl.GetSelectionMark();
m_ListCtrl.DeleteItem(nIndex);
// m_ListCtrl.DeleteColumn(nIndex);
}