예제 #1
0
        /// <summary>
        /// Paint the Text and the Image passed
        /// </summary>
        /// <param name="g">Graphics device where you can render your image and text</param>
        /// <param name="p_displayRectangle">Relative rectangle based on the display area</param>
        /// <param name="p_Image">Image to draw. Can be null.</param>
        /// <param name="p_ImageAlignment">Alignment of the image</param>
        /// <param name="p_ImageStretch">True to make the draw the image with the same size of the cell</param>
        /// <param name="p_Text">Text to draw (can be null)</param>
        /// <param name="p_StringFormat">String format (can be null)</param>
        /// <param name="p_AlignTextToImage">True to align the text with the image</param>
        /// <param name="p_Border">Cell Border</param>
        /// <param name="p_TextColor">Text Color</param>
        /// <param name="p_TextFont">Text Font(can be null)</param>
        /// <param name="p_ExpandedCell">if set to <c>true</c> cell is expanded.</param>
        /// <param name="p_LastExpandedCell">if set to <c>true</c> cell is last expanded cell.</param>
        protected static void PaintImageAndText(Graphics g,
                                                Rectangle p_displayRectangle,
                                                Image p_Image,
                                                ContentAlignment p_ImageAlignment,
                                                bool p_ImageStretch,
                                                string p_Text,
                                                StringFormat p_StringFormat,
                                                bool p_AlignTextToImage,
                                                RectangleBorder p_Border,
                                                Color p_TextColor,
                                                Font p_TextFont,
                                                bool p_ExpandedCell,
                                                bool p_LastExpandedCell)
        {
            // Calculate Rectangle with no border
            Rectangle l_CellRectNoBorder = p_Border.RemoveBorderFromRectangle(p_displayRectangle);

            // Draw image
            if (p_Image != null)
            {
                if (p_ImageStretch) // stretch image
                {
                    g.DrawImage(p_Image, l_CellRectNoBorder);
                }
                else
                {
                    PointF l_PointImage = CalculateObjAlignment(p_ImageAlignment,
                                                                (int)l_CellRectNoBorder.Left, (int)l_CellRectNoBorder.Top,
                                                                (int)l_CellRectNoBorder.Width, (int)l_CellRectNoBorder.Height,
                                                                p_Image.Width, p_Image.Height);

                    RectangleF l_RectDrawImage = l_CellRectNoBorder;
                    l_RectDrawImage.Intersect(new RectangleF(l_PointImage, p_Image.PhysicalDimension));

                    // Truncate the Rectangle for approximation problem
                    g.DrawImage(p_Image, Rectangle.Truncate(l_RectDrawImage));
                }
            }

            // Draw Text
            if (p_Text != null && p_Text.Length > 0)
            {
                if (l_CellRectNoBorder.Width > 0 && l_CellRectNoBorder.Height > 0)
                {
                    RectangleF l_RectDrawText = l_CellRectNoBorder;

                    // Align Text To Image
                    if (p_Image != null && p_ImageStretch == false && p_AlignTextToImage)
                    {
                        if (AlignmentUtility.IsBottom(p_ImageAlignment) && AlignmentUtility.IsBottom(p_StringFormat))
                        {
                            l_RectDrawText.Height -= p_Image.Height;
                        }
                        if (AlignmentUtility.IsTop(p_ImageAlignment) && AlignmentUtility.IsTop(p_StringFormat))
                        {
                            l_RectDrawText.Y      += p_Image.Height;
                            l_RectDrawText.Height -= p_Image.Height;
                        }
                        if (AlignmentUtility.IsLeft(p_ImageAlignment) && AlignmentUtility.IsLeft(p_StringFormat))
                        {
                            l_RectDrawText.X     += p_Image.Width;
                            l_RectDrawText.Width -= p_Image.Width;
                        }
                        if (AlignmentUtility.IsRight(p_ImageAlignment) && AlignmentUtility.IsRight(p_StringFormat))
                        {
                            l_RectDrawText.Width -= p_Image.Width;
                        }
                    }

                    try
                    {
                        SolidBrush textBrush = new SolidBrush(p_TextColor);
                        g.DrawString(p_Text,
                                     p_TextFont,
                                     textBrush,
                                     l_RectDrawText,
                                     p_StringFormat);
                    }
                    catch (Exception ex)
                    { // Very long strings can cause a GDI+ exception here.
                        LoggerManager.Log(LogLevels.Error, "Cell rendering failed unexpectedly because: " + ex.Message);
                    }
                }
            }

            // Draw expanded lines
            if (p_ExpandedCell)
            {
                Pen forePenDark = new Pen(Color.DarkGray, SystemInformation.BorderSize.Height);
                // horizontal line
                Point lineLeft  = new Point(l_CellRectNoBorder.Left + (int)((l_CellRectNoBorder.Right - l_CellRectNoBorder.Left) / 2f), l_CellRectNoBorder.Top + (int)((l_CellRectNoBorder.Bottom - l_CellRectNoBorder.Top) / 2f));
                Point lineRight = new Point(l_CellRectNoBorder.Right, (int)(l_CellRectNoBorder.Top + (l_CellRectNoBorder.Bottom - l_CellRectNoBorder.Top) / 2f));
                Point lineTop   = new Point(l_CellRectNoBorder.Left + (int)((l_CellRectNoBorder.Right - l_CellRectNoBorder.Left) / 2f), l_CellRectNoBorder.Top);
                Point lineBottom;
                if (p_LastExpandedCell == false)
                {
                    lineBottom = new Point(l_CellRectNoBorder.Left + (int)((l_CellRectNoBorder.Right - l_CellRectNoBorder.Left) / 2f), l_CellRectNoBorder.Bottom);
                }
                else
                {
                    lineBottom = new Point(l_CellRectNoBorder.Left + (int)((l_CellRectNoBorder.Right - l_CellRectNoBorder.Left) / 2f), l_CellRectNoBorder.Top + (int)((l_CellRectNoBorder.Bottom - l_CellRectNoBorder.Top) / 2f));
                }
                g.DrawLine(forePenDark, lineTop, lineBottom);
                g.DrawLine(forePenDark, lineLeft, lineRight);
            }
        }
