MySuperGrid.cpp
资源名称:supergrid.zip [点击查看]
上传用户:meggie0806
上传日期:2007-01-02
资源大小:87k
文件大小:27k
源码类别:
ListView/ListBox
开发平台:
Visual C++
- // MySuperGrid.cpp : implementation file
- //
- #include "stdafx.h"
- #include "supergrid.h"
- #include "MySuperGrid.h"
- #include "ComboInListView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMySuperGrid
- CMySuperGrid::CMySuperGrid()
- {
- m_bDrag = TRUE;
- }
- CMySuperGrid::~CMySuperGrid()
- {
- }
- BEGIN_MESSAGE_MAP(CMySuperGrid, CSuperGridCtrl)
- //{{AFX_MSG_MAP(CMySuperGrid)
- ON_WM_CREATE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMySuperGrid message handlers
- int CMySuperGrid::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CSuperGridCtrl::OnCreate(lpCreateStruct) == -1)
- return -1;
- /////////////////////
- //remember this
- ///////////////////
- //associate imagelist with listviewctrl
- if(!m_image.Create(IDB_FOLDERS,16,1,RGB(0, 255, 255)))
- return -1;
- SetImageList(&m_image, LVSIL_SMALL);
- CImageList *pImageList = GetImageList(LVSIL_SMALL);
- if(pImageList)
- ImageList_GetIconSize(pImageList->m_hImageList, &m_cxImage, &m_cyImage);
- else
- return -1;
- return 0;
- }
- void CMySuperGrid::InitializeGrid()
- {
- /*
- ExtendedStyle support:
- LVS_EX_TRACKSELECT
- LVS_EX_GRIDLINES
- LVS_EX_FLATSB
- LVS_EX_CHECKBOXES
- LVS_EX_FLATSB
- all other ExStyles are not supported...buhhh and you call your self a windows-developer :(
- */
- SetExtendedStyle(LVS_EX_GRIDLINES|LVS_EX_CHECKBOXES);
- LPTSTR lpszCols[] = {_T("Tree thing"),_T("Column #1"),_T("Column #2"),_T("Column #3"),_T("Column #4"),0};
- LV_COLUMN lvColumn;
- //initialize the columns
- lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
- lvColumn.fmt = LVCFMT_LEFT;
- lvColumn.cx = 200;
- for(int x = 0; lpszCols[x]!=NULL; x++)
- {
- //make the secondary columns smaller
- if(x)
- lvColumn.cx = 150;
- lvColumn.pszText = lpszCols[x];
- InsertColumn(x,&lvColumn);
- }
- CItemInfo* lp = new CItemInfo();
- lp->SetImage(4);
- //add item text
- lp->SetItemText(_T("Hello World"));
- //add subitem text
- lp->AddSubItemText(_T("Happy")); // 0 zero based subitems...
- lp->AddSubItemText(_T("Programming"));// 1
- lp->AddSubItemText(_T("with this")); // 2
- lp->AddSubItemText(_T("SuperGrid")); // 3 combo goes here
- //setup a combobox for the last column
- lp->SetControlType(lp->CONTROLTYPE::combobox, 3/*col # 3 from subitems...really col #4 in view, live with it*/);
- CStringList list;
- //initalized list for the combobox
- list.AddTail("Super control :}");
- list.AddTail("bad written control :{");
- list.AddTail("said piece of code :{");
- lp->SetListData(3, &list);
- //Create root item
- CTreeItem * pRoot = InsertRootItem(lp);//previous on N.Y.P.D we call it 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);
- }
- //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);
- }
- //this was pretty hard to get information !
- CItemInfo* lpAgent = new CItemInfo();
- lpAgent->SetItemText(_T("Mission: Impossible"));
- lpAgent->AddSubItemText(_T("Ethan Hunt"));
- lpAgent->AddSubItemText(_T("Jack Harmen"));
- lpAgent->AddSubItemText(_T("Jim Phelps"));
- lpAgent->AddSubItemText(_T("Allan Nielsen"));
- pParent2 = InsertItem(pParent1, lpAgent);
- CItemInfo* lpRelative = new CItemInfo();
- lpRelative->SetItemText("Nearst relative");
- lpRelative->AddSubItemText(_T("Margeret Ethan Hunt"));
- lpRelative->AddSubItemText(_T("Alexandra Soria"));
- lpRelative->AddSubItemText(_T("Claire Phelps"));
- lpRelative->AddSubItemText(_T("N/A"));
- InsertItem(pParent2, lpRelative);
- CItemInfo* lpTraningAgent = new CItemInfo();
- lpTraningAgent->SetItemText(_T("Training Agent"));
- lpTraningAgent->AddSubItemText(_T("Peter Menich"));
- lpTraningAgent->AddSubItemText(_T("Chris Smith"));
- lpTraningAgent->AddSubItemText(_T("Eva Wojdat"));
- lpTraningAgent->AddSubItemText(_T("Mr. Bean"));
- InsertItem(pParent2, lpTraningAgent);
- }
- }
- //Epilog
- CItemInfo* lpAgent = new CItemInfo();
- lpAgent->SetCheck(1);//I want this to be checked if LVS_EX_CHECKBOXES
- lpAgent->SetItemText(_T("Bugs: Impossible"));
- lpAgent->SetImage(3);
- CTreeItem *pParent2 = InsertItem(pRoot,lpAgent);
- CItemInfo* lpRelative = new CItemInfo();
- lpRelative->SetItemText("But if.....no When");
- lpRelative->AddSubItemText(_T("You"));//0
- lpRelative->AddSubItemText(_T("find one"));//1
- lpRelative->AddSubItemText(_T("please notify"));//2
- lpRelative->AddSubItemText(_T("me"));//3 combo goes here
- lpRelative->SetCheck(1); //I want this to be checked if LVS_EX_CHECKBOXES
- //add combobox in col 2(3)
- lpRelative->SetControlType(lpRelative->CONTROLTYPE::combobox, 2);
- CStringList lst;
- lst.AddTail("please call");
- lst.AddTail("shout at");
- lst.AddTail("phone");
- lpRelative->SetListData(2/*col*/, &lst);
- //add another combobox in this row in col 3(4)
- lpRelative->SetControlType(lpRelative->CONTROLTYPE::combobox, 3);
- CStringList list1;
- list1.AddTail("your mama");
- list1.AddTail("your pet dog");
- list1.AddTail("911");
- //set initalized list for combobox
- lpRelative->SetListData(3, &list1);
- InsertItem(pParent2, lpRelative);
- //create some node so we can sort them later on, well you can sort all of them but..
- CItemInfo* lpSort = new CItemInfo();
- lpSort->SetItemText("you can sort me and my children");
- CTreeItem *p=InsertItem(pRoot, lpSort);
- for(int a=10;a>=0;a--)
- {
- CItemInfo* lp = new CItemInfo();
- CString str;
- str.Format("%c%c",a+65,a+66);
- lp->SetItemText(str);
- CTreeItem *p1=InsertItem(p, lp);
- if(a%2)
- {
- for(int k=10;k>=0;k--)
- {
- CItemInfo* lp = new CItemInfo();
- CString str;
- str.Format("%c",k+65);
- lp->SetItemText(str);
- InsertItem(p1, lp);
- }
- }
- }
- //could now expand one level
- Expand(pRoot, 0 /*listview index 0*/);
- UINT uflag = LVIS_SELECTED | LVIS_FOCUSED;
- SetItemState(0, uflag, uflag);
- //Create another rootitem
- //block
- {
- CItemInfo* lp = new CItemInfo();
- lp->SetImage(4);
- //add item text
- lp->SetItemText(_T("Another Root Item"));
- CTreeItem * pRoot = InsertRootItem(lp);
- CItemInfo* lpAgent = new CItemInfo();
- lpAgent->SetItemText(_T("Bugs: well it's possible"));
- lpAgent->SetImage(3);
- CTreeItem *pParent2 = InsertItem(pRoot,lpAgent);
- CItemInfo* lpRelative = new CItemInfo();
- lpRelative->SetItemText("if");
- lpRelative->AddSubItemText(_T("You"));
- lpRelative->AddSubItemText(_T("find them"));
- lpRelative->AddSubItemText(_T("you are"));
- lpRelative->AddSubItemText(_T("on your own"));//3 combo goes here
- lpRelative->SetCheck(1); //I want this to be checked if LVS_EX_CHECKBOXES
- //add another combobox in this row in col 3(4)
- lpRelative->SetControlType(lpRelative->CONTROLTYPE::combobox, 3);
- CStringList list1;
- list1.AddTail("screwed");
- list1.AddTail("fucked");
- list1.AddTail("lucky");
- //set initalized list for combobox
- lpRelative->SetListData(3, &list1);
- InsertItem(pParent2, lpRelative);
- Expand(pRoot, NodeToIndex(pRoot) /*listview index 0*/);
- UINT uflag = LVIS_SELECTED | LVIS_FOCUSED;
- SetItemState(0, uflag, uflag);
- }
- }
- //helper function to copy CItemInfo used when drag/drop you must override this this function to suit your own CItemInfo class
- CItemInfo* CMySuperGrid::CopyData(CItemInfo* lpSrc)
- {
- ASSERT(lpSrc!=NULL);
- CItemInfo* lpDest = new CItemInfo;
- //well okay I put all the copy thing in one function, located in CItemInfo class,
- //so you should check out this function, remember to modify this function each time you add new data to CItemInfo class.
- lpDest->CopyObjects(lpSrc);
- return lpDest;
- }
- //override, like red!
- COLORREF CMySuperGrid::GetCellRGB()
- {
- return RGB(192,0,0);
- }
- //this is my override of GetIcon, override this to set what ever icon suits you
- int CMySuperGrid::GetIcon(const CTreeItem* pItem)
- {
- if(pItem!=NULL)
- {
- int n = GetData(pItem)->GetImage();
- if(n!=-1)
- return n;
- int iImage = 0;
- if(ItemHasChildren(pItem))
- {
- IsCollapsed(pItem) ? iImage = 1/*close icon*/:iImage = 0;/*open icon*/
- }
- else
- iImage = 2;//doc icon
- return iImage;
- }
- return 0;
- }
- //override
- void CMySuperGrid::OnUpdateListViewItem(CTreeItem* lpItem, LV_ITEM *plvItem)
- {
- //put some extra validation here
- CString str = (CString)plvItem->pszText;
- if(!str.Compare(_T("Bugs: Impossible")))
- {
- str+=_T(" (selfconfidence allright)");//that's valid enough :