📄 supergridctrl.cpp
字号:
int nItem = GetSelectedItem();
if(nItem!=-1)
{
CTreeItem* pSelItem = GetTreeItem(nItem);
if(pSelItem != NULL)
{
if(OnDeleteItem(pSelItem, nItem))
DeleteItemEx(pSelItem, nItem);
}
}
} break;
case VK_MULTIPLY:
{
int nIndex = GetSelectedItem();
if(nIndex != -1)
{
CWaitCursor wait;
SetRedraw(0);
CTreeItem *pParent = GetTreeItem(nIndex);
int nScroll=0;
if(OnVKMultiply(pParent, nIndex))
{
ExpandAll(pParent, nScroll);
}
SetRedraw(1);
RedrawItems(nIndex, nScroll);
EnsureVisible(nScroll, TRUE);
}
}break;
case VK_ADD:
{
int nIndex = GetSelectedItem();
if(nIndex!=-1)
{
CWaitCursor wait;
CTreeItem *pSelItem = GetTreeItem(nIndex);
int nScrollIndex = 0;
if(OnVKAdd(pSelItem, nIndex))
{
nScrollIndex = Expand(pSelItem, nIndex);
}
CRect rc;
GetItemRect(nIndex, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
EnsureVisible(nScrollIndex, 1);
}
}break;
case VK_SUBTRACT:
{
int nIndex = GetSelectedItem();
if(nIndex!=-1)
{
CWaitCursor wait;
CTreeItem *pSelItem = GetTreeItem(nIndex);
if(OnVkSubTract(pSelItem, nIndex))
{
Collapse(pSelItem);
}
CRect rc;
GetItemRect(nIndex, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
}
}break;
default :break;
}
*pResult = 0;
}*/
BOOL CSuperGridCtrl::HitTestOnSign(CPoint point, LVHITTESTINFO& ht)
{
ht.pt = point;
// Test which subitem was clicked.
SubItemHitTest(&ht);
if(ht.iItem!=-1)
{
//first hittest on checkbox
BOOL bHit = FALSE;
if(GetExtendedStyle() & LVS_EX_CHECKBOXES)
{
if (ht.flags == LVHT_ONITEM && (GetStyle() & LVS_OWNERDRAWFIXED))//isn't this allways ownerdrawfixed :-)
{
CRect rcIcon,rcLabel;
GetItemRect(ht.iItem, rcIcon, LVIR_ICON);//has to be between these two ....right :)
GetItemRect(ht.iItem, rcLabel, LVIR_LABEL);
// check if hit was on a state icon
if (point.x > rcIcon.left && point.x < rcLabel.left)
bHit = TRUE;
}
else if (ht.flags & LVHT_ONITEMSTATEICON)
bHit = TRUE;
}
CTreeItem* pItem = GetTreeItem(ht.iItem);
if(pItem!=NULL)
{
if(bHit)//if checkbox
{
//yes I know..have to maintain to sets of checkstates here...
//one for listview statemask and one for CTreeItem..but its located here so no harm done
SetCheck(ht.iItem,!GetCheck(ht.iItem));
CItemInfo *pInfo = GetData(pItem);
pInfo->SetCheck(!pInfo->GetCheck());
}
//if haschildren and clicked on + or - then expand/collapse
if(ItemHasChildren(pItem))
{
//hittest on the plus/sign "button"
CRect rcBounds;
GetItemRect(ht.iItem, rcBounds, LVIR_BOUNDS);
CRectangle rect(this, NULL, GetIndent(pItem), rcBounds);
BOOL bRedraw=0;//if OnItemExpanding or OnCollapsing returns false, dont redraw
if(rect.HitTest(point))
{
SetRedraw(0);
int nScrollIndex=0;
if(IsCollapsed(pItem))
{
if(OnItemExpanding(pItem, ht.iItem))
{
nScrollIndex = Expand(pItem, ht.iItem);
OnItemExpanded(pItem, ht.iItem);
bRedraw=1;
}
}
else {
if(OnCollapsing(pItem))
{
Collapse(pItem);
OnItemCollapsed(pItem);
bRedraw=1;
}
}
SetRedraw(1);
if(bRedraw)
{
CRect rc;
GetItemRect(ht.iItem, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
EnsureVisible(nScrollIndex, 1);
return 0;
}
}
}//has kids
}//pItem!=NULL
}
return 1;
}
/*void CSuperGridCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
if( GetFocus() != this)
SetFocus();
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
LVHITTESTINFO ht;
ht.pt = pNMListView->ptAction;
SubItemHitTest(&ht);
if(OnItemLButtonDown(ht))
{
BOOL bSelect=1;
bSelect = HitTestOnSign(pNMListView->ptAction, ht);
//normal selection
if(bSelect && ht.iItem !=-1)
{
int nIndex = GetSelectedItem();
if(nIndex!=-1)
{
CTreeItem *pSelItem = GetTreeItem(nIndex);
if (pSelItem != NULL)
{
BOOL bRedraw=0;
if(ItemHasChildren(pSelItem))
{
SetRedraw(0);
int nScrollIndex=0;
if(IsCollapsed(pSelItem))
{
if(OnItemExpanding(pSelItem, nIndex))
{
nScrollIndex = Expand(pSelItem, nIndex);
OnItemExpanded(pSelItem, nIndex);
bRedraw=1;
}
}
else
{
if(OnCollapsing(pSelItem))
{
Collapse(pSelItem);
OnItemCollapsed(pSelItem);
bRedraw=1;
}
}
SetRedraw(1);
if(bRedraw)
{
CRect rc;
GetItemRect(nIndex,rc,LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
EnsureVisible(nScrollIndex,1);
}
}//ItemHasChildren
}//!=NULL
}
}
}
*pResult = 0;
}*/
/*void CSuperGridCtrl::OnBegindrag(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
m_nDragItem = pNMListView->iItem;
CImageList* pDragImage=NULL;
pDragImage = CreateDragImageEx(m_nDragItem);//override this if you want another dragimage or none at all.
if(pDragImage)
{
pDragImage->BeginDrag(0, CPoint(0,0));
pDragImage->DragEnter(this, pNMListView->ptAction);
SetCapture();
m_bIsDragging = TRUE;
}
delete pDragImage;
}
*pResult = 0;
}*/
//Create dragimage : Icon + the itemtext
CImageList *CSuperGridCtrl::CreateDragImageEx(int nItem)
{
CImageList *pList = new CImageList;
//get image index
LV_ITEM lvItem;
lvItem.mask = LVIF_IMAGE;
lvItem.iItem = nItem;
lvItem.iSubItem = 0;
GetItem(&lvItem);
CRect rc;
GetItemRect(nItem, &rc, LVIR_BOUNDS);
CString str;
str = GetItemText(nItem, 0);
CFont *pFont = GetFont();
rc.OffsetRect(-rc.left, -rc.top);
rc.right = GetColumnWidth(0);
pList->Create(rc.Width(), rc.Height(),ILC_COLOR24| ILC_MASK , 1, 1);
CDC *pDC = GetDC();
if(pDC)
{
CDC dc;
dc.CreateCompatibleDC(pDC);
CBitmap bmpMap;
bmpMap.CreateCompatibleBitmap(pDC, rc.Width(), rc.Height());
CBitmap *pOldBmp = dc.SelectObject(&bmpMap);
CFont *pOldFont = dc.SelectObject(pFont);
dc.FillSolidRect(rc, GetSysColor(COLOR_WINDOW));
CImageList *pImgList = GetImageList(LVSIL_SMALL);
if(pImgList)
pImgList->Draw(&dc, lvItem.iImage, CPoint(0,0), ILD_TRANSPARENT);
dc.TextOut(m_cxImage + 4, 0, str);
dc.SelectObject(pOldFont);
dc.SelectObject(pOldBmp);
bmpMap.DeleteObject(); //hpxs 添加的销毁代码
//causes an error if the 1st column is hidden so must check the imagelist
if(pList->m_hImageList != NULL)
pList->Add(&bmpMap, RGB(255,255,255));
else
{
delete pList;
pList=NULL;
}
dc.DeleteDC(); //hpxs 添加的销毁DC代码
ReleaseDC(pDC);
}
return pList;
}
/*void CSuperGridCtrl::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_bIsDragging)
{
KillTimer(1);
if (CWnd::GetCapture() != this)
m_bIsDragging=0;
if(nFlags==MK_RBUTTON || nFlags==MK_MBUTTON)
m_bIsDragging=0;
if(GetKeyState(VK_ESCAPE) < 0)
m_bIsDragging=0;
if(!m_bIsDragging)//why not put this in a funtion :)
{
SetItemState (m_nDragTarget, 0, LVIS_DROPHILITED);
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
InvalidateRect(NULL);
UpdateWindow();
}
else
{
CPoint ptList(point);
MapWindowPoints(this, &ptList, 1);
CImageList::DragMove(ptList);
UINT uHitTest = LVHT_ONITEM;
m_nDragTarget = HitTest(ptList, &uHitTest);
// try turn off hilight for previous DROPHILITED state
int nPrev = GetNextItem(-1,LVNI_DROPHILITED);
if(nPrev != m_nDragTarget)//prevents flicker
SetItemState(nPrev, 0, LVIS_DROPHILITED);
CRect rect;
GetClientRect (rect);
int cy = rect.Height();
if(m_nDragTarget!=-1)
{
SetItemState(m_nDragTarget, LVIS_DROPHILITED, LVIS_DROPHILITED);
CTreeItem* pTarget = GetTreeItem(m_nDragTarget);
if ((point.y >= 0 && point.y <= m_cyImage) || (point.y >= cy - m_cyImage && point.y <= cy) ||
( pTarget!=NULL && ItemHasChildren(pTarget) && IsCollapsed(pTarget)))
{
SetTimer(1,300,NULL);
}
}
}
}
CListCtrl::OnMouseMove(nFlags, point);
}*/
/*void CSuperGridCtrl::OnTimer(UINT nIDEvent)
{
CListCtrl::OnTimer(nIDEvent);
if(nIDEvent==1)
{
if(CWnd::GetCapture()!=this)
{
SetItemState(m_nDragTarget, 0, LVIS_DROPHILITED);
m_bIsDragging=0;
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
InvalidateRect(NULL);
UpdateWindow();
KillTimer(1);
return;
}
SetTimer(1,300,NULL);//reset timer
DWORD dwPos = ::GetMessagePos();
CPoint ptList(LOWORD(dwPos),HIWORD(dwPos));
ScreenToClient(&ptList);
CRect rect;
GetClientRect(rect);
int cy = rect.Height();
//
// perform autoscroll if the cursor is near the top or bottom.
//
if (ptList.y >= 0 && ptList.y <= m_cyImage)
{
int n = GetTopIndex();
CImageList::DragShowNolock(0);
SendMessage(WM_VSCROLL, MAKEWPARAM(SB_LINEUP, 0), NULL);
CImageList::DragShowNolock(1);
if (GetTopIndex()== n)
KillTimer (1);
else {
CImageList::DragShowNolock(0);
CImageList::DragMove(ptList);
CImageList::DragShowNolock(1);
return;
}
}
else if (ptList.y >= cy - m_cyImage && ptList.y <= cy)
{
int n = GetTopIndex();
CImageList::DragShowNolock(0);
SendMessage(WM_VSCROLL, MAKEWPARAM(SB_LINEDOWN, 0), NULL);
CImageList::DragShowNolock(1);
if (GetTopIndex()== n)
KillTimer (1);
else {
CImageList::DragShowNolock(0);
CImageList::DragMove(ptList);
CImageList::DragShowNolock(1);
return;
}
}
//Hover test
CImageList::DragMove(ptList);
UINT uHitTest = LVHT_ONITEM;
m_nDragTarget = HitTest(ptList, &uHitTest);
if(m_nDragTarget!=-1)
{
//if the target has children
//expand them
CTreeItem* pTarget=GetTreeItem(m_nDragTarget);
if(pTarget != NULL && ItemHasChildren(pTarget) && IsCollapsed(pTarget) && (m_nDragItem!=-1))
{
CImageList::DragShowNolock(0);
CTreeItem* pSource = GetTreeItem(m_nDragItem);
SetRedraw(0);
int nScrollIndex=0;
if(ItemHasChildren(pTarget) && IsCollapsed(pTarget))
{
if(OnItemExpanding(pTarget, m_nDragTarget))
{
nScrollIndex = Expand(pTarget, m_nDragTarget);
OnItemExpanded(pTarget, m_nDragTarget);
}
}
m_nDragItem = NodeToIndex(pSource);
SetRedraw(1);
EnsureVisible(nScrollIndex, 1);
InvalidateRect(NULL);
UpdateWindow();
CImageList::DragShowNolock(1);
KillTimer(1);
return;
}
}
KillTimer(1);
}
}*/
void CSuperGridCtrl::OnLButtonUp(UINT nFlags, CPoint point)
{
if(m_bIsDragging == TRUE)
{
KillTimer(1);
CImageList::DragLeave(this);
CImageList::EndDrag();
ReleaseCapture();
m_bIsDragging = FALSE;
SetItemState(m_nDragTarget, 0, LVIS_DROPHILITED);
if((m_nDragTarget != -1) && (m_nDragTarget != m_nDragItem) && (m_nDragItem!=-1))//no drop on me self
{
CTreeItem* pSource = GetTreeItem(m_nDragItem);
CTreeItem* pTarget = GetTreeItem(m_nDragTarget);
if(IsRoot(pSource))
return;
CTreeItem* pParent = GetParentItem(pSource);
if(pParent==pTarget) //can't drag child to parent
return;
if(!IsChildOf(pSource, pTarget))//can't drag parent to child
{
CWaitCursor wait;
SetRedraw(0);
if(DoDragDrop(pTarget, pSource))
{
UINT uflag = LVIS_SELECTED | LVIS_FOCUSED;
SetItemState(m_nDragTarget, uflag, uflag);
m_nDragItem=-1;
//delete source
int nIndex = NodeToIndex(pSource);
DeleteItem(nIndex);
HideChildren(pSource, TRUE, nIndex);
Delete(pSource);
InternaleUpdateTree();
SetRedraw(1);
InvalidateRect(NULL);
UpdateWindow();
}else
SetRedraw(1);
}
}
}
else
CListCtrl::OnLButtonUp(nFlags, point);
}
//used with the drag/drop operation
void CSuperGridCtrl::CopyChildren(CTreeItem* pDest, CTreeItem* pSrc)
{
if (ItemHasChildren(pSrc))
{
POSITION pos = pSrc->m_listChild.GetHeadPosition();
while (pos != NULL)
{
CTreeItem* pItem = (CTreeItem *)pSrc->m_listChild.GetNext(pos);
CItemInfo* lp = CopyData(GetData(pItem));
CTreeItem* pNewItem = InsertItem(pDest, lp);
CopyChildren(pNewItem, pItem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -