[wxWidgets]_[中级]_[常用的wxGrid的总结]


场景:

1. wxGrid是比较好用的可定制的table,支持单元格显示效果定制,合并,隐藏,局部刷新等等。总体来说,还是比较实用的控件。


总结:

1. 初始化

grid->CreateGrid(0, 0);
	grid->EnableGridLines(true);
	grid->SetRowLabelSize(32);
	grid->SetDefaultRowSize(140);
	grid->SetScrollLineX(1);

	wxFont font(9, wxFONTFAMILY_SWISS, wxNORMAL, wxNORMAL, false, wxT("Arial"),
			wxFONTENCODING_ISO8859_1);
	grid->SetLabelFont(font);
	grid->SetSelectionMode(wxGrid::wxGridSelectCells);

	grid->HideCellEditControl();

	grid->SetEditable(true);
	grid->SetCellHighlightPenWidth(0);
	grid->EnableDragGridSize(false);

	grid->SetUseNativeColLabels(true);

	AddTitle(grid, wxT("."), 112, true);
	AddTitle(grid, wxT("."), 112, true);
	AddTitle(grid, wxT("."), 112, true);
	AddTitle(grid, wxT("."), 112, true);
	AddTitle(grid, wxT("."), 112, true);

	grid->SetColLabelSize(0);
	wxColor color = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
	grid->SetSelectionBackground(color);


2.添加行

grid_->InsertRows(position, 1, false);

	DhPhotoCheckCellRender* render = new DhPhotoCheckCellRender();
	render->SetShowText(array[0]);
	render->CenterCheckOffset();
	grid_->SetReadOnly(position, 0, true);
	grid_->SetCellRenderer(position, 0, render);
	grid_->SetCellValue(position, 0, wxT(" "));

3.设置单元格渲染

class DhPhotoCheckCellRender: public wxGridCellRenderer

4.使用render和editor,wxGrid使用这两个比较特殊,它在内部维护一个对render和editor的引用计数.在每次GetRender和GetEidtor返回前会InCRef,所以每次返回后需要DecRef.

DhPhotoCheckCellRender* render =
					(DhPhotoCheckCellRender*) current_show_grid_->GetCellRenderer(
							i, j);
			render->DecRef();

注意,这里如果减的次数过多就会崩溃。看chm文档有描述.


5.wxGrid对动态增加列貌似支持不好,我Del 全部列再新增列就会崩溃,可能是bug吧,这个问题已经找到原因,是del全部后insert行就会触发一个wxEVT_GRID_SELECT_CELL事件,很奇怪,之后这个事件触发函数调用了一个没创建的自定义render就会崩溃.

grid_->DeleteAllRows();
	grid_->HideOtherCols(5);


备注(wx3.0.0): 快速DeleteRows并且InsertRows经常会崩溃,临时解决方案是先AppendRows到末尾,之后再删除前面的.


6.绑定常用的事件

grid->Connect(wxEVT_GRID_SELECT_CELL,
			wxGridEventHandler(DhRecoveryKindWindow::OnCellLeftClick), NULL,
			this);

7.设置列宽和表头

SetColLabelValue(index, label);
	SetColSize(index, width);


8.添加表头

AddTitle

int col_num = grid->GetNumberCols();
	grid->AppendCols();
	if (is_show)
	{
		grid->ShowCol(col_num);
		grid->SetColLabelValue(col_num, grid_title);
		grid->SetColSize(col_num, grid_width);
		grid->SetColLabelAlignment(wxALIGN_CENTRE, wxALIGN_CENTRE);
	}
	else
	{
		grid->HideCol(col_num);
	}




你可能感兴趣的:(注意事项,wxwidgets,wxGrid)