📄 supergridctrl.cpp
字号:
return 1;
}
*/
void CSuperGridCtrl::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
return;
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
int nItem = HitTest(pNMListView->ptAction);
if(nItem !=-1)
{
CTreeItem *pSelItem = reinterpret_cast<CTreeItem*>(GetItemData(nItem));
if (pSelItem != NULL)
{
if(ItemHasChildren(pSelItem))
{
SetRedraw(0);
int nScrollIndex=0;
if(IsCollapsed(pSelItem))
nScrollIndex = Expand(pSelItem, nItem);
else
Collapse(pSelItem);
SetRedraw(1);
CRect rc;
GetItemRect(nItem,rc,LVIR_BOUNDS);
InvalidateRect(rc);
//UpdateWindow();
}
}
}
*pResult = 0;
}
//default implementation for setting icons
int CSuperGridCtrl::GetIcon(const CTreeItem* pItem)
{
return 0;//just take the first item in CImageList ..what ever that is
}
void CSuperGridCtrl::OnLButtonDown(UINT nFlags, CPoint point)
{
int nItem = HitTest(point);
if(nItem!=-1)
{
CTreeItem* pItem = reinterpret_cast<CTreeItem*>(GetItemData(nItem));
if(pItem!=NULL)
{
//if haschildren and clicked on + or - then expand/collapse
if(ItemHasChildren(pItem))
{
//hittest on the plus/sign "button"
//see the DrawTreeItem for setting up the plus/sign "button"
CRect rcBounds;
GetItemRect(nItem, rcBounds, LVIR_BOUNDS);
int nHalfImage = (m_cxImage>>1);
SIZE right_bottom = {(m_cxImage>>1)+2+1, (m_cyImage>>1)+2+1};//once again ANDY: the '+ 1' is to center the [+] or [-]
int left = rcBounds.left + GetIndent(pItem) * m_cxImage - (m_cxImage>>1);
int top = (rcBounds.top + nHalfImage) - (right_bottom.cy >> 1);
POINT left_top = {left - (right_bottom.cx >> 1), top};
CRect rc(left_top, right_bottom);
if(!rc.PtInRect(point))
{
CListCtrl::OnLButtonDown(nFlags, point);
if (nItem == 0 && !IsCollapsed(pItem)) //Root
{
//GetOwner()->SendMessage(WM_FOLDER_SELECTED, 0,0);
return;
}
}
{
SetRedraw(0);
int nScrollIndex=0;
if(IsCollapsed(pItem))
nScrollIndex = Expand(pItem, nItem);
else
Collapse(pItem);
SetRedraw(1);
CRect rc;
GetItemRect(nItem, rc, LVIR_BOUNDS);
InvalidateRect(rc);
//UpdateWindow();
//EnsureVisible(nScrollIndex,1);
//GetOwner()->SendMessage(WM_FOLDER_SELECTED, 0,0);
return;
}
}
}
CListCtrl::OnLButtonDown(nFlags, point);
//OnControlLButtonDown(nFlags, point, ht);
//update row anyway for selection bar
/*CRect rc;
GetItemRect(nItem, rc, LVIR_BOUNDS);
InvalidateRect(rc);
UpdateWindow();
*/
//Update owner
//GetOwner()->SendMessage(WM_FOLDER_SELECTED, 0,0);
}
}
//some day I should base the drag/drop on OLE instead
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);
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);
pList->Add(&bmpMap, RGB(255,255,255));
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 = reinterpret_cast<CTreeItem*>(GetItemData(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=reinterpret_cast<CTreeItem*>(GetItemData(m_nDragTarget));
if(pTarget != NULL && ItemHasChildren(pTarget) && IsCollapsed(pTarget) && (m_nDragItem!=-1))
{
CImageList::DragShowNolock(0);
CTreeItem* pSource = reinterpret_cast<CTreeItem*>(GetItemData(m_nDragItem));
SetRedraw(0);
int nScrollIndex=0;
if(ItemHasChildren(pTarget) && IsCollapsed(pTarget))
nScrollIndex = Expand(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)
{
/* No Drag and drop
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 = reinterpret_cast<CTreeItem*>(GetItemData(m_nDragItem));
CTreeItem* pTarget = reinterpret_cast<CTreeItem*>(GetItemData(m_nDragTarget));
if (pSource == m_pRoot)//can't drag root in my application, sorry !!
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);
int nIndex1 = NodeToIndex(pSource);
if(DoDragDrop(pTarget, m_nDragTarget, pSource, m_nDragItem))
{
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)
{
return;
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);
}
}
}
//hmmm
BOOL CSuperGridCtrl::DoDragDrop(CTreeItem* pTarget, int nDragTarget, CTreeItem* pSource, int nDragItem)
{
return 0; // No DragDrop
if(pTarget==NULL)
return 0;
BOOL bUpdate=FALSE;
if(!IsCollapsed(pTarget))
bUpdate=TRUE; //children are expanded, want to see update right away
//make a copy of the source data
CItemInfo* lp = CopyData(GetData(pSource));
//create new node with the source data and make pTarget the parent
CTreeItem* pNewParent = InsertItem(pTarget, lp, bUpdate);
//if the source has children copy all source data and make the newly create item the parent
if(ItemHasChildren(pSource))
CopyChildren(pNewParent, pSource);
return 1;
}
void CSuperGridCtrl::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
//its not meself
if( GetFocus() != this)
SetFocus();
CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);
}
CSuperGridCtrl::CTreeItem* CSuperGridCtrl::GetRootItem()
{
return m_pRoot;
}
void CSuperGridCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
//its not meself
if( GetFocus() != this)
SetFocus();
CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);
}
BOOL CSuperGridCtrl::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(GetFocus()==this)
{
switch( pMsg->wParam )
{
case VK_LEFT:
{
// Decrement the order number.
m_CurSubItem--;
if(m_CurSubItem < 0)
m_CurSubItem = 0;
else{
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
// Make the column visible.
// We have to take into account that the header may be reordered.
MakeColumnVisible( Header_OrderToIndex( pHeader->m_hWnd, m_CurSubItem));
// Invalidate the item.
int iItem = GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
if( iItem != -1 )
{
CRect rcBounds;
GetItemRect(iItem, rcBounds, LVIR_BOUNDS);
InvalidateRect(&rcBounds);
UpdateWindow();
}
}
}
return TRUE;
case VK_RIGHT:
{
// Increment the order number.
m_CurSubItem++;
CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
int nColumnCount = pHeader->GetItemCount();
// Don't go beyond the last column.
if( m_CurSubItem > nColumnCount -1 )
m_CurSubItem = nColumnCount-1;
else
{
MakeColumnVisible(Header_OrderToIndex( pHeader->m_hWnd, m_CurSubItem));
int iItem = GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
// Invalidate the item.
if( iItem != -1 )
{
CRect rcBounds;
GetItemRect(iItem, rcBounds, LVIR_BOUNDS);
InvalidateRect(&rcBounds);
UpdateWindow();
}
}
}
return TRUE;
case VK_RETURN://edit itemdata
{
/*
int iItem = GetNextItem( -1, LVNI_ALL | LVNI_SELECTED);
if( m_CurSubItem != -1 && iItem != -1)
{
CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
int iSubItem = Header_OrderToIndex(pHeader->m_hWnd, m_CurSubItem);
if(iSubItem==0)//that's just me saying all nodes in col 0 are edit-controls, you may modify this
{
CRect rcItem;
GetItemRect(iItem, rcItem, LVIR_LABEL);
DWORD dwStyle = WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL|ES_LEFT;
CEdit *pEdit = new CListEditCtrl(iItem, iSubItem, GetItemText(iItem, iSubItem));
pEdit->Create(dwStyle, rcItem, this, 0x1233);
}
else
EditLabelEx(iItem, iSubItem);
return 1;
}*/
}
break;
default:
break;
}
}
}
return CListCtrl::PreTranslateMessage(pMsg);
}
/*
#define IDC_EDITCTRL 0x1234
CEdit* CSuperGridCtrl::EditLabelEx(int nItem, int nCol)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -