1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| class CSubeditListView : public CListView
1) Vars:
public:
CSubEdit m_editWnd;
int m_item;
int m_subitem;
2) Init vars:
CSubeditListView::CSubeditListView()
{
m_subitem = 0;
}
3) Detect subitem leftclick
void CSubeditListView::OnLButtonDown(UINT nFlags, CPoint point)
{
LVHITTESTINFO lvhit;
lvhit.pt = point;
int item = GetListCtrl().SubItemHitTest(&lvhit);
//if (over a subitem)
if (item != -1 && lvhit.iSubItem && (lvhit.flags & LVHT_ONITEM ))
{
//Mouse click outside the editcontrol in an allready editing cell cancels editing
if (m_subitem == lvhit.iSubItem && item == m_item)
{
CListView::OnLButtonDown(nFlags, point);
}
else
{
CListView::OnLButtonDown(nFlags, point);
m_subitem = lvhit.iSubItem;
m_item = item;
GetListCtrl().EditLabel(item);
}
}
else
CListView::OnLButtonDown(nFlags, point);
}
4) Begin editing
void CSubeditListView::OnBeginLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
//if (subitem selected for editing)
if (m_subitem)
{
ASSERT(m_item == pDispInfo->item.iItem);
CRect subrect;
GetListCtrl().GetSubItemRect( pDispInfo->item.iItem, m_subitem, LVIR_BOUNDS , subrect );
//Get edit control and subclass
HWND hWnd=(HWND)SendMessage(LVM_GETEDITCONTROL);
ASSERT(hWnd!=NULL);
VERIFY(m_editWnd.SubclassWindow(hWnd));
//Move edit control text 1 pixel to the right of org label, as Windows does it...
m_editWnd.m_x=subrect.left + 6;
m_editWnd.SetWindowText(GetListCtrl().GetItemText(pDispInfo->item.iItem,m_subitem));
//Hide subitem text so it don't show if we delete some text in the editcontrol.
//OnPaint handles other issues also regarding this.
CRect rect;
GetListCtrl().GetSubItemRect(pDispInfo->item.iItem,m_subitem,LVIR_LABEL ,rect);
CClientDC dc(this);
dc.FillRect(rect,&CBrush(::GetSysColor(COLOR_WINDOW)));
}
//return: editing permitted
*pResult = 0;
}
5) Some OnPaint tweaking
void CSubeditListView::OnPaint()
{
//if (subitem editing)
if (m_subitem)
{
CRect rect;
CRect editrect;
GetListCtrl().GetSubItemRect(m_item,m_subitem,LVIR_LABEL ,rect);
m_editWnd.GetWindowRect(editrect);
ScreenToClient(editrect);
//Prevent redraw of subitem label (beneath editcontrol)
//If we didn't do this and deleted some text in the edit control, the subitems original label would show.
if (editrect.right < rect.right)
{
rect.left = editrect.right ;
ValidateRect(rect);
}
//Prevent redraw of leftmost item, since it would be overwritten and filled with background colour.
GetListCtrl().GetItemRect(m_item,rect,LVIR_LABEL );
ValidateRect(rect);
//Prevent redraw of leftmost items icon.
GetListCtrl().GetItemRect(m_item,rect,LVIR_ICON );
ValidateRect(rect);
//NB: Leftmost label, icon and label beneath editcontrol will not be redrawn while editing!
//This is a "bug", but it's unlikely to happend unless you move the listview out of view and back, while editing.
//And who would do such a thing:-)
}
CListView::OnPaint();
}
6) End editing
void CSubeditListView::OnEndLabelEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
LV_ITEM *plvItem = &plvDispInfo->item;
//if (end of sub-editing) do cleanup
if (m_subitem)
{
//plvItem->pszText is NULL if editing canceled
if (plvItem->pszText != NULL )
GetListCtrl().SetItemText(plvItem->iItem,m_subitem, plvItem->pszText);
VERIFY(m_editWnd.UnsubclassWindow()!=NULL);
m_subitem = 0;
//allways revert to org label (Windows thinks we are editing the leftmost item)
*pResult = 0;
}
else
//return: update label on leftmost item
*pResult = 1;
}
7) Handle resizing
void CSubeditListView::OnSize(UINT nType, int cx, int cy)
{
//stop editing if resizing
if( GetFocus() != this ) SetFocus();
CListView::OnSize(nType, cx, cy);
}
class CSubEdit : public CEdit
1) Vars
public:
int m_x;
2) Position editcontrol:
void CSubEdit::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
lpwndpos->x=m_x;
CEdit::OnWindowPosChanging(lpwndpos);
} |
Partager