Exemplo n.º 1
0
        } //End Function

        #region protected virtual Cell GetMergeCell(Point GridLocation,string[,] arrStrGrid,int rowHeight,int[] ArrColWidth,int rowSel,int colSel,GridMergeFlag gridMergeFlag)

        /// <summary>
        /// 任意合并方式下返回指定单元格左顶宽高
        /// </summary>
        /// <param name="GridLocation">网格起点坐标</param>
        /// <param name="arrStrGrid">二维网格</param>
        /// <param name="rowHeight">行高</param>
        /// <param name="ArrColWidth">列宽数组</param>
        /// <param name="rowSel">指定单元格行</param>
        /// <param name="colSel">指定单元格列</param>
        /// <returns></returns>
        protected virtual CellRectangle GetMergeCell(Point GridLocation, string[,] arrStrGrid, int rowHeight,
            int[] ArrColWidth, int rowSel, int colSel, GridMergeFlag gridMergeFlag)
        {
            CellRectangle cell = new CellRectangle(0, 0, 0, 0);

            int lngRows = arrStrGrid.GetLength(0); //行数
            int lngCols = arrStrGrid.GetLength(1); //列数

            int lngMergeRows = 1; //合并的行数(本身为1)
            int lngMergeCols = 1; //合并的列数

            int lngStartRow = rowSel; //记录与此单元格合并的起始行
            int lngEndRow = rowSel; //以便计算高及起点Y坐标

            int lngStartCol = colSel; //记录与此单元格合并的起始列
            int lngEndCol = colSel; //以便计算宽及起点X坐标

            if (gridMergeFlag == GridMergeFlag.Any || gridMergeFlag == GridMergeFlag.Col ||
                gridMergeFlag == GridMergeFlag.ColDependOnBeforeGroup)
            {
                //计算在"列"上进行行合并时起始行与合并的多少
                //往上查合并(列不变)
                for (int rowIndex = rowSel - 1; rowIndex >= 0; rowIndex--)
                {
                    if (arrStrGrid[rowSel, colSel] == arrStrGrid[rowIndex, colSel])
                    {
                        lngMergeRows++;
                        lngStartRow--;
                    }
                    else
                    {
                        break;
                    }
                }
                //往下查合并(列不变)
                for (int rowIndex = rowSel + 1; rowIndex < lngRows; rowIndex++)
                {
                    if (arrStrGrid[rowSel, colSel] == arrStrGrid[rowIndex, colSel])
                    {
                        lngMergeRows++;
                        lngEndRow++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (gridMergeFlag == GridMergeFlag.Any || gridMergeFlag == GridMergeFlag.Row)
            {
                //计算在"行"上进行列合并时起始列与合并的多少
                //往左查合并(行不变)
                for (int colIndex = colSel - 1; colIndex >= 0; colIndex--)
                {
                    if (arrStrGrid[rowSel, colSel] == arrStrGrid[rowSel, colIndex])
                    {
                        lngMergeCols++;
                        lngStartCol--;
                    }
                    else
                    {
                        break;
                    }
                }
                //往右查合并(行不变)
                for (int colIndex = colSel + 1; colIndex < lngCols; colIndex++)
                {
                    if (arrStrGrid[rowSel, colSel] == arrStrGrid[rowSel, colIndex])
                    {
                        lngMergeCols++;
                        lngEndCol++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //******************计算左顶宽高******************
            int cellLeft = GridLocation.X;
            int cellTop = GridLocation.Y + lngStartRow*rowHeight; //若行高不是固定行高,可以计算之前行的行高总和

            int cellWidth = 0;
            int cellHeight = 0;

            //单元格合并列的前边的单元格列宽和
            for (int i = lngStartCol - 1; i >= 0; i--)
            {
                cellLeft += ArrColWidth[i];
            }

            //单元格合并列列宽和
            for (int i = lngStartCol; i <= lngEndCol; i++)
            {
                cellWidth += ArrColWidth[i];
            }

            cellHeight = lngMergeRows*rowHeight; //若行高不是固定行高,可以计算所有行的行高总和

            cell = new CellRectangle(cellLeft, cellTop, cellWidth, cellHeight);

            return cell;
        }
Exemplo n.º 2
0
        } //End Function

        #endregion

        protected void DrawGridText(Graphics g, Rectangle p_rec, Brush p_brush, string[,] arrStrGrid, int p_rowHeight,
            int[] p_arrColsWidth, string alignment, Font p_font, PointF p_scaleXY, GridMergeFlag gridMergeFlag)
        {
            if (gridMergeFlag == GridMergeFlag.None)
            {
                DrawGridText(g, p_rec, p_brush, arrStrGrid, p_rowHeight, p_arrColsWidth, alignment, p_font, p_scaleXY);
                return;
            }

            try
            {
                //缩放矩阵,用于绘图
                Rectangle rec = new Rectangle(p_rec.X, p_rec.Y, p_rec.Width, p_rec.Height);

                Font font = p_font;
                if (font == null)
                {
                    font = new Font("宋体", 12.0F);
                }

                int lngRows = arrStrGrid.GetLength(0); //行数
                int lngCols = arrStrGrid.GetLength(1); //列数

                //列宽
                int[] mArrColWidth = new int[lngCols];
                mArrColWidth = p_arrColsWidth;

                //列对齐方式
                AlignFlag[] arrAlign;
                arrAlign = this.GetColsAlign(alignment);

                //变换
                this.TransGrid(g, rec, p_scaleXY);

                #region	画单元格文本

                StringFormat sf = new StringFormat(); //字符格式
                sf.LineAlignment = StringAlignment.Center; //垂直居中
                sf.FormatFlags = StringFormatFlags.LineLimit; //| StringFormatFlags.NoWrap; //可换行否

                CellRectangle cell = new CellRectangle(rec.X, rec.Y, 0, p_rowHeight); //单元格

                for (int i = 0; i < lngRows; i++)
                {
                    for (int j = 0; j < lngCols; j++)
                    {
                        //.....
                        cell = this.GetMergeCell(new Point(rec.X, rec.Y), arrStrGrid, p_rowHeight, mArrColWidth, i, j,
                            gridMergeFlag);

                        Rectangle recCell = new Rectangle(cell.Left, cell.Top, cell.Width, cell.Height + 4);
                            //实际上居中会稍微偏上,因为字体有预留边距

                        sf.Alignment = StringAlignment.Near; //默认左对齐						

                        if (arrAlign.Length > j)
                        {
                            if (arrAlign[j] == AlignFlag.Center)
                            {
                                sf.Alignment = StringAlignment.Center; //居中
                            }
                            else if (arrAlign[j] == AlignFlag.Right)
                            {
                                sf.Alignment = StringAlignment.Far; //居右
                            }
                        }

                        g.DrawString(arrStrGrid[i, j], font, p_brush, recCell, sf);
                    } //End For 列	
                } //End For 行					

                #endregion

                //重置,不再变换
                this.ResetTransGrid();

                //font.Dispose();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
            finally
            {
            }
        } //End Function