📄 mysupergrid.cpp
字号:
CItemInfo* lp = GetData(pParent);
CString strData = lp->GetItemText();
strData+='\t';
nLineY+=cy;
for(int nCol=0; nCol < lp->GetItemCount(); nCol++)
{
CString str = lp->GetSubItem(nCol);
strData+=str;
strData+='\t';
}
#ifdef _WIN32_WCE // Unsupported Win32 API call
pDC->ExtTextOut(10,nLineY,0, NULL, strData,strData.GetLength(), 0);
#else
pDC->TabbedTextOut(10,nLineY,strData,strData.GetLength(), 0, NULL, 0);
#endif
nLineY+=cy;
//GetNext ....loop through all children
for(;;)
{
CTreeItem *pCur = GetNext(pParent,pItem, TRUE, FALSE/*regardless of the item are hidden or not*/);
if(!IsChildOf(pParent, pCur))
break;
else
if(pCur==pItem)
break;
CItemInfo* lp = GetData(pCur);
CString strData = lp->GetItemText();
strData+='\t';
for(int nCol=0; nCol < lp->GetItemCount(); nCol++)
{
CString str = lp->GetSubItem(nCol);
strData+=str;
strData+='\t';
}
#ifdef _WIN32_WCE // Unsupported Win32 API call
pDC->ExtTextOut(10,nLineY,0, NULL, strData,strData.GetLength(), 0);
#else
pDC->TabbedTextOut(10,nLineY,strData,strData.GetLength(), 0, NULL, 0);
#endif
nLineY+=cy;
pItem=pCur;
}
}
}
}
void CMySuperGrid::HowToLoopThroughAllItems_that_has_a_checkmark_and_print_them_or_what_ever(CDC *pDC)
{
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
int cy = tm.tmHeight + tm.tmExternalLeading;
int nLineY=0;
nLineY+=cy;
CString strData = _T("GIVE ME A BREAK YOU KNOW PRINT PREVIEW IS NOT THE ISSUE HERE");
#ifdef _WIN32_WCE // Unsupported Win32 API call
pDC->ExtTextOut(10, nLineY, 0, NULL, strData, strData.GetLength(), 0);
#else
pDC->TextOut(10,nLineY,strData);
#endif
nLineY+=cy;
nLineY+=cy;
if(!GetItemCount())
return;
int nIndex=0;//has to be the root
if(nIndex!=-1)
{
//do a GetHeadPosition
POSITION pos = GetRootHeadPosition();
while(pos != NULL)
{
CTreeItem *pParent = (CTreeItem*)GetNextRoot(pos);
CTreeItem *pItem = pParent;
CItemInfo* lp = GetData(pParent);
if(lp->GetCheck())//very hard :)=
{
CString strData=lp->GetItemText();
strData+='\t';
nLineY+=cy;
for(int nCol=0; nCol < lp->GetItemCount(); nCol++)
{
CString str = lp->GetSubItem(nCol);
strData+=str;
strData+='\t';
}
#ifdef _WIN32_WCE // Unsupported Win32 API call
pDC->ExtTextOut(10, nLineY, 0, NULL, strData, strData.GetLength(), 0);
#else
pDC->TabbedTextOut(10,nLineY,strData,strData.GetLength(), 0, NULL, 0);
#endif
nLineY+=cy;
}
//GetNext ....loop through all children
for(;;)
{
CTreeItem *pCur = GetNext(pParent, pItem, TRUE, FALSE/*regardless of the item are hidden or not*/);
if(!IsChildOf(pParent, pCur))
break;
else
if(pCur==pItem)
break;
CItemInfo* lp = GetData(pCur);
if(lp->GetCheck())
{
CString strData = lp->GetItemText();
strData+='\t';
for(int nCol=0; nCol < lp->GetItemCount(); nCol++)
{
CString str = lp->GetSubItem(nCol);
strData+=str;
strData+='\t';
}
#ifdef _WIN32_WCE // Unsupported Win32 API call
pDC->ExtTextOut(10, nLineY, 0, NULL, strData, strData.GetLength(), 0);
#else
pDC->TabbedTextOut(10,nLineY,strData,strData.GetLength(), 0, NULL, 0);
#endif
nLineY+=cy;
}
pItem=pCur;
}
}//while
}//if
}
//HOWTO: Search nodeptr that have a specific item and subitems also shows you how to select the node and delete it
void CMySuperGrid::HowToSearch_I_am_using_hardcoded_values_here_cause_I_am_tired_now(void)
{
//one Item and two Subitems
CTreeItem *pNode = Search(__T("Hello World"),_T("Happy"),_T("Programming"),NULL);
if(pNode!=NULL)
{
CItemInfo *pInfo = GetData(pNode);
AfxMessageBox(_T("Found Item ") + pInfo->GetItemText());
}
else AfxMessageBox(_T("not found"));
//one Item and one Subitem
CTreeItem *pNode1 = Search(_T("Mission: Impossible"),_T("Allan Nielsen"),NULL);
if(pNode1!=NULL)
{
CItemInfo *pInfo = GetData(pNode1);
AfxMessageBox(_T("Found Item ") + pInfo->GetItemText());
}
else AfxMessageBox(_T("not found"));
//one Item and one Subitem
CTreeItem *pNode2 = Search(_T("Training Agent"),_T("Mr. Bean"),NULL);
if(pNode2!=NULL)
{
CItemInfo *pInfo = GetData(pNode2);
AfxMessageBox(_T("Found Item") + pInfo->GetItemText());
}
else AfxMessageBox(_T("not found"));
CTreeItem *pNode3 = Search(_T("BC"),NULL);
if(pNode3!=NULL)
{
CItemInfo *pInfo = GetData(pNode3);
AfxMessageBox(_T("Found Item ") + pInfo->GetItemText());
int nIndex = SelectNode(pNode3);
if(nIndex!=-1)
if(AfxMessageBox(_T("Do you want to delete it"),MB_OKCANCEL)==IDOK)
DeleteItemEx(pNode3, nIndex);
}
else AfxMessageBox(_T("not found"));
CTreeItem *pItem = NULL;
POSITION pos = GetRootHeadPosition();
while(pos != NULL)
{
CTreeItem * pRoot = (CTreeItem*)GetNextRoot(pos);
pItem = SearchEx(pRoot, _T("AB"));
if(pItem!=NULL)
{
CItemInfo *pInfo = GetData(pItem);
AfxMessageBox(_T("Found Item ") + pInfo->GetItemText());
int nIndex = SelectNode(pItem);
if(nIndex!=-1)
if(AfxMessageBox(_T("Do you want to delete it"),MB_OKCANCEL)==IDOK)
DeleteItemEx(pItem, nIndex);
}
}
if( pItem == NULL )
AfxMessageBox(_T("not found"));
}
//this is just one way to search items...strItem must match and then all subitems must be
//a match before returning the node
//the search function here search all nodes regardless if collapsed or expanded
CMySuperGrid::CTreeItem* CMySuperGrid::Search(CString strItem,...)
{
if(!GetItemCount())
return NULL;
va_list args;
va_start(args, strItem);
int nCount=0;
for(;;)
{
LPCTSTR lpsz = va_arg(args, LPCTSTR);
if(lpsz==NULL)
break;
nCount++;
}
POSITION pos = GetRootHeadPosition();
while(pos != NULL)
{
CTreeItem *pParent = (CTreeItem*)GetNextRoot(pos);
CTreeItem *pItem = pParent;
CItemInfo* lp = GetData(pParent);
CString strData = lp->GetItemText();
if(strData==strItem)//must be a match before going further...suit you self
{
va_start(args, strItem);
int nResult=0;
for(int i=0; i<nCount;i++)
{
LPCTSTR lpsz = va_arg(args, LPCTSTR);
for(int nCol=0; nCol < lp->GetItemCount(); nCol++)
{
CString str = lp->GetSubItem(nCol);
if(!str.CompareNoCase(lpsz))
{
nResult++;
break;
}
}
}
va_end(args);
if(nCount==nResult)//all args was found return node
return pParent;
}
//GetNext ....loop through all children
for(;;)
{
CTreeItem *pCur = GetNext(pParent, pItem, TRUE, FALSE/*regardless of the item are hidden or not, set to TRUE if only visible items must be included in the search*/);
if(!IsChildOf(pParent, pCur))
break;
else
if(pCur==pItem)
break;
CItemInfo* lp = GetData(pCur);
CString strData = lp->GetItemText();
if(strData==strItem)//must be a match before going further
{
va_start(args, strItem);
int nResult=0;
for(int i=0; i<nCount;i++)
{
LPCTSTR lpsz = va_arg(args, LPCTSTR);
for(int nCol=0; nCol < lp->GetItemCount(); nCol++)
{
CString str = lp->GetSubItem(nCol);
if(!str.CompareNoCase(lpsz))
{
nResult++;
break;
}
}
}
va_end(args);
if(nCount==nResult)//all args was found return node
return pCur;
}
pItem=pCur;//next;
}
}
return NULL;
}
void CMySuperGrid::SortData()
{
int nIndex = GetNextItem(-1, LVNI_ALL | LVNI_SELECTED);
if(nIndex==-1)
return;
CTreeItem *pItem = reinterpret_cast<CTreeItem*>(GetItemData(nIndex));
if(AfxMessageBox(_T("Sort all children(Yes)\nor just sort rootitems(No)"),MB_YESNO)==IDYES)
Sort(pItem, TRUE);
else
Sort(pItem, FALSE);
//do a simple refresh thing
if(ItemHasChildren(pItem))
{
SetRedraw(0);
Collapse(pItem);
Expand(pItem, nIndex);
SetRedraw(1);
}
}
//another search thing
CMySuperGrid::CTreeItem* CMySuperGrid::SearchEx(CTreeItem *pStartPosition, CString strItem)
{
CItemInfo* lp = GetData(pStartPosition);
//if(lp->GetCheck()) another condition here maybe
CString strData = lp->GetItemText();
if(strData==strItem)
{
return pStartPosition;
}
const int nChildren = NumChildren(pStartPosition);
if (nChildren > 0)
{
POSITION pos = GetHeadPosition(pStartPosition);
while (pos)
{
CTreeItem *pChild = GetNextChild(pStartPosition, pos);
CItemInfo* lp = GetData(pChild);
CString strData = lp->GetItemText();
if(strData==strItem)
{
return pChild;
}
}
}
POSITION pos = GetHeadPosition(pStartPosition);
while (pos)
{
CTreeItem *pChild = GetNextChild(pStartPosition, pos);
CItemInfo* lp = GetData(pChild);
CString strData = lp->GetItemText();
if(strData==strItem)
{
return pChild;
}
pChild = SearchEx(pChild, strItem);
if(pChild!=NULL)
return pChild;
}
return NULL;
}
BOOL CMySuperGrid::OnDeleteItem(CTreeItem* pItem, int nIndex)
{
return 1;
}
BOOL CMySuperGrid::OnItemExpanding(CTreeItem *pItem, int iItem)
{
return 1;
}
BOOL CMySuperGrid::OnItemExpanded(CTreeItem* pItem, int iItem)
{
return 1;
}
BOOL CMySuperGrid::OnCollapsing(CTreeItem *pItem)
{
return 1;
}
BOOL CMySuperGrid::OnItemCollapsed(CTreeItem *pItem)
{
return 1;
}
CImageList *CMySuperGrid::CreateDragImageEx(int nItem)
{
if(m_bDrag)
return CSuperGridCtrl::CreateDragImageEx(GetDragItem());
else
return NULL;
}
void CMySuperGrid::_DeleteAll()
{
DeleteAll();//call CSuperGridCtrl::DeleteAll();
//add some new data
CItemInfo* lp = new CItemInfo();
lp->SetImage(4);
//add item text
lp->SetItemText(_T("New data"));
//Create root item
CTreeItem * pRoot = InsertRootItem(lp);//previous we call CreateTreeCtrl(lp)
if( pRoot == NULL )
return;
//insert items
int nCol = GetNumCol();
for(int i=0; i < nCol; i++)
{
CItemInfo* lpItemInfo = new CItemInfo();
CString strItem;
strItem.Format(_T("Item %d"),i);
//add items text
lpItemInfo->SetItemText(strItem);
//add subitem text
for(int y=0;y < nCol-1; y++)
{
CString str;
str.Format(_T("subItem %d of %s"),y,lpItemInfo->GetItemText());
lpItemInfo->AddSubItemText(str);
lpItemInfo->AddSubItemText(str);
}
//insert the iteminfo with ParentPtr
CTreeItem* pParent = InsertItem(pRoot, lpItemInfo);
//other nodes
if(i%nCol)
{
CTreeItem* pParent1=NULL;
CTreeItem* pParent2=NULL;
for(int x=0; x < nCol; x++)
{
CItemInfo* lpItemInfo = new CItemInfo();
CString strItem;
strItem.Format(_T("Item %d"),x);
lpItemInfo->SetItemText(strItem);
for(int z=0; z < nCol-1; z++)
{
CString str;
str.Format(_T("subItem %d of %s"),z, lpItemInfo->GetItemText());
lpItemInfo->AddSubItemText(str);
}
pParent1 = InsertItem(pParent, lpItemInfo);
}
}
}
//expand one level
Expand(pRoot, 0 /*listview index 0*/);
UINT uflag = LVIS_SELECTED | LVIS_FOCUSED;
SetItemState(0, uflag, uflag);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -