Пример #1
0
 public static void OnReportCellStringDrawing(ReportCellStringDrawingArgs e)
 {
     if (ReportCellStringDrawing != null)
     {
         ReportCellStringDrawing(e);
     }
 }
Пример #2
0
        public virtual RectangleF DrawContent(Graphics g, float scale, I3ReportData reportData, I3ReportCell cell, RectangleF rect, I3ReportCellStyle style, I3PrintArea area, bool draw)
        {
            string text = GetText(cell, area);

            if (string.IsNullOrEmpty(text))
            {
                return(RectangleF.Empty);
            }

            StringFormat sf = GetStringFormat(cell, style);
            //TextFormatFlags ff = GetTextFormatFlags(cell, style);
            Brush brush = new SolidBrush(style.FontColor);
            Font  font  = GetFont(scale, cell, style);


            string[] strs = text.Split(new string[] { NewLineFlag }, StringSplitOptions.None);

            try
            {
                //先计算正常位置
                RectangleF        rr       = RectangleF.Empty;
                List <RectangleF> rectList = new List <RectangleF>();
                float             startY   = float.MaxValue; //上线
                float             endY     = float.MinValue; //下线
                foreach (string str in strs)
                {
                    #region 计算文本绘制区域
                    SizeF sizeF = g.MeasureString(str, font, (int)rect.Width, sf);
                    //Size sizeF = TextRenderer.MeasureText(g, str, font, new Size((int)rect.Width, 200), ff);
                    RectangleF r = new RectangleF(rect.X, rect.Y, rect.Width, rect.Height);
                    if (style.Alignment == StringAlignment.Center)
                    {
                        r.X    += r.Width / 2 - sizeF.Width / 2;
                        r.Width = sizeF.Width;
                    }
                    else if (style.Alignment == StringAlignment.Far)
                    {
                        r.X    += r.Width - sizeF.Width;
                        r.Width = sizeF.Width;
                    }
                    else
                    {
                        r.Width = sizeF.Width;
                    }
                    if (style.LineAlignment == StringAlignment.Center)
                    {
                        r.Y     += r.Height / 2 - sizeF.Height / 2;
                        r.Height = sizeF.Height;
                    }
                    else if (style.LineAlignment == StringAlignment.Far)
                    {
                        r.Y     += r.Height - sizeF.Height;
                        r.Height = sizeF.Height;
                    }
                    else
                    {
                        r.Height = sizeF.Height;
                    }
                    #endregion

                    #region 居中或居下时的处理
                    if (style.LineAlignment == StringAlignment.Center)
                    {
                        //下面会有一点空白区域,下移以便于达到真正的垂直居中
                        //RectangleF stringRect = new RectangleF(r.X, r.Y + r.Height * 0.1F, r.Width, sizeF.Height);
                        //r = stringRect;
                        //2017.10.09  调整了一下偏移量,不然自动换行时,上面有间距而下面没有
                        RectangleF stringRect = new RectangleF(r.X, r.Y + r.Height * 0.05F, r.Width, sizeF.Height);
                        r = stringRect;
                    }
                    else if (style.LineAlignment == StringAlignment.Far)
                    {
                        //下面会有一点空白区域,下移以便于达到真正的垂直居下
                        RectangleF stringRect = new RectangleF(r.X, r.Y + r.Height * 0.2F, r.Width, r.Height);
                        r = stringRect;
                    }
                    #endregion

                    rectList.Add(r);
                    startY = Math.Min(startY, r.Y);
                    endY   = Math.Max(endY, r.Y + r.Height);
                }
                float centerY = startY + (endY - startY) / 2;//中线
                float stepY   = endY - startY;

                //再计算换行引起的位置偏移
                int i = -1;
                foreach (string str in strs)
                {
                    i++;
                    RectangleF r = rectList[i];
                    #region 换行位置调整
                    if (strs.Length > 0)                                   //有多行才做调整
                    {
                        if (style.LineAlignment == StringAlignment.Center) //居中
                        {
                            float totalHeight = strs.Length * stepY;
                            float totalStartY = centerY - totalHeight / 2;
                            r.Y = totalStartY + stepY * i;
                        }
                        else if (style.LineAlignment == StringAlignment.Far)//靠下
                        {
                            float thisEndY = endY - stepY * i;
                            r.Y = thisEndY - r.Height;
                        }
                        else//靠上
                        {
                            r.Y = startY + stepY * i;
                        }
                    }
                    #endregion

                    #region 绘制
                    if (draw && !r.IsEmpty)
                    {
                        ReportCellStringDrawingArgs e = new ReportCellStringDrawingArgs(g, str, font, brush, r, sf);
                        OnReportCellStringDrawing(e);
                        if (!e.Hand)
                        {
                            //try
                            //{

                            //用这句,有的电脑上会报错  A generic error occurred in GDI+.
                            g.DrawString(str, font, brush, r, sf);

                            //用这句,不报错了,但是放大缩小时,有的文字会不显示,而且参数不一样,估计输出结果可能也不一样
                            //Rectangle r2 = new Rectangle((int)Math.Ceiling(r.Left), (int)Math.Ceiling(r.Top), (int)Math.Ceiling(r.Width), (int)Math.Ceiling(r.Height));
                            //TextRenderer.DrawText(g, str, font, r2, style.FontColor, ff);

                            //}
                            //catch (Exception ex)
                            //{
                            //    I3LocalLogUtil.Current.WriteExceptionLog("drawstring error", ex);
                            //    I3LocalLogUtil.Current.WriteInfoLog(str);
                            //    I3LocalLogUtil.Current.WriteInfoLog(font.ToString());
                            //    I3LocalLogUtil.Current.WriteInfoLog(brush.ToString());
                            //    I3LocalLogUtil.Current.WriteInfoLog(r.ToString());
                            //    I3LocalLogUtil.Current.WriteInfoLog(sf.ToString());
                            //    I3LocalLogUtil.Current.CompleteLog();
                            //}
                        }
                    }
                    #endregion

                    #region 相交区域处理
                    if (rr == RectangleF.Empty)
                    {
                        rr = r;
                    }
                    else
                    {
                        rr.Y = Math.Min(rr.Y, r.Y);                              //y取最小值
                        float yEnd = Math.Max(rr.Y + rr.Height, r.Y + r.Height); //yEnd取最大值
                        rr.Height = yEnd - rr.Y;
                        rr.X      = Math.Min(rr.X, r.X);                         //x取最小值
                        float xEnd = Math.Max(rr.X + rr.Width, r.X + r.Width);   //xEnd取最大值
                        rr.Width = xEnd - rr.X;
                    }
                    #endregion
                }

                return(rr);
            }
            finally
            {
                font.Dispose();
                brush.Dispose();
                sf.Dispose();
            }
        }