protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // Setup drawing area var viewRect = GetViewRect(); if (viewRect.X != 0 || viewRect.Y != 0) { // Set the graphics transform and reset the view rectangle, // so the whole drawing is based on (0,0) point e.Graphics.TranslateTransform(viewRect.X, viewRect.Y); viewRect.X = viewRect.Y = 0; } // Draw background using (var brush = new SolidBrush(CellBackColor)) e.Graphics.FillRectangle(brush, viewRect); // Draw cells if (source != null) { using (var userBrush = new SolidBrush(UserCellForeColor)) using (var fixedBrush = new SolidBrush(FixedCellForeColor)) using (var format = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }) { var font = this.Font; for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { var value = source.GetCellValue(row, col); if (value == null) { continue; } var text = value.ToString(); var brush = !source.IsReady || source.IsCellFixed(row, col) ? fixedBrush : userBrush; var rect = GetBaseCellRect(row, col); e.Graphics.DrawString(text, font, brush, rect, format); } } } } // Draw grid using (var thinPen = new Pen(CellBorderColor, 1)) using (var thickPen = new Pen(CellBorderColor, 3)) { // Draw grid lines for (int i = 1; i < 9; i++) { var pen = (i % 3) == 0 ? thickPen : thinPen; int pos = i * CellSize; // Draw horizontal line at x = pos e.Graphics.DrawLine(pen, 0, pos, viewRect.Right, pos); // Draw vertical line at y = pos e.Graphics.DrawLine(pen, pos, 0, pos, viewRect.Bottom); } // Draw grid border e.Graphics.DrawRectangle(thickPen, viewRect); } // Draw selection rectangle using (var pen = new Pen(SelectedCellBorderColor, 2)) { var cellRect = GetBaseCellRect(selectedRow, selectedCol); cellRect.Inflate(-1, -1); e.Graphics.DrawRectangle(pen, cellRect); } }
public override bool CanExecute(PuzzleViewModel target) => oldValue != newValue && (!target.IsReady || !target.IsCellFixed(cell));