/// <summary>Gets the font to use for rendering.</summary>
 /// <returns>The font to use for rendering.</returns>
 private Font GetFontForText()
 {
     if (!_autosizeFont)
     {
         return(base.Font);
     }
     if (_font == null)
     {
         Font baseFont = base.Font;
         if (Text == null || Text.Length == 0)
         {
             return(baseFont);
         }
         using (Graphics g = CreateGraphics())
         {
             int width  = Width * 9 / 10;
             int height = Height * 9 / 10;
             _font = new Font(baseFont.FontFamily,
                              GraphicsHelpers.GetMaximumEMSize(Text, g, baseFont.FontFamily, baseFont.Style, width, height), baseFont.Style);
         }
     }
     return(_font);
 }
        /// <summary>Raises the Paint event.</summary>
        /// <param name="pe">A PaintEventArgs that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            // Do base painting
            base.OnPaint(pe);

            // Draw the base underlying image
            if (_underImage != null)
            {
                pe.Graphics.DrawImage(_underImage, 0, 0);
            }

            // Render all of the sprites
            if (_sprites != null && _sprites.Count > 0)
            {
                for (int i = _sprites.Count - 1; i >= 0; --i)
                {
                    ImageSprite s = _sprites[i];
                    s.Paint(pe.Graphics);
                }
            }

            // Show the congratulatory text
            string text = ResourceHelper.PuzzleSolvedCongratulations;

            if (_sf != null && text != null && text.Length > 0)
            {
                float emSize = GraphicsHelpers.GetMaximumEMSize(text,
                                                                pe.Graphics, Font.FontFamily, Font.Style, ClientRectangle.Width, ClientRectangle.Height);
                using (Font f = new Font(Font.FontFamily, emSize))
                {
                    pe.Graphics.DrawString(text, f, Brushes.Black, new RectangleF(2, 2, ClientRectangle.Width, ClientRectangle.Height), _sf);
                    pe.Graphics.DrawString(text, f, Brushes.Gray, new RectangleF(-1, -1, ClientRectangle.Width, ClientRectangle.Height), _sf);
                    pe.Graphics.DrawString(text, f, Brushes.Yellow, new RectangleF(0, 0, ClientRectangle.Width, ClientRectangle.Height), _sf);
                }
            }
        }
Beispiel #3
0
        /// <summary>Draws the playing grid onto the specified graphics object and within the specified bounding rectangle.</summary>
        /// <param name="graphics">The graphics object onto</param>
        public void DrawToGraphics(Graphics graphics, Rectangle clipRectangle)
        {
            if (graphics == null)
            {
                throw new ArgumentNullException("graphics");
            }
            Rectangle rect = BoardRectangle;

            // Draw the underlying board images
            graphics.DrawImage(ResourceHelper.BoardBackgroundImage, 0, 0, Width, Height);
            graphics.DrawImage(ResourceHelper.BoardImage, rect);

            // Precompute some important sizes, such as the width and height of
            // a cell in the grid, positioning information for drawing possible numbers,
            // and the em size to use for drawing text.
            RectangleF genericCellRect = GetCellRectangle(rect, new Point(0, 0));
            float      cellWidth       = genericCellRect.Width;
            float      cellHeight      = genericCellRect.Height;

            // Get the em size for the current font based on the current board size.
            // This is cached, and the cache is only cleared when the board is resized or when the font is changed.
            float emSize;

            if (_cachedEmSize < 0)
            {
                _cachedEmSize = GraphicsHelpers.GetMaximumEMSize(ResourceHelper.FontSizingString, graphics, this.Font.FontFamily, FontStyle.Bold, cellWidth, cellHeight);
            }
            emSize = _cachedEmSize;

            bool showSuggestedCells = ShowSuggestedCells;

            // Draw cell images
            using (Font setNumberFont = new Font(this.Font.FontFamily, emSize, FontStyle.Bold))
            {
                for (int i = 0; i < State.GridSize; i++)
                {
                    for (int j = 0; j < State.GridSize; j++)
                    {
                        RectangleF cellRect = GetCellRectangle(rect, new Point(i, j));
                        if (clipRectangle.IntersectsWith(Rectangle.Ceiling(cellRect)))
                        {
                            if (State.Status != PuzzleStatus.Solved)
                            {
                                if (_selectedCell.HasValue && _selectedCell.Value.X == i && _selectedCell.Value.Y == j)
                                {
                                    Image selectedCellImage;
                                    if (i == 0 && j == 0)
                                    {
                                        selectedCellImage = ResourceHelper.CellActiveUpperLeft;
                                    }
                                    else if (i == 0 && j == State.GridSize - 1)
                                    {
                                        selectedCellImage = ResourceHelper.CellActiveUpperRight;
                                    }
                                    else if (i == State.GridSize - 1 && j == 0)
                                    {
                                        selectedCellImage = ResourceHelper.CellActiveLowerLeft;
                                    }
                                    else if (i == State.GridSize - 1 && j == State.GridSize - 1)
                                    {
                                        selectedCellImage = ResourceHelper.CellActiveLowerRight;
                                    }
                                    else
                                    {
                                        selectedCellImage = ResourceHelper.CellActiveSquare;
                                    }
                                    graphics.DrawImage(selectedCellImage, cellRect.X, cellRect.Y, cellRect.Width, cellRect.Height);
                                }
                                else if (showSuggestedCells && SuggestedCell.HasValue &&
                                         SuggestedCell.Value.X == i && SuggestedCell.Value.Y == j)
                                {
                                    Image suggestedCellImage;
                                    if (i == 0 && j == 0)
                                    {
                                        suggestedCellImage = ResourceHelper.CellHintUpperLeft;
                                    }
                                    else if (i == 0 && j == State.GridSize - 1)
                                    {
                                        suggestedCellImage = ResourceHelper.CellHintUpperRight;
                                    }
                                    else if (i == State.GridSize - 1 && j == 0)
                                    {
                                        suggestedCellImage = ResourceHelper.CellHintLowerLeft;
                                    }
                                    else if (i == State.GridSize - 1 && j == State.GridSize - 1)
                                    {
                                        suggestedCellImage = ResourceHelper.CellHintLowerRight;
                                    }
                                    else
                                    {
                                        suggestedCellImage = ResourceHelper.CellHintSquare;
                                    }
                                    graphics.DrawImage(suggestedCellImage, cellRect.X, cellRect.Y, cellRect.Width, cellRect.Height);
                                }
                            }

                            // If a cell has a value, then draw that value
                            if (State[i, j].HasValue)
                            {
                                Brush b;
                                if (ShowIncorrectNumbers &&
                                    State[i, j].HasValue && _solvedOriginalState != null &&
                                    State[i, j].Value != _solvedOriginalState[i, j].Value)
                                {
                                    b = _incorrectValueBrush;
                                }
                                else if (_originalState != null && _originalState[i, j].HasValue)
                                {
                                    b = _originalValueBrush;
                                }
                                else
                                {
                                    b = _userValueBrush;
                                }

                                graphics.DrawString((State[i, j] + 1).Value.ToString(CultureInfo.InvariantCulture), setNumberFont, b,
                                                    cellRect, _centerNumberFormat);
                            }
                        }
                    }
                }
            }
        }