第3课 使用( Get...Set ) 函数

来源:互联网 发布:沙驰皮鞋 知乎 编辑:程序博客网 时间:2024/06/10 02:43

操作某个单元格、标题或者一列,甚至是整个表格,方法就是使用 Get...Set  函数。当你需要操作单元格的时候,必须先用GetCell()  函数去获取单元格对象的拷贝,然后修改其属性(比如你可以改变某个单元格的背景色,或则修改其文字),最后使用SetCell()函数再写回单元格改变它。正确的语法如下所示:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//******* Create a CUGCell object
CUGCell cell;

//******* Get cell 1,1
GetCell(11, &cell);

//******** Amend the cell properties
cell.SetText("Ultimate Toolbox");
cell.SetAlignment(UG_ALIGNTOP);

//****** Set 3 cells
SetCell(11, &cell);
SetCell(
21, &cell);
SetCell(
31, &cell);
 

第1步 对单元格信息进行修改。

当我们用鼠标的左键单击一个单元格时,程序会调用消息响应函数。下面的例子展示了在OnLClicked()消息响应函数内使用GetCell()和SetCell()函数。

程序将完成如下功能,当我们用鼠标的左键点击单元格时,此单元格的背景色将改变成青色,并将文字"Ultimate Toolbox" 写在格内。

添加下列代码到 MyCue 类的 OnLClicked()函数中

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*****************************************************************/
void MyCug::OnLClicked(int col, long row, int updn, RECT *rect, POINT *point, int processed)
{
    
//******* Declare all Variables
    CString message = "Ultimate Toolbox";
    CUGCell cell;

    
//******* Amend the cell using 'Get..Set'
    GetCell(col, row, &cell);

    cell.SetBackColor(RGB(
0255255));
    cell.SetText(message);
    SetCell(col, row, &cell);

    RedrawCell(col, row);
}

 下面是运行结果:

第2步 对某一列的默认参数进行修改。

可以使用 GetColDefault()   函数指定某一列,然后再用 SetColDefault()  函数修改这一列的属性(比如文字、背景色等等)。

下列代码段使第0列为红色的背景和文字靠右对齐。

 C++ Code 
1
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
void MyCug::OnSetup()
{
    
//****** Declare all variables
    CString heading, number;
    
int num_rows, num_cols;
    CUGCell cell;

    
//****** Set the Rows and Columns
    SetNumberCols(10);
    SetNumberRows(
10) ;

    
//****** Get the number of rows and columns
    num_rows = GetNumberRows();
    num_cols = GetNumberCols();

    
//******* Add Row Heading to the grid
    for (int index = 0; index < num_rows; index++)
    {
        heading.Format(
"%d", index + 1);
        QuickSetText(-
1, index, heading);
    }

    
//****** Add the Column Heading to the grid
    for (index = 0; index < num_cols; index++)
    {
        heading.Format(
"%d", index + 1);
        QuickSetText(index, -
1, heading);
    }

    
//***** Add the Multiplication table to the grid
    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);
        }
    }

    
//*******Set a Default style to column 0
    GetColDefault(0, &cell);
    cell.SetBackColor(RGB(
25500));
    cell.SetAlignment(UG_ALIGNRIGHT);
    SetColDefault(
0, &cell);
}

  

第3步 设置表格标题行与标题列的默认属性。

可以使用 GetHeadingDefault() 函数获得将要进行操作的标题行与标题列,然后再用 SetHeadingDefault() 函数修改其属性(比如文字、背景色等等)。

下列代码段使标题行与标题列为绿色的背景和文字竖直方向靠底边对齐。

 C++ Code 
1
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
57
58
void MyCug::OnSetup()
{
    
//****** Declare all variables
    CString heading, number;
    
int num_rows, num_cols;
    CUGCell cell ;

    
//****** Set the Rows and Columns
    SetNumberCols(10);
    SetNumberRows(
10) ;

    
//****** Get the number of rows and columns
    num_rows = GetNumberRows();
    num_cols = GetNumberCols();

    
//******* Add Row Heading to the grid
    for (int index = 0; index < num_rows; index++)
    {
        heading.Format(
"%d", index + 1);
        QuickSetText(-
1, index, heading);
    }

    
//****** Add the Column Heading to the grid
    for (index = 0; index < num_cols; index++)
    {
        heading.Format(
"%d", index + 1);
        QuickSetText(index, -
1, heading);
    }

    
//***** Add the Multiplication table to the grid
    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);
        }
    }

    
//******* Create a CFont pointer
    CFont *myFont;

    
//******* create the font index variable
    int fontIndex ;

    
//****** Get the font index
    fontIndex = AddFont);

    
//******* Populate the CFont pointer
    myFont = GetFont(fontIndex);

    
//******** Set the Heading Default
    GetHeadingDefault(&cell);
    cell.SetFont(myFont);
    cell.SetAlignment(UG_ALIGNBOTTOM);
    cell.SetBackColor(RGB(
02550));
    SetHeadingDefault(&cell);
}
 

第4步 对表格默认属性进行修改。

可以使用GetGridDefault() 函数获得整个表格,然后再用 SetGridDefault()  函数修改其属性(比如风格、背景色等等)。

下列代码段使表格的背景色为青色,单元格风格为边框突起

 C++ Code 
1
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
void MyCug::OnSetup()
{
    
//**** Declare all variables
    CUGCell cell ;
    CString buf, number;

    
//****** Define the Grid Default
    GetGridDefault(&cell);
    cell.SetBorder(UG_BDR_RAISED);
    cell.SetBackColor(RGB(
0255255));
    SetGridDefault(&cell);

    
//****** Set the number of rows and columns
    SetNumberCols(10);
    SetNumberRows(
10);

    
//****** Get the number of rows and columns
    int numRows = GetNumberRows();
    
int numCols = GetNumberCols();

    
//******* Add the top heading
    for (int xx = 0; xx < numCols; xx++)
    {
        buf.Format(
"%d", xx + 1);
        QuickSetText(xx, -
1, buf);
    }

    
//****** Add the side heading
    for (int zz = 0; zz < numRows; zz++)
    {
        buf.Format(
"%d", zz + 1);
        QuickSetText(-
1, zz, buf);
    }

    
//***** Add the multiplication table to the grid
    for (int x = 0; x < numCols; x++)
    {
        
for(int z = 0; z < numRows; z++)
        {
            number.Format(
"%d", ((x + 1) * (z + 1)));
            QuickSetText(x, z, number);
        }
    }
}
 

原创粉丝点击