GridCtrl控件FAQ

发布时间 : 星期四 文章GridCtrl控件FAQ更新完毕开始阅读

? 注:

复杂但更灵活的方法是采用如下的方法: GV_ITEM Item;//一个结构

设定这个结构的成员参数,然后将这个结构传递给表格, CGridCtrlObject.SetItem(&Item);

typedef struct _GV_ITEM {

int row,col; // Row and Column of item

UINT mask; // Mask for use in getting/setting cell data UINT state; // cell state (focus/hilighted etc) UINT nFormat; // Format of cell. Default imaplentation // used CDC::DrawText formats CString szText; // Text in cell

int iImage; // index of the list view item’s icon COLORREF crBkClr; // Background colour (or CLR_DEFAULT) COLORREF crFgClr; // Forground colour (or CLR_DEFAULT) LPARAM lParam; // 32-bit value to associate with item LOGFONT lfFont; // cell font

} GV_ITEM;

例如以下代码可以设置表格内容: m_GridCtrl.SetRowCount(3);

m_GridCtrl.SetItemText(1, 0, \第一格\m_GridCtrl.SetItemText(1, 1, \第二格\m_GridCtrl.SetItemText(1, 2, \第三格\m_GridCtrl.SetItemText(2, 0, \第四格\m_GridCtrl.SetItemText(2, 1, \第五格\m_GridCtrl.SetItemText(2, 2, \第六格\m_GridCtrl.ExpandColumnsToFit();

6. 在选定一行时有响应函数 7. 由双击的响应函数 8. 由响应右键点击的函数

A:

当进行单击,双击或右击单元格等操作时,表格会发送响应的消息,可以在父窗口添加处理消息的函数,做法如下:

GVN_BEGINDRAG // Sent when dragging starts

GVN_BEGINLABELEDIT // Sent when inplace editing starts GVN_ENDLABELEDIT // Sent when inplace editing stops

—5—

GVN_SELCHANGING // Sent just before cell selection changes GVN_SELCHANGED // Sent after cell selection changes

GVN_GETDISPINFO // A request for cell information when the grid is // in virtual mode

GVN_ODCACHEHINT // Cache hint when in virtual mode

右键点击,按键盘响应消息在扩展中实现了,可参见条款21 以下需要手工添加 H文件中:

//}}AFX_MSG

afx_msg void OnGridDblClick(NMHDR *pNotifyStruct, LRESULT* pResult); afx_msg void OnGridClick(NMHDR *pNotifyStruct, LRESULT* pResult); DECLARE_MESSAGE_MAP() // Generated message map functions //{{AFX_MSG(CDlgSectionLib)

afx_msg void OnGridEndSelChange(NMHDR *pNotifyStruct, LRESULT* pResult); CPP文件中:

BEGIN_MESSAGE_MAP(CDlgSectionLib, CDialog)

END_MESSAGE_MAP() 然后自定义响应函数

void CDlgSectionLib::OnGridDblClick(NMHDR *pNotifyStruct, LRESULT* pResult) {

NM_GRIDVIEW* pItem = (NM_GRIDVIEW*) pNotifyStruct; pItem->iRow, pItem->iColumn;//得到当前行、列 }

// This structure sent to Grid's parent in a WM_NOTIFY message typedef struct tagNM_GRIDVIEW { NMHDR hdr; int iRow; int iColumn; } NM_GRIDVIEW;

//{{AFX_MSG_MAP(CDlgSectionLib) //}}AFX_MSG_MAP

ON_NOTIFY(NM_DBLCLK, IDC_GRID, OnGridDblClick) ON_NOTIFY(NM_CLICK, IDC_GRID, OnGridClick)

ON_NOTIFY(GVN_SELCHANGED, IDC_GRID, OnGridEndSelChange)

typedef struct tagNMHDR {

HWND hwndFrom; // handle of control sending message UINT idFrom;// identifier of control sending message UINT code; // notification code; see below

—6—

} NMHDR;

NM_CLICK The user has clicked the left mouse button within the control. NM_DBLCLK The user has double-clicked the left mouse button within the control. NM_KILLFOCUS The control has lost the input focus.

NM_OUTOFMEMORY The control could not complete an operation because there is not enough memory available.

NM_RCLICK The user has clicked the right mouse button within the control. NM_RDBLCLK The user has double-clicked the right mouse button within the control.

NM_RETURN The control has the input focus, and the user has pressed the ENTER key.

NM_SETFOCUS The control has received the input focus.

9. 可以方便的删除和添加固定列头 10.可以设置、删除、添加固定行头

A:

参考(4)&(5)

删除可以用以下一些函数

BOOL DeleteColumn(int nColumn); BOOL DeleteRow(int nRow); BOOL DeleteNonFixedRows(); BOOL DeleteAllItems();

11.可以在第一个单元格中加入Check控件

A: #include \包含头文件

BOOL CGridCtrl::SetCellType(int nRow, int nCol, CRuntimeClass* pRuntimeClass);

比如:CGridCtrlObject.SetCellType(1,1, RUNTIME_CLASS(CGridCellCheck));

—7—

12.可设表格的背景和字体,可设单元格的颜色和字体

设置表格的颜色CGridCtrlObject.GetDefaultCell(FALSE, ALSE)->SetBackClr(RGB(xxx,xxx,xxx));

下面的函数均可以调用:

virtual void CGridCtrl::SetTextClr(COLORREF clr);

virtual void CGridCtrl::SetBackClr(COLORREF clr); virtual void CGridCtrl::SetFont(const LOGFONT* plf);

设置单元格的背景颜色和前景颜色

BOOL CGridCtrl::SetItemBkColour(int nRow, int nCol, COLORREF cr = CLR_DEFAULT)

BOOL CGridCtrl::SetItemFgColour(int nRow, int nCol, COLORREF cr = CLR_DEFAULT)

BOOL CGridCtrl::SetItemFont(int nRow, int nCol, LOGFONT*lf)

颜色:

COLORREF clr = RGB(xxx,xxx,xxx);

字体:

CFont* pFont = m_Grid.GetFont(); LOGFONT lf;

pFont->GetLogFont(&lf);

memcpy(lf.lfFaceName, _T(\), 6); lf.lfEscapement = 900; lf.lfOrientation = 900;

关于单元格的格式都可以通过下述方法设定,同前面关于设置单元格内容(4)的方法

GV_ITEM Item 设置单元格格式 Item.crBkClr = ?; Item.crFgClr = ?; Item.lfFont=?; SetItem(&Item);

Item.mask |= (GVIF_BKCLR|GVIF_FGCLR);

13.可以方便的添加或者删除一行

int CGridCtrl::InsertColumn(LPCTSTR strHeading,UINT nFormat, int nColumn = -1)

int CGridCtrl::InsertRow(LPCTSTR strHeading, int nRow = -1)

—8—

联系合同范文客服:xxxxx#qq.com(#替换为@)