mfc 可编辑 list control
维护到一个古老的gm工具的时候 需要这个功能 在网上找到一份很好用的代码 贴到这里 再次感谢那位同僚
#pragma once //#include "OrangeMessage.h" // COrangeEdit class COrangeEdit : public CEdit { DECLARE_DYNAMIC(COrangeEdit) public: COrangeEdit(); virtual ~COrangeEdit(); protected: DECLARE_MESSAGE_MAP() virtual void PreSubclassWindow(); public: afx_msg void OnKillFocus(CWnd* pNewWnd); };
// OrangeEdit.cpp : 实现文件 // #include "stdafx.h" //#include "ListDemo.h" #include "OrangeEdit.h" #include "WindowManager.h" // COrangeEdit IMPLEMENT_DYNAMIC(COrangeEdit, CEdit) COrangeEdit::COrangeEdit() { } COrangeEdit::~COrangeEdit() { } BEGIN_MESSAGE_MAP(COrangeEdit, CEdit) ON_WM_KILLFOCUS() END_MESSAGE_MAP() // COrangeEdit 消息处理程序 void COrangeEdit::PreSubclassWindow() { // TODO: 在此添加专用代码和/或调用基类 CEdit::PreSubclassWindow(); } void COrangeEdit::OnKillFocus(CWnd* pNewWnd) { CEdit::OnKillFocus(pNewWnd); // TODO: 在此处添加消息处理程序代码 CWnd* pParent = this->GetParent(); ::SendMessage(pParent->GetSafeHwnd(),ORANGE_LVN_ENDEDIT,0,0); }
#pragma once #include "orangeedit.h" //#include "OrangeMessage.h" // COrangeListCtrl class COrangeListCtrl : public CListCtrl { DECLARE_DYNAMIC(COrangeListCtrl) public: COrangeListCtrl(); virtual ~COrangeListCtrl(); protected: DECLARE_MESSAGE_MAP() public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); protected: virtual void PreSubclassWindow(); afx_msg LRESULT OnEndEdit(WPARAM wParam,LPARAM lParam); void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct); afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct); private: int m_nItem;//主项 int m_nSubItem;//子项 COrangeEdit m_Edit_ctlItem; CFont m_FontItem; int m_nRowHeight; public: void SetRowHeigt(int nHeight); };
// OrangeListCtrl.cpp : 实现文件 // #include "stdafx.h" #include "WindowManager.h" #include "OrangeListCtrl.h" // COrangeListCtrl IMPLEMENT_DYNAMIC(COrangeListCtrl, CListCtrl) COrangeListCtrl::COrangeListCtrl() { m_nRowHeight=0; } COrangeListCtrl::~COrangeListCtrl() { } BEGIN_MESSAGE_MAP(COrangeListCtrl, CListCtrl) ON_WM_LBUTTONDOWN() ON_MESSAGE(ORANGE_LVN_ENDEDIT, OnEndEdit) ON_WM_MEASUREITEM() ON_WM_MEASUREITEM_REFLECT() END_MESSAGE_MAP() // COrangeListCtrl 消息处理程序 void COrangeListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: 添加您的代码以绘制指定项 CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC); LVITEM lvi = {0}; lvi.mask = LVIF_STATE; lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ; lvi.iItem = lpDrawItemStruct->itemID; BOOL bGet = GetItem(&lvi); BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED) && ((GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS)))); CRect rcBack = lpDrawItemStruct->rcItem; if( bHighlight ) //高亮显示 { pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT)); pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT)); pDC->FillRect(rcBack, &CBrush(::GetSysColor(COLOR_HIGHLIGHT))); } else { pDC->SetTextColor(::GetSysColor(COLOR_WINDOWTEXT)); pDC->FillRect(rcBack, &CBrush(::GetSysColor(COLOR_WINDOW))); } //绘制文本 CString strText=L""; CRect rcItem; if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE) { for (int i = 0; i < GetHeaderCtrl()-> GetItemCount();i++) { if ( !GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem )) continue; strText = GetItemText( lpDrawItemStruct->itemID, i ); pDC->DrawText(strText,strText.GetLength(), &rcItem, DT_LEFT|DT_VCENTER|DT_SINGLELINE|DT_BOTTOM); } } } void COrangeListCtrl::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 CListCtrl::OnLButtonDown(nFlags, point); CRect rcCtrl; LVHITTESTINFO lvhti; lvhti.pt = point; m_nItem =SubItemHitTest(&lvhti); if (m_nItem == -1) return; m_nSubItem = lvhti.iSubItem; GetSubItemRect(m_nItem,m_nSubItem,LVIR_LABEL,rcCtrl); if (m_Edit_ctlItem.m_hWnd == NULL) { m_Edit_ctlItem.Create(ES_AUTOHSCROLL|WS_CHILD|ES_LEFT|ES_WANTRETURN|WS_BORDER,CRect(0,0,0,0),this,99); m_Edit_ctlItem.ShowWindow(SW_HIDE); m_FontItem.CreateStockObject(DEFAULT_GUI_FONT); m_Edit_ctlItem.SetFont(&m_FontItem); m_FontItem.DeleteObject(); } CString strItem=GetItemText(m_nItem,m_nSubItem); m_Edit_ctlItem.MoveWindow(&rcCtrl); m_Edit_ctlItem.ShowWindow(SW_SHOW); m_Edit_ctlItem.SetWindowText(strItem); m_Edit_ctlItem.SetFocus(); m_Edit_ctlItem.SetSel(-1); } void COrangeListCtrl::PreSubclassWindow() { // TODO: 在此添加专用代码和/或调用基类 ModifyStyle(0,LVS_OWNERDRAWFIXED); CListCtrl::PreSubclassWindow(); } LRESULT COrangeListCtrl::OnEndEdit(WPARAM wParam,LPARAM lParam) { CString strText; m_Edit_ctlItem.GetWindowText(strText); SetItemText(m_nItem,m_nSubItem,strText); m_Edit_ctlItem.ShowWindow(SW_HIDE); return 0; } void COrangeListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) { if (m_nRowHeight>0) { lpMeasureItemStruct->itemHeight = m_nRowHeight; } } void COrangeListCtrl::SetRowHeigt(int nHeight) { m_nRowHeight = nHeight; CRect rcWin; GetWindowRect(&rcWin); WINDOWPOS wp; wp.hwnd = m_hWnd; wp.cx = rcWin.Width(); wp.cy = rcWin.Height(); wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER; SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp); } void COrangeListCtrl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) { // TODO: 在此添加消息处理程序代码和/或调用默认值 CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct); }