这一课演示如何改变行或列的尺寸以及单元格文字的对齐方式。通常,当我们把鼠标移动到标题行的分割线或标题列的分隔线时,光标会变成左右或上下两个箭头形状的光标,此时就可以拖动鼠标来改变行或列的尺寸。当然也可以对表格编程以使它的行或列尺寸不可改变。第3课已经介绍了改变单元格属性既可以采用Get...Set函数也可以采用QuickSet()函数,这部分用QuickSet()函数去改变单元格的对齐方式。
第1步 修改OnCanSizeCol()函数
在MyCug()类的OnCanSizeCol()函数中添加如下代码。这段代码将使得用户无法改变第三列的宽度(把鼠标移动到第3列的右边线,就是上标题中绿色的4和5中间的分割线时,会跳出一个警告框)。
C++ Code
2 3 4 5 6 7 8 9 10 11 |
|
int MyCug::OnCanSizeCol(int col)
{ UNREFERENCED_PARAMETER(col); //****** Disallow column 3 from being resized if (col == 3) { MessageBox("illegal Procedure!!- Column 3 cannot be resized", "Grid Error", MB_ICONSTOP); return FALSE; } return TRUE; } |
第2步 修改OnCanSizeRow()函数
在MyCug()类的OnCanSizeRow()函数中添加如下代码。这段代码将使得用户无法改变第2行的高度(把鼠标移动到第2行的下边线,就是左标题中绿色的3和4中间的分割线时,会跳出一个警告框)。
C++ Code
2 3 4 5 6 7 8 9 10 11 |
|
int MyCug::OnCanSizeRow(long row)
{ UNREFERENCED_PARAMETER(row); if (row == 2) { //****** Disallow row 2 from being resized MessageBox("Illegal Procedure -- Row 2 cannot be resized.", "Grid Error", MB_ICONSTOP); return FALSE; } return TRUE; } |
第3步 改变表格文本对齐方式。
文本有好多种对齐方式(左对齐、右对齐、上对齐等等)。这里举例使用UG_ALIGNVCENTER(水平居中)。在MyCug()类中的OnSetup()函数中添加如下代码
C++ Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
//******* Declare all variables
int index , num_cols, num_rows; CUGCell cell; CString heading, number; //****** Set the Rows and Columns SetNumberCols(10); SetNumberRows(10) ; //******* Get the number of rows and columns num_rows = GetNumberRows(); num_cols = GetNumberCols(); //****** Set the column headings for (index = 0; index < num_rows; index++) { heading.Format("%d", index + 1); QuickSetText(-1, index, heading); QuickSetAlignment(-1, index, UG_ALIGNCENTER ); } //******* Set the row headings for (index = 0; index < num_cols; index++) { heading.Format("%d", index + 1); QuickSetText(index, -1, heading); QuickSetAlignment(index, -1, UG_ALIGNCENTER ); } //******* Set the multiplication table for (int x = 0; x < num_cols; x++) { for(int z = 0; z < num_rows; z++) { number.Format("%d", ((x + 1) * (z + 1))); QuickSetText(x, z, number); QuickSetAlignment(x, z, UG_ALIGNCENTER ); } } |
(数据都水平居中了)
第4步 改变表格中文字的字体
这个例子演示了如何改变字体风格(25pt 大小的"Arial Bold Italic" 字体)。添加下列代码到MyCug()类的OnSetup()函数中。
C++ Code
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
void MyCug::OnSetup()
{ //******* Declare all variables int index , num_cols, num_rows; CUGCell cell; CString heading, number; //****** Set the Rows and Columns SetNumberCols(10); SetNumberRows(10) ; //******* Get the number of rows and columns num_rows = GetNumberRows(); num_cols = GetNumberCols(); //****** Set the column headings for (index = 0; index < num_rows; index++) { heading.Format("%d", index + 1); QuickSetText(-1, index, heading); QuickSetAlignment(-1, index, UG_ALIGNCENTER ); } //******* Set the row headings for (index = 0; index < num_cols; index++) { heading.Format("%d", index + 1); QuickSetText(index, -1, heading); QuickSetAlignment(index, -1, UG_ALIGNCENTER ); } //******* Set the multiplication table for (int x = 0; x < num_cols; x++) { for(int z = 0; z < num_rows; z++) { number.Format("%d", ((x + 1) * (z + 1))); QuickSetText(x, z, number); QuickSetAlignment(x, z, UG_ALIGNCENTER ); } } //***** Create a new Font int fontIndex = AddFont("Arial Bold Italic", 25, 2); //****** Create a CFont pointer CFont *myFont; //****** Populate the CFont Pointer myFont = GetFont(fontIndex); //****** Set the font to column 0 GetColDefault(0, &cell); cell.SetFont(myFont); SetColDefault(0, &cell); } |