System.Windows.Forms.ThemeWin32Classic.DrawListViewItem C# (CSharp) Method

DrawListViewItem() protected method

protected DrawListViewItem ( Graphics dc, ListView control, System.Windows.Forms.ListViewItem item ) : void
dc System.Drawing.Graphics
control ListView
item System.Windows.Forms.ListViewItem
return void
		protected virtual void DrawListViewItem (Graphics dc, ListView control, ListViewItem item)
		{				
			Rectangle rect_checkrect = item.CheckRectReal;
			Rectangle icon_rect = item.GetBounds (ItemBoundsPortion.Icon);
			Rectangle full_rect = item.GetBounds (ItemBoundsPortion.Entire);
			Rectangle text_rect = item.GetBounds (ItemBoundsPortion.Label);			

			// Tile view doesn't support CheckBoxes
			if (control.CheckBoxes && control.View != View.Tile) {
				if (control.StateImageList == null) {
					// Make sure we've got at least a line width of 1
					int check_wd = Math.Max (3, rect_checkrect.Width / 6);
					int scale = Math.Max (1, rect_checkrect.Width / 12);

					// set the checkbox background
					dc.FillRectangle (SystemBrushes.Window,
							  rect_checkrect);
					// define a rectangle inside the border area
					Rectangle rect = new Rectangle (rect_checkrect.X + 2,
									rect_checkrect.Y + 2,
									rect_checkrect.Width - 4,
									rect_checkrect.Height - 4);
					Pen pen = ResPool.GetSizedPen (this.ColorWindowText, 2);
					dc.DrawRectangle (pen, rect);

					// Need to draw a check-mark
					if (item.Checked) {
						Pen check_pen = ResPool.GetSizedPen (this.ColorWindowText, 1);
						// adjustments to get the check-mark at the right place
						rect.X ++; rect.Y ++;
						// following logic is taken from DrawFrameControl method
						int x_offset = rect.Width / 5;
						int y_offset = rect.Height / 3;
						for (int i = 0; i < check_wd; i++) {
							dc.DrawLine (check_pen, rect.Left + x_offset,
								     rect.Top + y_offset + i,
								     rect.Left + x_offset + 2 * scale,
								     rect.Top + y_offset + 2 * scale + i);
							dc.DrawLine (check_pen,
								     rect.Left + x_offset + 2 * scale,
								     rect.Top + y_offset + 2 * scale + i,
								     rect.Left + x_offset + 6 * scale,
								     rect.Top + y_offset - 2 * scale + i);
						}
					}
				}
				else {
					int simage_idx;
					if (item.Checked)
						simage_idx = control.StateImageList.Images.Count > 1 ? 1 : -1;
					else
						simage_idx = control.StateImageList.Images.Count > 0 ? 0 : -1;

					if (simage_idx > -1)
						control.StateImageList.Draw (dc, rect_checkrect.Location, simage_idx);
				}
			}

			ImageList image_list = control.View == View.LargeIcon || control.View == View.Tile ? control.LargeImageList : control.SmallImageList;
			if (image_list != null) {
				int idx;

				if (item.ImageKey != String.Empty)
					idx = image_list.Images.IndexOfKey (item.ImageKey);
				else
					idx = item.ImageIndex;

				if (idx > -1 && idx < image_list.Images.Count)
					image_list.Draw (dc, icon_rect.Location, idx);
			}

			// draw the item text			
			// format for the item text
			StringFormat format = new StringFormat ();
			if (control.View == View.SmallIcon || control.View == View.LargeIcon)
				format.LineAlignment = StringAlignment.Near;
			else
				format.LineAlignment = StringAlignment.Center;
			if (control.View == View.LargeIcon)
				format.Alignment = StringAlignment.Center;
			else
				format.Alignment = StringAlignment.Near;
			
			if (control.LabelWrap && control.View != View.Details && control.View != View.Tile)
				format.FormatFlags = StringFormatFlags.LineLimit;
			else
				format.FormatFlags = StringFormatFlags.NoWrap;

			if ((control.View == View.LargeIcon && !item.Focused) || control.View == View.Details || control.View == View.Tile)
				format.Trimming = StringTrimming.EllipsisCharacter;

			Rectangle highlight_rect = text_rect;
			if (control.View == View.Details) { // Adjustments for Details view
				Size text_size = Size.Ceiling (dc.MeasureString (item.Text, item.Font));

				if (!control.FullRowSelect) // Selection shouldn't be outside the item bounds
					highlight_rect.Width = Math.Min (text_size.Width + 4, text_rect.Width);
			}

			if (item.Selected && control.Focused)
				dc.FillRectangle (SystemBrushes.Highlight, highlight_rect);
			else if (item.Selected && !control.HideSelection)
				dc.FillRectangle (SystemBrushes.Control, highlight_rect);
			else
				dc.FillRectangle (ResPool.GetSolidBrush (item.BackColor), text_rect);
			
			Brush textBrush =
				!control.Enabled ? SystemBrushes.ControlLight :
				(item.Selected && control.Focused) ? SystemBrushes.HighlightText :
				this.ResPool.GetSolidBrush (item.ForeColor);

			// Tile view renders its Text in a different fashion
			if (control.View == View.Tile && Application.VisualStylesEnabled) {
				// Item.Text is drawn using its first subitem's bounds
				dc.DrawString (item.Text, item.Font, textBrush, item.SubItems [0].Bounds, format);

				int count = Math.Min (control.Columns.Count, item.SubItems.Count);
				for (int i = 1; i < count; i++) {
					ListViewItem.ListViewSubItem sub_item = item.SubItems [i];
					if (sub_item.Text == null || sub_item.Text.Length == 0)
						continue;

					Brush itemBrush = item.Selected && control.Focused ? 
						SystemBrushes.HighlightText : GetControlForeBrush (sub_item.ForeColor);
					dc.DrawString (sub_item.Text, sub_item.Font, itemBrush, sub_item.Bounds, format);
				}
			} else
			
			if (item.Text != null && item.Text.Length > 0) {
				Font font = item.Font;

				if (control.HotTracking && item.Hot)
					font = item.HotFont;

				if (item.Selected && control.Focused)
					dc.DrawString (item.Text, font, textBrush, highlight_rect, format);
				else
					dc.DrawString (item.Text, font, textBrush, text_rect, format);
			}

			if (item.Focused && control.Focused) {				
				Rectangle focus_rect = highlight_rect;
				if (control.FullRowSelect && control.View == View.Details) {
					int width = 0;
					foreach (ColumnHeader col in control.Columns)
						width += col.Width;
					focus_rect = new Rectangle (0, full_rect.Y, width, full_rect.Height);
				}
				if (control.ShowFocusCues) {
					if (item.Selected)
						CPDrawFocusRectangle (dc, focus_rect, ColorHighlightText, ColorHighlight);
					else
						CPDrawFocusRectangle (dc, focus_rect, control.ForeColor, control.BackColor);
				}
			}

			format.Dispose ();
		}
ThemeWin32Classic