Visual C++ 7.1Visual Studio .NET 2003Windows 2003Windows 2000Windows XPMFCIntermediateDevVisual StudioWindowsC++
GridCtrl






4.92/5 (21投票s)
本文介绍了GridCtrl,它由编辑框、组合框和按钮等通用控件组成。GridCtrl可以帮助您创建、交换和管理布局。
引言
有时我需要处理包含许多数据字段(如组合框、编辑框和按钮)的表单。此外,我还想自动移除、动态添加和管理这些字段的布局。由于找不到我需要的东西,我决定自己创建一个具有这些功能的控件。
我想感谢Mathias Tunared,因为我的项目中使用了他的AdvComboBox
。
GridCtrl
的功能
- 创建三种控件:编辑框、组合框或按钮;在左侧带有一个静态(标题)字段;
- 将这些字段组合成具有(顶部)标题的列和行;
- 允许您通过索引或ID管理器访问任何字段;
- 实现与字段的数据交换;
- 在运行时添加或删除任何字段。
如何使用
首先,您需要创建GridCtrl
。为此,请使用函数Create
,其定义如下:
bool CGridCtrl::Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID);
DWORD dwStyle
-GridCtrl
的样式,可以是以下之一:0
- 创建简单的网格;GS_AUTOVSIZE
- 创建一个会自动调整自身垂直大小的网格。
RECT& rct
-GridCtrl
的大小。如果您定义了GS_AUTOVSIZE
,则RECT::bottom
无效。CWnd* pParent
- 父窗口。UINT nID
-GridCtrl
的ID。
在添加字段之前,我需要解释一些特性。GridCtrl
有一个CGridMgr
类,它负责管理插入、删除等操作。可以通过重载的operator->
来访问它。在GridCtrl
中有两种方法来管理这些操作:使用相应的函数或使用属性。
gi_hdr(const char* pcName=0, DWORD dwStyle=0, DWORD dwID=-1, void* pvData=0); //Header item gi_edit(...); //Edit item gi_combo(...); //Combo item const char* pcName //- title; DWORD dwStyle //- style /////// Layout style ///////// #define GLS_NR 0x000000 //create with new row #define GLS_NC 0x010000 //create with new col /////// Grid edit style ////////////// #define GES_PASS 0x1 //password #define GES_NUMB 0x2 //number /////// Grid combo style ///////////// #define GCS_DRDNL 0x0 //Drop down list #define GCS_DRDN 0x1 //Drop down /////// Grid custom style //////////// #define GCSS_CSBT 0x0100 // Item with custom button // (send command message to parent when click) /////// Grid shared style //////////// #define GSS_RDNL 0x10000000 //Item is readonly
DWORD dwID
- 项目的ID(发送到父窗口的任何消息都是标准的通知消息)。void* pvData
- 用户定义的数据。
现在,我们有了足够的信息来创建GridCtrl
并添加一些项目。让我给您展示一个例子,以便您能完全理解这个过程。
CGridCtrl m_Grid; m_Grid <<gi_hdr("Header") <<gi_edit("Edit",0,0x00) <<gi_combo("Combo",0,0x01) <<gi_hdr("Header",GLS_NR) <<gi_edit("Edit(password)",GES_PASS,0x02) <<gi_combo("Combo (drop down list) with cbutton",GCS_DRDNL|GCSS_CSBT,0x03) ; //Follow code example how to exhange data with items: //set text to edit m_Grid->ID[0x00]->Text="Edit Text"; //insert item into ComboBox m_Grid->ID[0x01]->SubItem->Insert("Text ListBox Item",-1,0); //select first item m_Grid->ID[0x01]->SubItem->Select=0; //get text from edit CString strEdit(m_Grid->ID[0x00]->Text); //get current selectetd item from combo int nSel(m_Grid->ID[0x01]->SubItem->Select); //get text from first item CString strSubItem(m_Grid->ID[0x01]->SubItem->Text[0]); //or CString strSubItem(m_Grid->ID[0x01]->SubItem->Text); //get text from current selected item
函数和类
- 类
CGridCtrl
bool Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID)
- 参见本文顶部。_GridMgr* operator->()
- 提供对_GridMgr
对象的访问。CGridCtrl& operator<<(grid_item& gi)
- 将新项目添加到末尾。void MoveWindow(RECT* rct)
- 根据设置的GS_AUTOVSIZE
标志移动网格控件。
template<class TObj>class CGridMgr
CItemMgr<TOBJ>* ID[]
- 获取用于通过ID
访问项目管理器属性。CItemMgr<TOBJ>* Index[]
- 获取用于通过Index
访问项目管理器属性。bool ReadOnlyAll
- 设置所有项目为只读的属性。DWORD Count
- 获取属性,返回管理器中的项目数量。void DeleteAll()
- 从网格管理器中删除所有项目。
template<class TObj> class CItemMgr
bool Insert(grid_item& gi)
- 将项目插入项目管理器。bool Delete()
- 从项目管理器中删除项目。CString Name
- 设置/获取项目标题的属性。CString Text
- 设置/获取项目文本的属性。GI_TYPE Type
- 获取项目类型的属性。void* Data
- 设置/获取用户定义数据到/从项目的属性。CSubItemMgr<CADVCOMBOBOX>* SubItem
- 获取作为组合框的项目的子项管理器的属性。int ID
- 获取项目ID的属性。int Index
- 获取项目索引的属性。bool ReadOnly
- 设置/获取只读状态的属性。void SetFocus()
- 将焦点设置到项目。
template<class TCombo> class CSubItemMgr
void Insert(const char* pcText,LPARAM lData=-1,int nIndex=-1)
- 将子项插入组合框。void Delete(int nIndex=-1)
- 按索引删除。如果nIndex
= -1,则删除最后一个项目。int Find(const char* pcText)
- 查找具有指定字符串的项目。如果查找成功,则返回该项目的索引,否则返回-1。int Find(LPARAM lData)
- 查找具有指定数据的项目,返回值与int Find(const char* pcText)
相同。int Count
- 获取属性,返回组合框中的项目数量。int Select
- 设置/获取属性,设置/获取组合框中当前选定项。CString Text[]
- 获取属性,根据索引获取项目文本。LPARAM Data[]
- 获取属性,根据索引获取项目数据。
颜色和消息
GridCtrl
的颜色在开发时已选定,但您可以使用宏更改任何颜色。
//GridCtrl.h #define G_BKGND 0xC9DBC8 //background #define G_GHEAD_CLR 0x9EC29B //color of header (from item top) #define G_IHEAD_CLR 0xACC2AB //color of header (at item left) #define G_EDIT_RDNL_CLR 0xE7EEE7 //background of readonly items
网格中项目的任何消息都将发送到创建该网格的父窗口。消息的ID是传递给以下之一的ID。
gi_hdr(...) gi_edit(...) gi_combo(...)