ListCtrlDlg.cpp
上传用户:sunnie
上传日期:2022-07-13
资源大小:4512k
文件大小:9k
- // ListCtrlDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ListCtrl.h"
- #include "ListCtrlDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- // Implementation
- 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 dialog
- CListCtrlDlg::CListCtrlDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CListCtrlDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_nPos = 0;
- }
- void CListCtrlDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_LST_CTRL, m_lstCtrl);
- DDX_Control(pDX, IDC_BTN_INSERTCTRL, m_btnInsertCtrl);
- DDX_Control(pDX, IDC_BTN_NORMAL, m_btnInsertNormal);
- DDX_Control(pDX, IDC_BTN_SHOWBAR, m_btnShowBar);
- DDX_Control(pDX, IDC_BTN_SHOWCHK, m_btnChkBox);
- DDX_Control(pDX, IDC_BTN_EDT, m_btnEdit);
- }
- BEGIN_MESSAGE_MAP(CListCtrlDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDC_BTN_INSERTCTRL, &CListCtrlDlg::OnBnClickedBtnInsertctrl)
- ON_BN_CLICKED(IDC_BTN_NORMAL, &CListCtrlDlg::OnBnClickedBtnNormal)
- ON_WM_SIZE()
- ON_BN_CLICKED(IDC_BTN_SHOWBAR, &CListCtrlDlg::OnBnClickedBtnShowbar)
- ON_BN_CLICKED(IDC_BTN_SHOWCHK, &CListCtrlDlg::OnBnClickedBtnShowchk)
- ON_BN_CLICKED(IDC_BTN_EDT, &CListCtrlDlg::OnBnClickedBtnEdt)
- ON_WM_TIMER()
- END_MESSAGE_MAP()
- // CListCtrlDlg message handlers
- /*hdr
- ** Copyright ShangHai Skoito Lamp Ltd.,China
- **
- ** 函数名: InitLst
- ** 作者: RobateWang
- ** 日期: 05 - 06 - 2010
- **
- ** 描述:
- ** 初始化列表,新建4列,分别为行号,精度条、编辑框、复选框
- **
- ** 变量: 变量名 方向 类型 描述
- **
- **
- ** 返回值:
- ** 如果成功放回真,否则返回假
- **
- ** 备注:
- **
- */
- BOOL
- CListCtrlDlg::InitLst()
- {
- //初始化List
- LVCOLUMN column;
- column.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH /*| LVCF_IMAGE*/;
- column.fmt = LVCFMT_LEFT/* | LVCFMT_BITMAP_ON_RIGHT*/;
- column.pszText = _T("行号");
- column.cx = 80;
- m_lstCtrl.InsertColumn(0, &column);
- column.pszText = _T("进度条");
- column.cx = 150;
- m_lstCtrl.InsertColumn(1, &column);
- column.pszText = _T("编辑框");
- column.cx = 120;
- m_lstCtrl.InsertColumn(5, &column);
- column.pszText = _T("复选框");
- column.cx = 120;
- m_lstCtrl.InsertColumn(6, &column);
- // m_lstCtrl.SetBkColor(RGB(230, 230, 255));
- // m_lstCtrl.SetTextBkColor(RGB(230, 230, 255));
- m_lstCtrl.SetTextColor(RGB(0, 0, 0));
- m_lstCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT);
- //设置行高,用一个空ImageList将行撑高
- CImageList imgLst;
- imgLst.Create(1, 22, TRUE|ILC_COLOR32, 1, 0);
- m_lstCtrl.SetImageList(&imgLst, LVSIL_SMALL);
- return TRUE;
- }
- BOOL CListCtrlDlg::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)
- {
- BOOL bNameValid;
- CString strAboutMenu;
- bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
- ASSERT(bNameValid);
- 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
- InitLst();
- m_btnShowBar.EnableWindow(FALSE);
- m_btnChkBox.EnableWindow(FALSE);
- m_btnEdit.EnableWindow(FALSE);
- return TRUE; // return TRUE unless you set the focus to a control
- }
- BOOL CListCtrlDlg::DestroyWindow()
- {
- m_lstCtrl.ReleaseAll();
- KillTimer(1);
- return CDialog::DestroyWindow();
- }
- void CListCtrlDlg::OnSize(UINT nType, int cx, int cy)
- {
- CDialog::OnSize(nType, cx, cy);
- if (!IsWindow(m_lstCtrl.m_hWnd) || (cx == 0) || (cy == 0))
- {
- return;
- }
- CRect rcWnd;
- CRect rcList;
- CRect rcBtnCtrl;
- CRect rcBtnNormal;
- CRect rcBtnBar;
- CRect rcBtnChk;
- GetClientRect(&rcWnd);
- m_lstCtrl.GetWindowRect(&rcList);
- m_btnInsertCtrl.GetWindowRect(&rcBtnCtrl);
- m_btnInsertNormal.GetWindowRect(&rcBtnNormal);
- m_btnShowBar.GetWindowRect(&rcBtnBar);
- m_btnChkBox.GetWindowRect(&rcBtnChk);
- ScreenToClient(&rcList);
- ScreenToClient(&rcBtnCtrl);
- ScreenToClient(&rcBtnNormal);
- ScreenToClient(&rcBtnBar);
- ScreenToClient(&rcBtnChk);
- int nBottomEdge = 10;
- int nMoveX = rcWnd.right - rcBtnNormal.right - nBottomEdge;
- int nMoveY = rcWnd.bottom - rcList.bottom - nBottomEdge;
- // rcBtnCtrl.right += nMoveX;
- // rcBtnCtrl.left += nMoveX;
- rcBtnCtrl.OffsetRect(nMoveX, nMoveY);
- // rcBtnNormal.right += nMoveX;
- // rcBtnNormal.left += nMoveX;
- rcBtnNormal.OffsetRect(nMoveX, nMoveY);
- rcBtnBar.OffsetRect(nMoveX, nMoveY);
- rcBtnChk.OffsetRect(nMoveX, nMoveY);
- rcList.bottom += nMoveY;
- rcList.right += nMoveX;
- m_btnInsertCtrl.MoveWindow(&rcBtnCtrl);
- m_btnInsertNormal.MoveWindow(&rcBtnNormal);
- m_lstCtrl.MoveWindow(&rcList);
- m_btnShowBar.MoveWindow(&rcBtnBar);
- m_btnChkBox.MoveWindow(&rcBtnChk);
- }
- void CListCtrlDlg::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 CListCtrlDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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();
- }
- }
- BOOL CListCtrlDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
- {
- HD_NOTIFY *pHDNotify = (HD_NOTIFY*)lParam;
- switch(pHDNotify->hdr.code)
- {
- case HDN_ITEMCHANGINGA:
- case HDN_ITEMCHANGINGW:
- case HDN_ENDTRACK:
- {
- m_lstCtrl.MovePosition();
- break;
- }
- default:
- {
- break;
- }
- }
- return CDialog::OnNotify(wParam, lParam, pResult);
- }
- void CListCtrlDlg::OnTimer(UINT_PTR nIDEvent)
- {
- m_lstCtrl.SetBarPosition(m_nPos);
- m_nPos += 2;
- if (m_nPos >= 100)
- {
- m_nPos = 0;
- }
-
- CDialog::OnTimer(nIDEvent);
- }
- // The system calls this function to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CListCtrlDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CListCtrlDlg::OnBnClickedBtnInsertctrl()
- {
- LV_ITEM lvitem;
- CString strTem = "";
- int nRow = m_lstCtrl.GetItemCount();
- //Name
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = nRow;
- lvitem.iSubItem = 0;
- strTem.Format("%s%d%s", "第", nRow + 1, "行");
- lvitem.pszText = strTem.GetBuffer();
- m_lstCtrl.InsertItem(&lvitem);
- //Progress bar, edit box, check box
- m_lstCtrl.InsertItemEx(nRow, SW_SHOWNORMAL);
- m_btnShowBar.EnableWindow(TRUE);
- m_btnChkBox.EnableWindow(TRUE);
- m_btnEdit.EnableWindow(TRUE);
- }
- void CListCtrlDlg::OnBnClickedBtnNormal()
- {
- LV_ITEM lvitem;
- CString strTem = "";
- int nRow = m_lstCtrl.GetItemCount();
- //Name
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = nRow;
- lvitem.iSubItem = 0;
- strTem.Format("%s%d%s", "第", nRow + 1, "行");
- lvitem.pszText = strTem.GetBuffer();
- m_lstCtrl.InsertItem(&lvitem);
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = nRow;
- lvitem.iSubItem = 1;
- lvitem.pszText = _T("普通行");
- m_lstCtrl.SetItem(&lvitem);
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = nRow;
- lvitem.iSubItem = 2;
- lvitem.pszText = _T("普通行");
- m_lstCtrl.SetItem(&lvitem);
- lvitem.mask = LVIF_TEXT;
- lvitem.iItem = nRow;
- lvitem.iSubItem = 3;
- lvitem.pszText = _T("普通行");
- m_lstCtrl.SetItem(&lvitem);
- }
- void CListCtrlDlg::OnBnClickedBtnShowbar()
- {
- // m_lstCtrl.SetBarPosition(5);
- SetTimer(1, 100, NULL);
- }
- void CListCtrlDlg::OnBnClickedBtnShowchk()
- {
- m_lstCtrl.SetChkBox();
- }
- void CListCtrlDlg::OnBnClickedBtnEdt()
- {
- m_lstCtrl.SetEdtBox();
- }