protected override void OnDrawItem(OwnerDrawnListBox <CallRecord> .DrawItemEventArgs e) { int itemTop = e.Bounds.Top; int itemLeft = e.Bounds.Left + DRAW_OFFSET; int textLeft = itemLeft + DRAW_OFFSET; int textRight = e.Bounds.Right - DRAW_OFFSET; CallRecord data = e.Item; string dateString = data.DueDate.ToShortDateString(); Color dateColor = (data.IsActive) ? Color.Red : e.ForeColor; if (data.IsActive) { if (data.DueDate.Date < DateTime.Now.Date) { dateString = "Overdue"; dateColor = Color.Red; } else if (data.DueDate.Date == DateTime.Now.Date) { dateString = "Today"; dateColor = Color.Red; } else if (data.DueDate.Date == DateTime.Now.AddDays(1).Date) { dateString = "Tomorrow"; dateColor = Color.Red; } else { dateColor = e.ForeColor; } } else { dateString = "Completed"; } dateString = string.Format("{0}", dateString, data.DueDate.ToShortTimeString()); SizeF dateSize = e.Graphics.MeasureString(dateString, DateFont); SizeF titleSize = e.Graphics.MeasureString(data.Name, TitleFont); SizeF notesSize = e.Graphics.MeasureString(data.Description, NotesFont); StringFormat textFormat = new StringFormat(); textFormat.LineAlignment = StringAlignment.Center; using (SolidBrush brush = new SolidBrush(e.ForeColor)) { using (SolidBrush dateBrush = new SolidBrush(dateColor)) { ImageAttributes attrs = new ImageAttributes(); Image img = null; if (this.TileSize != TileSizeMode.Small) { bool dispose = false; img = e.Item.GetImage(new Size(PICTURE_WIDTH, e.Bounds.Height - (DRAW_OFFSET << 1))); if (null == img) { int height = e.Bounds.Height - (DRAW_OFFSET << 1); if (height < EmptyContact.Height) { img = CallRecord.GetImage(EmptyContact, new Size(PICTURE_WIDTH, height)); dispose = true; } else { img = EmptyContact; } attrs.SetColorKey(EmptyContact.GetPixel(0, 0), EmptyContact.GetPixel(0, 0)); } e.Graphics.DrawImage ( img, new Rectangle(e.Bounds.Left + DRAW_OFFSET + ((PICTURE_WIDTH - img.Width) >> 1), e.Bounds.Top + DRAW_OFFSET /* + ((e.Bounds.Height - img.Height - DRAW_OFFSET - DRAW_OFFSET) >> 1)*/, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, attrs ); if (dispose) { img.Dispose(); } textLeft += PICTURE_WIDTH; } Rectangle textRect = new Rectangle(textLeft, itemTop + DRAW_OFFSET, textRight - textLeft, (int)titleSize.Height); Bitmap notImage = (data.WasNotified) ? NotifyOff : NotifyOn; Bitmap priImage = (data.Priority == CallPriority.High) ? HighPriority : LowPriority; attrs.SetColorKey(priImage.GetPixel(0, 0), priImage.GetPixel(0, 0)); int priImgWidth = Math.Min(textRect.Height + DRAW_OFFSET * 2, priImage.Height); e.Graphics.DrawImage ( priImage, new Rectangle(textRect.Right - DRAW_OFFSET - priImgWidth, textRect.Top + ((textRect.Height - priImgWidth) >> 1), priImgWidth, priImgWidth), 0, 0, priImage.Width, priImage.Height, GraphicsUnit.Pixel, attrs ); attrs.SetColorKey(notImage.GetPixel(0, 0), notImage.GetPixel(0, 0)); e.Graphics.DrawImage ( notImage, new Rectangle(textRect.Right - (DRAW_OFFSET << 1) - (priImgWidth << 1), textRect.Top + ((textRect.Height - priImgWidth) >> 1), priImgWidth, priImgWidth), 0, 0, notImage.Width, notImage.Height, GraphicsUnit.Pixel, attrs ); Rectangle dateRect = new Rectangle(textRect.Left, textRect.Top, textRect.Width, textRect.Height); if (this.TileSize == TileSizeMode.Small) { int dateWidth = 0; foreach (string str in new string[] { "Tomorrow ", "Completed ", "Today ", "Overdue " }) { dateWidth = Math.Max(dateWidth, (int)Math.Ceiling(e.Graphics.MeasureString(str, DateFont).Width)); } dateRect = new Rectangle(textRect.Left, textRect.Top, dateWidth, textRect.Height); } // Draw the item e.Graphics.DrawString(dateString, DateFont, dateBrush, dateRect, textFormat); if (this.TileSize == TileSizeMode.Small) { textRect.Location = new Point(dateRect.Right + DRAW_OFFSET, textRect.Top); } else { textRect.Offset(0, (int)dateSize.Height); textRect.Offset(0, DRAW_OFFSET); e.Graphics.DrawLine(new Pen(Color.Black, 1), textLeft, textRect.Top, textRect.Right, textRect.Top); textRect.Offset(0, DRAW_OFFSET); } e.Graphics.DrawString(data.Name, TitleFont, brush, textRect, textFormat); textRect.Offset(0, (int)titleSize.Height); if (this.TileSize == TileSizeMode.Large) { textRect.Height = (e.Bounds.Bottom - textRect.Top); textFormat.LineAlignment = StringAlignment.Near; e.Graphics.DrawString(data.Description, NotesFont, brush, textRect, textFormat); } } } }
protected override void OnPaint(PaintEventArgs e) { // The base class contains a bitmap, offScreen, for constructing // the control and is rendered when all items are populated. // This technique prevents flicker. _screenIndices.Clear(); using (Graphics gOffScreen = Graphics.FromImage(this.OffScreen)) { using (Brush backBrush = new SolidBrush(this.BackColor)) { gOffScreen.FillRectangle(backBrush, this.ClientRectangle); } int itemTop = 0; int itemWidth = this.Width; if (this.VScrollBar.Visible) { itemWidth -= this.VScrollBar.Width; } if (this.FilteredItems.Count == 0) { using (Brush foreBrush = new SolidBrush(this.ForeColor)) { StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Center; format.Alignment = StringAlignment.Center; gOffScreen.DrawString(this.EmptySetText, this.Font, foreBrush, this.ClientRectangle, format); } } else { string groupText = string.Empty; if (this.VScrollBar.Value > 0 && this.UseGrouping) { groupText = this.Grouping.GetGroupText(this.FilteredItems[this.VScrollBar.Value - 1]); } // Draw the fonts in the list. for (int n = this.VScrollBar.Value; n < this.VScrollBar.Value + this.PaintCount + 10 && n < this.FilteredItems.Count; n++) { string currGroupText = string.Empty; if (this.UseGrouping) { currGroupText = this.Grouping.GetGroupText(this.FilteredItems[n]); } if (currGroupText != groupText) { groupText = currGroupText; using (SolidBrush brush = new SolidBrush(this.ForeColor)) { Rectangle rectGroup = new Rectangle(5, itemTop, itemWidth - 5, _groupHeight); StringFormat fmat = new StringFormat(); fmat.LineAlignment = StringAlignment.Center; gOffScreen.DrawString(groupText, _groupingFont, brush, rectGroup, fmat); itemTop += _groupHeight; _screenIndices.Add(rectGroup, -1); } gOffScreen.DrawImage(GroupingBar, new Rectangle(5, itemTop - 4, itemWidth - 5, GroupingBar.Height), new Rectangle(0, 0, GroupingBar.Width, GroupingBar.Height), GraphicsUnit.Pixel); } Color backColor, fontColor; // Draw the selected item to appear highlighted if (n == this.SelectedIndex) { Color highlightColor = Color.FromArgb(0xaa, 0xcd, 0xff); using (SolidBrush highlightBrush = new SolidBrush(highlightColor)) { gOffScreen.FillRectangle(highlightBrush,//SystemColors.Highlight), 1, itemTop + 1, // If the scroll bar is visible, subtract the scrollbar width // otherwise subtract 2 for the width of the rectangle this.ClientSize.Width - (this.VScrollBar.Visible ? this.VScrollBar.Width : 2), this.ItemHeight); backColor = highlightColor; fontColor = CalcTextColor(highlightColor); } } else { backColor = this.BackColor; fontColor = this.ForeColor; } Rectangle bounds = new Rectangle(0, itemTop, itemWidth, this.ItemHeight); DrawItemEventArgs ea = new OwnerDrawnListBox <T> .DrawItemEventArgs(n, this.FilteredItems[n], bounds, gOffScreen, backColor, fontColor); _screenIndices.Add(bounds, n); OnDrawItem(ea); itemTop += this.ItemHeight; if (itemTop > this.Bottom) { break; } } } // Draw the list box e.Graphics.DrawImage(this.OffScreen, 0, 0); } }