/// <summary> /// Performs size and positioning layout for the control /// </summary> /// <param name="startDate"></param> private void LayoutYearMonths(DateTime startDate) { Size size = GetPreferredSize(startDate); if (size.Width > 0 && size.Height > 0) { int maxCols = Math.Max(1, (ClientRect.Width - 1) / size.Width); int maxRows = Math.Max(1, (ClientRect.Height - 1) / size.Height); int targetCols = (int)Math.Ceiling(Math.Sqrt(_NumberOfMonths)); if (targetCols >= maxCols) { targetCols = maxCols; } else { while (targetCols < maxCols) { int targetRows = Math.Max(1, (_NumberOfMonths + targetCols - 1) / targetCols); if (targetRows <= maxRows) break; targetCols++; } } _NumberOfRows = Math.Max(1, (_NumberOfMonths + targetCols - 1) / targetCols); _NumberOfCols = (_NumberOfMonths + _NumberOfRows - 1) / _NumberOfRows; int monthHeight = (ClientRect.Height - 1) / _NumberOfRows; int monthWidth = (ClientRect.Width - 1) / _NumberOfCols; int rowSpread = 0; if (monthHeight < size.Height) monthHeight = size.Height; else rowSpread = ClientRect.Height - (_NumberOfRows * monthHeight) - 1; int colSpread = 0; if (monthWidth < size.Width) monthWidth = size.Width; else colSpread = ClientRect.Width - (_NumberOfCols * monthWidth) - 1; // Loop through each week int n = 0; DateTime date = startDate; Rectangle r = new Rectangle(ClientRect.X, ClientRect.Y, monthWidth, monthHeight); r.Y += _VScrollPos; for (int i = 0; i < _NumberOfRows; i++) { if (i < rowSpread) r.Height++; for (int j = 0; j < _NumberOfCols; j++) { if (n >= _NumberOfMonths) break; if (_YearMonths[n] == null) _YearMonths[n] = new YearMonth(this, date); else _YearMonths[n].StartDate = date; // Calculate the bounding rect limits r.Width = monthWidth; if (j < colSpread) r.Width++; // Set the bounding rect _YearMonths[n].Bounds = r; r.X += r.Width; n++; date = date.AddMonths(1); } r.X = ClientRect.X; r.Y += r.Height; r.Height = monthHeight; } CalendarView.YearViewMax = (r.Y - (ClientRect.Y + _VScrollPos)); } }
/// <summary> /// Gets the preferred size of the control /// </summary> /// <param name="startDate"></param> /// <returns></returns> private Size GetPreferredSize(DateTime startDate) { if (_PreferredSize.IsEmpty) { if (_YearMonths[0] == null) _YearMonths[0] = new YearMonth(this, startDate); _PreferredSize = _YearMonths[0].GetPreferredSize(); } return (_PreferredSize); }
/// <summary> /// UpdateCustomItems /// </summary> /// <param name="yearMonth"></param> private void UpdateCustomItems(YearMonth yearMonth) { CustomCalendarItemCollection items = _View.CalendarView.CustomItems; if (items != null && items.Count > 0) { for (int i = 0; i < items.Count; i++) { CustomCalendarItem item = items[i]; if (IsCustomItemVisible(item) == true && (item.StartTime.Year == yearMonth.StartDate.Year && item.StartTime.Month == yearMonth.StartDate.Month)) { yearMonth.AppBits.Set(item.StartTime.Day - 1, true); break; } } } }