GridCtrl






4.92/5 (21 votes)
This article describes GridCtrl that consists of common controls such as EditBox, ComboBox and Button. The GridCtrl helps you create, exchange and manage layouts.
Introduction
Some times I work at forms that have many data fields, such as combo, edit and buttons. Also, I need removing, dynamically adding and managing the layout of these fields automatically. Finding nothing such as I needed, I decided to create a control by myself, that should have these features.
I want to express my thanks to Mathias Tunared for his AdvComboBox
that I am using in my project.
What the GridCtrl
does:
- Creates three controls: edit, combo or button; that have a static (header) field on left side;
- Combines these fields into column and row, that have header (on top);
- Allows you to have access to any field with help index or ID manager;
- Makes exchanging data with fields possible;
- Adds or removes any field at run-time.
How to use
At first, you should create GridCtrl
. For that, use the function Create
, that is defined as:
bool CGridCtrl::Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID);
DWORD dwStyle
- style of theGridCtrl
which can be one of the following:0
- create simple grid;GS_AUTOVSIZE
- create grid that will change its own vertical size.
RECT& rct
- size ofGridCtrl
. If you defineGS_AUTOVSIZE
,RECT::bottom
has no effect.CWnd* pParent
- parent window.UINT nID
- ID ofGridCtrl
.
Before adding a field, I should explain some features. The GridCtrl
has a class CGridMgr
, that manages operations like insertion, deletion etc. It's available via the overloaded operator->
. There are two ways in GridCtrl
for managing these operations: using the appropriate functions or using properties.
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 of item (any message sent to parent is a standard notifications message).void* pvData
- User defined data.
Now, we have enough information for creating GridCtrl
and adding some items. Let me show an example to you, so that you can fully appreciate the process:
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
Function and classes
- Class
CGridCtrl
bool Create(DWORD dwStyle,RECT& rct,CWnd* pParent,UINT nID)
- see top of this article._GridMgr* operator->()
- gives access to a_GridMgr
object.CGridCtrl& operator<<(grid_item& gi)
- insert new item to back.void MoveWindow(RECT* rct)
- move grid control according to the setGS_AUTOVSIZE
flag.
template<class TObj>class CGridMgr
CItemMgr<TOBJ>* ID[]
- get property for accessing item manager withID
.CItemMgr<TOBJ>* Index[]
- get property for accessing item manager withIndex
.bool ReadOnlyAll
- put property set's all item as read only.DWORD Count
- get property return number of item in manager.void DeleteAll()
- delete all items from grid manager.
template<class TObj> class CItemMgr
bool Insert(grid_item& gi)
- insert item into item manager.bool Delete()
- remove item from item manager.CString Name
- put/get property set/get header item name.CString Text
- put/get property set/get item text.GI_TYPE Type
- get property return type of item.void* Data
- put/get property set/get user defined data to/from item.CSubItemMgr<CADVCOMBOBOX>* SubItem
- get property return subitem manager for an item as combo box.int ID
- get property return item ID.int Index
- get property return item index.bool ReadOnly
- put/get property set/get read only state.void SetFocus()
- sets focus to item.
template<class TCombo> class CSubItemMgr
void Insert(const char* pcText,LPARAM lData=-1,int nIndex=-1)
- insert subitem into combobox.void Delete(int nIndex=-1)
- delete by index. IfnIndex
= -1, last item will be deleted.int Find(const char* pcText)
- find item with specified string. If the find succeeds return index of the item, otherwise return -1.int Find(LPARAM lData)
- find item with specified data, return value same asint Find(const char* pcText)
.int Count
- get property, return count item in combobox.int Select
- put/get property, set/get selection to currently selected item in combo box.CString Text[]
- get property, get text from item with index.LPARAM Data[]
- get property, get data from item with index.
Color and messages
Color of GridCtrl
has been selected at the time of development, but you can change any color using macros:
//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
Any message from items in the grid are sent to parent window that created the grid. Thr ID of message is the ID that is passed to one of these:
gi_hdr(...) gi_edit(...) gi_combo(...)