예제 #2
0
        /// <summary>
        /// Returns the minimum required size of the current cell, calculating using the
        /// current DisplayString, Image and Borders information.
        /// </summary>
        /// <param name="p_Graphics">GDI+ drawing surface</param>
        /// <param name="p_DisplayText">The display text.</param>
        /// <param name="p_StringFormat">The string format.</param>
        /// <param name="p_Font">The font.</param>
        /// <param name="p_Image">The image.</param>
        /// <param name="p_ImageAlignment">The image alignment.</param>
        /// <param name="p_AlignTextToImage">if set to <c>true</c> align text to image.</param>
        /// <param name="p_ImageStretch">if set to <c>true</c> image stretch.</param>
        /// <param name="p_Border">The border.</param>
        /// <returns></returns>
        protected static SizeF CalculateRequiredSize(Graphics p_Graphics,
                                                     string p_DisplayText,
                                                     StringFormat p_StringFormat,
                                                     Font p_Font,
                                                     Image p_Image,
                                                     ContentAlignment p_ImageAlignment,
                                                     bool p_AlignTextToImage,
                                                     bool p_ImageStretch,
                                                     RectangleBorder p_Border)
        {
            SizeF l_ReqSize;

            // Calculate Text Size
            if (p_DisplayText != null && p_DisplayText.Length > 0)
            {
                l_ReqSize         = p_Graphics.MeasureString(p_DisplayText, p_Font, MaxStringWidth, p_StringFormat);
                l_ReqSize.Width  += 2; // 2 extra space to always fit the text
                l_ReqSize.Height += 2; // 2 extra space to always fit the text
            }
            else
            {
                l_ReqSize = new SizeF(0, 0);
            }

            // Calculate Image Size
            if (p_Image != null)
            {
                // Check whether align Text To Image
                if (p_ImageStretch == false && p_AlignTextToImage &&
                    p_DisplayText != null && p_DisplayText.Length > 0)
                {
                    if (AlignmentUtility.IsBottom(p_ImageAlignment) && AlignmentUtility.IsBottom(p_StringFormat))
                    {
                        l_ReqSize.Height += p_Image.Height;
                    }
                    else if (AlignmentUtility.IsTop(p_ImageAlignment) && AlignmentUtility.IsTop(p_StringFormat))
                    {
                        l_ReqSize.Height += p_Image.Height;
                    }
                    else // Max between Image and Text
                    {
                        if (p_Image.Height > l_ReqSize.Height)
                        {
                            l_ReqSize.Height = p_Image.Height;
                        }
                    }

                    if (AlignmentUtility.IsLeft(p_ImageAlignment) && AlignmentUtility.IsLeft(p_StringFormat))
                    {
                        l_ReqSize.Width += p_Image.Width;
                    }
                    else if (AlignmentUtility.IsRight(p_ImageAlignment) && AlignmentUtility.IsRight(p_StringFormat))
                    {
                        l_ReqSize.Width += p_Image.Width;
                    }
                    else // Max between Image and Text
                    {
                        if (p_Image.Width > l_ReqSize.Width)
                        {
                            l_ReqSize.Width = p_Image.Width;
                        }
                    }
                }
                else
                {
                    // Max between Image and Text
                    if (p_Image.Height > l_ReqSize.Height)
                    {
                        l_ReqSize.Height = p_Image.Height;
                    }
                    if (p_Image.Width > l_ReqSize.Width)
                    {
                        l_ReqSize.Width = p_Image.Width;
                    }
                }
            }

            // Add Border Width
            l_ReqSize.Width  += p_Border.Left.Width + p_Border.Right.Width;
            l_ReqSize.Height += p_Border.Top.Width + p_Border.Bottom.Width;

            return(l_ReqSize);
        }