/// <summary> /// Draw the specified canvas. /// </summary> /// <param name="canvas">Canvas.</param> /// <param name="rect">Rect.</param> public void DrawCalendar(NGraphics.ICanvas canvas, NGraphics.Rect rect) { _monthYearLabel.Text = _currentMonth.ToString("MMMMM yyyy"); base.Draw(canvas, rect); // Should we use last line? // var drawLastLine = true; var colWidth = rect.Width / 7; var rowHeight = rect.Height / 6; var hasEmptyRow = false; // Find first week of day var firstWeekDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; var date = _currentMonth; while (date.DayOfWeek != firstWeekDay) { date = date.AddDays(-1); } _firstDate = date; // Set up brushes var selectedBrush = new NGraphics.SolidBrush(SelectedBackground.ToNColor()); var regularBrush = new NGraphics.SolidBrush(RegularBackground.ToNColor()); var weekendBrush = new NGraphics.SolidBrush(WeekendBackground.ToNColor()); var notInMonthBrush = new NGraphics.SolidBrush(NotInMonthBackground.ToNColor()); var disabledBrush = new NGraphics.SolidBrush(DisabledBackground.ToNColor()); var col = 0; var row = 0; for (int d = 0; d < _dayNumberLabels.Length; d++) { // Update text var label = _dayNumberLabels[d]; label.XAlign = TextAlignment.Center; label.YAlign = TextAlignment.Center; if (hasEmptyRow || (date.DayOfWeek == firstWeekDay && date >= _currentMonth.AddMonths(1))) { hasEmptyRow = true; label.IsVisible = false; } else { label.IsVisible = true; label.Text = date.Day.ToString(); _lastDate = date; if (HighlightDisabled && (date < MinDate - MinDate.TimeOfDay || date > MaxDate)) { _dayNumberLabels[d].TextColor = DisabledColor; canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, disabledBrush); } else if (IsSameDay(SelectedDate, date)) { // Selected date _dayNumberLabels[d].TextColor = SelectedColor; canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, selectedBrush); } else if (date.Month != _currentMonth.Month) { // Prev/next month _dayNumberLabels[d].TextColor = NotInMonthColor; canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, notInMonthBrush); } else if (date.DayOfWeek == DayOfWeek.Sunday || date.DayOfWeek == DayOfWeek.Saturday) { // Weekends _dayNumberLabels[d].TextColor = WeekendColor; canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, weekendBrush); } else { // Regular days _dayNumberLabels [d].TextColor = RegularColor; canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, regularBrush); } if (IsSameDay(DateTime.Now, date)) { // Today // Background and color if not selected if (!IsSameDay(SelectedDate, date)) { _dayNumberLabels[d].TextColor = TodayColor; var wh = Math.Min(colWidth, rowHeight); var dx = (colWidth - wh) / 2; var dy = (rowHeight - wh) / 2; var rc = new NGraphics.Rect((col * colWidth) + dx, (row * rowHeight) + dy, wh, wh); rc.Inflate(-1, -1); canvas.DrawEllipse(rc, null, new NGraphics.SolidBrush(TodayBackground.ToNColor())); } else { _dayNumberLabels[d].TextColor = TodayBackground; } } } // Col/row-counter col++; if (col == 7) { col = 0; row++; } date = date.AddDays(1); } var colRowPen = new NGraphics.Pen(GridColor.ToNColor(), 1); // Draw row lines for (var r = 0; r < (hasEmptyRow ? 6 : 7); r++) { canvas.DrawPath(new NGraphics.PathOp[] { new NGraphics.MoveTo(0, r * rowHeight), new NGraphics.LineTo(rect.Width, r * rowHeight) }, colRowPen); } // Draw col lines for (var c = 0; c < 7; c++) { canvas.DrawPath(new NGraphics.PathOp[] { new NGraphics.MoveTo(c * colWidth, 0), new NGraphics.LineTo(c * colWidth, hasEmptyRow ? rect.Height - rowHeight : rect.Height) }, colRowPen); } }
/// <summary> /// Draw the specified canvas. /// </summary> /// <param name="canvas">Canvas.</param> /// <param name="rect">Rect.</param> public void DrawCalendar(NGraphics.ICanvas canvas, NGraphics.Rect rect) { base.Draw(canvas, rect); // Should we use last line? // var drawLastLine = true; var colWidth = rect.Width / 7; var rowHeight = rect.Height / 6; // Find first week of day var firstWeekDay = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek; // Find last of this day in previous month var lastDayInPrevMonth = GetLastDayInMonth(SelectedDate.AddMonths(-1)); while (lastDayInPrevMonth.DayOfWeek != firstWeekDay) { lastDayInPrevMonth = lastDayInPrevMonth.AddDays(-1); } // Set up brushes var selectedBrush = new NGraphics.SolidBrush(SelectedDateColor.ToNColor()); var prevNextBrush = new NGraphics.SolidBrush(Color.FromHex("#EEEEEE").ToNColor()); var currentDate = lastDayInPrevMonth; StartDate = lastDayInPrevMonth; var col = 0; var row = 0; for (int d = 0; d < _dayNumberLabels.Length; d++) { // Update text _dayNumberLabels [d].Text = currentDate.Day.ToString(); if (currentDate.Month != SelectedDate.Month) { // Prev/next month _dayNumberLabels [d].TextColor = Color.FromHex("#CECECE"); } else { // Background canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, prevNextBrush); if (currentDate.DayOfWeek == DayOfWeek.Sunday || currentDate.DayOfWeek == DayOfWeek.Saturday) { // Weekends _dayNumberLabels [d].TextColor = Color.FromHex("#CACACA"); } else { // Regular days _dayNumberLabels [d].TextColor = Color.Black; } } if (IsSameDay(SelectedDate, currentDate)) { // Selected colors _dayNumberLabels[d].TextColor = Color.White; // Background canvas.DrawRectangle(new NGraphics.Rect(col * colWidth, row * rowHeight, colWidth, rowHeight), null, selectedBrush); } if (IsSameDay(DateTime.Now, currentDate)) { // Today _dayNumberLabels[d].TextColor = Color.White; // Background var wh = Math.Min(colWidth, rowHeight); var rc = new NGraphics.Rect((col * colWidth), (row * rowHeight), wh, wh); rc.Inflate(-1, -1); rc.X += 3; canvas.DrawEllipse(rc, null, new NGraphics.SolidBrush(Color.FromHex("#fc3d39").ToNColor())); } // Col/row-counter col++; if (col == 7) { col = 0; row++; } currentDate = currentDate.AddDays(1); } var colRowPen = new NGraphics.Pen(Color.FromHex("#FFFFFF").ToNColor(), 1); // Draw row lines for (var r = 0; r < 7; r++) { canvas.DrawPath(new NGraphics.PathOp[] { new NGraphics.MoveTo(0, r * rowHeight), new NGraphics.LineTo(rect.Width, r * rowHeight) }, colRowPen); } // Draw col lines for (var c = 0; c < 7; c++) { canvas.DrawPath(new NGraphics.PathOp[] { new NGraphics.MoveTo(c * colWidth, 0), new NGraphics.LineTo(c * colWidth, rect.Height) }, colRowPen); } }