protected Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { Rectangle subItemRect = Rectangle.Empty; if (Item == null) throw new ArgumentNullException("Item"); int[] order = GetColumnOrder(); if (order == null) // No Columns return subItemRect; if (SubItem >= order.Length) throw new IndexOutOfRangeException("SubItem "+SubItem+" out of range"); // Retrieve the bounds of the entire ListViewItem (all subitems) Rectangle lviBounds = Item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; // Calculate the X position of the SubItem. // Because the columns can be reordered we have to use Columns[order[i]] instead of Columns[i] ! ColumnHeader col; int i; for (i=0; i<order.Length; i++) { col = this.Columns[order[i]]; if (col.Index == SubItem) break; subItemX += col.Width; } subItemRect = new Rectangle(subItemX, lviBounds.Top, this.Columns[order[i]].Width, lviBounds.Height); return subItemRect; }
public Rectangle GetSubItemBounds(ListViewItem lvi, int subItemIndex) { Rectangle lviBounds = lvi.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; for (int i = 0; i < GetNameColumnIndex(); i++) subItemX += this.listView.Columns[i].Width; Rectangle rect = new Rectangle(); rect.Y = lviBounds.Top; rect.X = subItemX + lvi.GetBounds(ItemBoundsPortion.Icon).Width + 4; // magic number for making the textbox appear just to the right of the icon rect.Size = new Size(GetSelectedItemNameTextWidth(), lvi.GetBounds(ItemBoundsPortion.Entire).Height); return rect; }
private void listAddField_MouseDoubleClick(object sender, MouseEventArgs e) { System.Windows.Forms.ListViewItem item = listAddField.GetItemAt(e.X, e.Y); if (item == null) { return; } System.Drawing.Rectangle rec = item.GetBounds(System.Windows.Forms.ItemBoundsPortion.Entire); if (e.X <= listAddField.Columns[0].Width) { //·ûºÅ±à¼ m_EditObject = item; Form.frmSymbolEdit frm = new GeoSymbology.Form.frmSymbolEdit(this, item.Tag as ISymbol, ""); frm.ShowDialog(); } }
/// <summary> /// Retrieve the bounds of a ListViewSubItem /// </summary> /// <param name="Item">The Item containing the SubItem</param> /// <param name="SubItem">Index of the SubItem</param> /// <returns>Subitem's bounds</returns> protected Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { Rectangle subItemRect = Rectangle.Empty; if (Item == null) { throw new ArgumentNullException("Item"); } int[] order = GetColumnOrder(); if (order == null) // No Columns { return(subItemRect); } if (SubItem >= order.Length) { throw new IndexOutOfRangeException("SubItem " + SubItem + " out of range"); } // Retrieve the bounds of the entire ListViewItem (all subitems) Rectangle lviBounds = Item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; // Calculate the X position of the SubItem. // Because the columns can be reordered we have to use Columns[order[i]] instead of Columns[i] ! ColumnHeader col; int i; for (i = 0; i < order.Length; i++) { col = this.Columns[order[i]]; if (col.Index == SubItem) { break; } subItemX += col.Width; } subItemRect = new Rectangle(subItemX + 1, lviBounds.Top + 1, this.Columns[order[i]].Width - 2, lviBounds.Height - 1); return(subItemRect); }
private void DoListViewMouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e) { DevComponents.DotNetBar.Controls.ListViewEx listView = sender as DevComponents.DotNetBar.Controls.ListViewEx; System.Windows.Forms.ListViewItem item = null; if (item == null) { return; } switch (listView.Name) { case "listAddField": { item = listAddField.GetItemAt(e.X, e.Y); System.Drawing.Rectangle rec = item.GetBounds(System.Windows.Forms.ItemBoundsPortion.Entire); if (e.X <= listAddField.Columns[0].Width) { m_EditObject = item; Form.frmSymbolEdit frm = new GeoSymbology.Form.frmSymbolEdit(this, item.Tag as ISymbol, ""); frm.ShowDialog(); } else if (e.X > listAddField.Columns[0].Width && e.X <= listAddField.Columns[0].Width + listAddField.Columns[1].Width) { listAddField.Items.Remove(item); item.ImageKey = ""; listAllField.Items.Add(item); } } break; case "listAllField": { item = listAllField.GetItemAt(e.X, e.Y); listAllField.Items.Remove(item); listAddField.Items.Add(item); listAddField.SmallImageList.Images.Add(item.Name.Replace("Item", "Symbol"), ModuleCommon.Symbol2Picture(item.Tag as ISymbol, ModuleCommon.ImageWidth, ModuleCommon.ImageHeight)); item.ImageKey = item.Name.Replace("Item", "Symbol"); } break; } }
public int GetSubItemAt(int x, int y, out ListViewItem item) { item = base.GetItemAt(x, y); if (item != null) { int[] columnOrder = this.GetColumnOrder(); int left = item.GetBounds(ItemBoundsPortion.Entire).Left; for (int i = 0; i < columnOrder.Length; i++) { ColumnHeader header = base.Columns[columnOrder[i]]; if (x < (left + header.Width)) { return header.Index; } left += header.Width; } } return -1; }
int GetCellNo( int x, int y ) { //System.Windows.Forms.ListViewItem.ListViewSubItem Cell = new System.Windows.Forms.ListViewItem.ListViewSubItem(); System.Windows.Forms.ListViewItem Item = new ListViewItem(); // 获得单元格所在的行。 Item = GetItemAt( x, y ); if ( Item == null ) return -1; //cell.ItemIndex = cell.Item.Index; // 现在 Item.Index 还能用 // 根据各列宽度,获得单元格所在的列,以及 Bounds。 int curX = Item.GetBounds( ItemBoundsPortion.Entire ).Left; // 依次循环各列,表示各列的起点值 int scrollLeft = GetScrollPos( Handle.ToInt32(), 0 ); // 可能出现了横向滚动条,左边隐藏起来的宽度 for ( int i = 0 ; i < Columns.Count ; i++ ) { if ( scrollLeft + x >= curX && scrollLeft + x < curX + Columns[ i ].Width ) { //ListViewColumn = Columns[ i ]; // 列找到了 //Rectangle itemBounds = cell.Item.GetBounds( ItemBoundsPortion.Entire ); //cell.Bounds = new Rectangle( curX, // itemBounds.Y, // Columns[ i ].Width, // itemBounds.Height ); //break; return i; } curX += Columns[ i ].Width; } return -1; }
private void StartCellEdit(ListViewItem item) { var eventArgs = new ListViewCellCancelEventArgs(m_currentRow, m_currentCol); OnCellBeginEdit(eventArgs); if (eventArgs.Cancel) return; Rectangle itemBound = item.GetBounds(ItemBoundsPortion.Entire); int cellLeft = itemBound.Left; for (int i = 0; i < m_currentCol; ++i) cellLeft += Columns[i].Width; int cellWidth = Columns[m_currentCol].Width; if (m_currentCol == 0 && CheckBoxes) { cellLeft += CheckBoxWidth ; cellWidth -= CheckBoxWidth; } cellLeft += TextOffset; cellWidth -=TextOffset; Rectangle cellBound = new Rectangle(cellLeft, itemBound.Y, cellWidth, itemBound.Height); if (m_propertyDescriptors[m_currentCol].PropertyType.IsEnum) { m_comboBox.DataSource = Enum.GetValues(m_propertyDescriptors[m_currentCol].PropertyType); m_comboBox.Bounds = cellBound; m_activeEditingControl = m_comboBox; } else { m_textBox.Bounds = cellBound; m_activeEditingControl = m_textBox; } EnableEditingControl(); }
// draws the ListViewItem of the given index protected override void DrawListViewItem( Graphics dc, ListView control, ListViewItem item ) { Rectangle rect_checkrect = item.CheckRectReal; Rectangle rect_iconrect = item.GetBounds( ItemBoundsPortion.Icon ); Rectangle full_rect = item.GetBounds( ItemBoundsPortion.Entire ); Rectangle text_rect = item.GetBounds( ItemBoundsPortion.Label ); if ( control.CheckBoxes ) { 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( this.ResPool.GetSolidBrush( this.ColorWindow ), 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 = new Pen( this.ColorWindowText, 2 ); dc.DrawRectangle( pen, rect ); // Need to draw a check-mark if ( item.Checked ) { pen.Width = 1; // adjustments to get the check-mark at the right place rect.X ++; rect.Y ++; // following logic is taken from DrawFrameControl method for ( int i = 0; i < check_wd; i++ ) { dc.DrawLine( pen, rect.Left + check_wd / 2, rect.Top + check_wd + i, rect.Left + check_wd / 2 + 2 * scale, rect.Top + check_wd + 2 * scale + i ); dc.DrawLine( pen, rect.Left + check_wd / 2 + 2 * scale, rect.Top + check_wd + 2 * scale + i, rect.Left + check_wd / 2 + 6 * scale, rect.Top + check_wd - 2 * scale + i ); } } } else { if ( item.Checked && control.StateImageList.Images.Count > 1 ) control.StateImageList.Draw( dc, rect_checkrect.Location, 1 ); else if ( ! item.Checked && control.StateImageList.Images.Count > 0 ) control.StateImageList.Draw( dc, rect_checkrect.Location, 0 ); } } // Item is drawn as a special case, as it is not just text if ( control.View == View.LargeIcon ) { if ( item.ImageIndex > -1 && control.LargeImageList != null && item.ImageIndex < control.LargeImageList.Images.Count ) { // center image Point image_location = rect_iconrect.Location; Image image = control.LargeImageList.Images[ item.ImageIndex ]; if ( image.Width < rect_iconrect.Width ) { int icon_rect_middle = rect_iconrect.Width / 2; int image_middle = image.Width / 2; image_location.X = image_location.X + icon_rect_middle - image_middle; } control.LargeImageList.Draw( dc, image_location, item.ImageIndex ); } } else { if ( item.ImageIndex > -1 && control.SmallImageList != null && item.ImageIndex < control.SmallImageList.Images.Count ) control.SmallImageList.Draw( dc, rect_iconrect.Location, item.ImageIndex ); } // draw the item text // format for the item text StringFormat format = new StringFormat( ); format.LineAlignment = StringAlignment.Center; if ( control.View == View.LargeIcon ) format.Alignment = StringAlignment.Center; else format.Alignment = StringAlignment.Near; if ( !control.LabelWrap ) format.FormatFlags = StringFormatFlags.NoWrap; if ( item.Selected ) { if ( control.View == View.Details ) { if ( control.FullRowSelect ) { // fill the entire rect excluding the checkbox full_rect.Location = item.GetBounds (ItemBoundsPortion.Label).Location; dc.FillRectangle( this.ResPool.GetSolidBrush ( this.ColorHighlight ), full_rect ); } else { Size text_size = Size.Ceiling( dc.MeasureString( item.Text, item.Font ) ); text_rect.Width = text_size.Width; dc.FillRectangle( this.ResPool.GetSolidBrush ( this.ColorHighlight ), text_rect ); } } else { /*Size text_size = Size.Ceiling (dc.MeasureString (item.Text, item.Font)); Point loc = text_rect.Location; loc.X += (text_rect.Width - text_size.Width) / 2; text_rect.Width = text_size.Width;*/ dc.FillRectangle( this.ResPool.GetSolidBrush( this.ColorHighlight ), text_rect ); } } else dc.FillRectangle( ResPool.GetSolidBrush( item.BackColor ), text_rect ); if ( item.Text != null && item.Text.Length > 0 ) { if ( control.View != View.LargeIcon ) { if ( item.Selected ) dc.DrawString( item.Text, item.Font, this.ResPool.GetSolidBrush ( this.ColorHighlightText ), text_rect, format ); else dc.DrawString( item.Text, item.Font, this.ResPool.GetSolidBrush ( item.ForeColor ), text_rect, format ); } else { // ListView CalcTextSize says wrapping is done for two lines only !?! // text is centered for the complete text_rect but it should be centered per available row/line // calculate how much lines we get out of text_rect and current item.Font int nr_lines = text_rect.Height / item.Font.Height; int rest = text_rect.Height % item.Font.Height; int line_height = item.Font.Height + ( rest > 1 ? 2 : 0 ); Rectangle[] text_rects = new Rectangle[ nr_lines ]; for ( int i = 0; i < nr_lines; i++ ) { text_rects[ i ].X = text_rect.X; text_rects[ i ].Y = text_rect.Y + i * line_height; text_rects[ i ].Width = text_rect.Width; text_rects[ i ].Height = line_height; } string[] lines = new string[ nr_lines ]; string text = item.Text; int line_nr = 0; int current_pos = 0; for ( int k = 1; k <= text.Length; k++ ) { lines[ line_nr ] = text.Substring( current_pos, k - current_pos ); // FIXME: Graphics.MeasureString returns wrong results if there is a // space char in the string SizeF sizef = dc.MeasureString( lines[ line_nr ], item.Font, text_rect.Width, format ); if ( (int)sizef.Width > text_rect.Width - 3 ) { lines[ line_nr ] = lines[ line_nr ].Remove( lines[ line_nr ].Length - 1, 1 ); k--; current_pos = k; line_nr++; if ( line_nr == nr_lines ) break; } } int j = 0; foreach ( Rectangle t_rect in text_rects ) { if ( item.Selected ) dc.DrawString( lines[ j ], item.Font, this.ResPool.GetSolidBrush ( this.ColorHighlightText ), t_rect, format ); else dc.DrawString( lines[ j ], item.Font, this.ResPool.GetSolidBrush ( item.ForeColor ), t_rect, format ); j++; } } } if ( control.View == View.Details && control.Columns.Count > 0 ) { // draw subitems for details view ListViewItem.ListViewSubItemCollection subItems = item.SubItems; int count = ( control.Columns.Count < subItems.Count ? control.Columns.Count : subItems.Count ); if ( count > 0 ) { ColumnHeader col; ListViewItem.ListViewSubItem subItem; Rectangle sub_item_rect = text_rect; // set the format for subitems format.FormatFlags = StringFormatFlags.NoWrap; format.Alignment = StringAlignment.Near; // 0th subitem is the item already drawn for ( int index = 1; index < count; index++ ) { subItem = subItems[ index ]; col = control.Columns[ index ]; sub_item_rect.X = col.Rect.Left; sub_item_rect.Width = col.Wd; sub_item_rect.X -= control.h_marker; SolidBrush sub_item_back_br = null; SolidBrush sub_item_fore_br = null; Font sub_item_font = null; if ( item.UseItemStyleForSubItems ) { sub_item_back_br = this.ResPool.GetSolidBrush ( item.BackColor ); sub_item_fore_br = this.ResPool.GetSolidBrush ( item.ForeColor ); sub_item_font = item.Font; } else { sub_item_back_br = this.ResPool.GetSolidBrush ( subItem.BackColor ); sub_item_fore_br = this.ResPool.GetSolidBrush ( subItem.ForeColor ); sub_item_font = subItem.Font; } // In case of fullrowselect, background is filled // for the entire rect above if ( item.Selected && control.FullRowSelect ) { if ( subItem.Text != null && subItem.Text.Length > 0 ) dc.DrawString( subItem.Text, sub_item_font, this.ResPool.GetSolidBrush ( this.ColorHighlightText ), sub_item_rect, format ); } else { dc.FillRectangle( sub_item_back_br, sub_item_rect ); if ( subItem.Text != null && subItem.Text.Length > 0 ) dc.DrawString( subItem.Text, sub_item_font, sub_item_fore_br, sub_item_rect, format ); } sub_item_rect.X += col.Wd; } } } if ( item.Focused ) { if ( item.Selected ) CPDrawFocusRectangle( dc, text_rect, ColorHighlightText, ColorHighlight ); else CPDrawFocusRectangle( dc, text_rect, control.ForeColor, control.BackColor ); } format.Dispose( ); }
private void DrawItemBackground(Graphics g, Rectangle r, ListViewItem item, ListViewItemStates state) { if (!this.Enabled) return; // Bug fix in ListView for this specific state if (state == 0 && item.Selected && this.View == View.Details && this.FullRowSelect && this.Focused) r.X++; if (!item.BackColor.IsEmpty) { using (SolidBrush brush = new SolidBrush(item.BackColor)) { g.FillRectangle(brush, r); } } bool selected = IsInState(state, ListViewItemStates.Selected) || state == 0 && item.Selected; if (selected) { Office2007ListViewColorTable ct = GetColorTable(); // Draw the background for selected item. r.Height--; r.Width--; using (Pen pen = new Pen(ct.SelectionBorder)) g.DrawRectangle(pen, r); r.Height++; r.Width++; Rectangle ir = new Rectangle(r.X, r.Y + 1, r.Width, r.Height - 2); DisplayHelp.FillRectangle(g, ir, ct.SelectionBackground); } else if (IsInState(state, ListViewItemStates.Hot) && this.HotTracking) { Office2007Renderer rnd = this.GetRenderer() as Office2007Renderer; Office2007ButtonItemPainter.PaintBackground(g, rnd.ColorTable.ButtonItemColors[0].MouseOver, r, RoundRectangleShapeDescriptor.RectangleShape); } if (IsInState(state, ListViewItemStates.Focused) && (!IsInState(state, ListViewItemStates.Hot) && this.View != View.LargeIcon || selected)) { Rectangle rFocus = item.Bounds; if (this.View == View.Details && !this.FullRowSelect || this.View == View.List) rFocus = item.GetBounds(ItemBoundsPortion.Label); else if (this.View == View.Details && this.FullRowSelect) rFocus = r; else if (this.View == View.SmallIcon) rFocus = r; if (selected) { rFocus.Y++; rFocus.Height -= 2; } DrawFocusRectangle(g, rFocus, item); } else if (selected && this.View == View.Details && this.FullRowSelect && this.Focused) // Bug fix in ListView for this specific state { Rectangle rFocus = r; rFocus.Y++; rFocus.Height -= 2; Region oldClip = g.Clip; Rectangle rClip = rFocus; rClip.Width--; g.SetClip(rClip); DrawFocusRectangle(g, rFocus, item); g.Clip = oldClip; } }
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 (); }
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); #if NET_2_0 // Tile view doesn't support CheckBoxes if (control.CheckBoxes && control.View != View.Tile) { #else if (control.CheckBoxes) { #endif 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) #if NET_2_0 simage_idx = control.StateImageList.Images.Count > 1 ? 1 : -1; #else simage_idx = control.StateImageList.Images.Count > 1 ? 1 : 0; #endif else simage_idx = control.StateImageList.Images.Count > 0 ? 0 : -1; if (simage_idx > -1) control.StateImageList.Draw (dc, rect_checkrect.Location, simage_idx); } }
private void DrawImage( Graphics g, ListViewItem item, bool selected) { ImageList imageList = item.ImageList; Size imageSize = imageList.ImageSize; Rectangle imageRect = item.GetBounds(ItemBoundsPortion.Icon); if (imageRect.Width > imageSize.Width) { imageRect.X += (imageRect.Width - imageSize.Width) / 2; imageRect.Width = imageSize.Width; } if (imageRect.Height > imageSize.Height) { imageRect.Y += (imageRect.Height - imageSize.Height) / 2; imageRect.Height = imageSize.Height; } int imageIndex = item.ImageIndex != -1 ? item.ImageIndex : imageList.Images.IndexOfKey(item.ImageKey); if (selected) { IntPtr hIcon = NativeMethods.ImageList_GetIcon( imageList.Handle, imageIndex, (int)NativeMethods.ImageListDrawFlags.ILD_SELECTED); g.DrawIcon(Icon.FromHandle(hIcon), imageRect); NativeMethods.DestroyIcon(hIcon); } else { Image image = imageList.Images[imageIndex]; g.DrawImage( image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } }
internal void BeginEdit (ListViewItem item) { if (edit_item != null) EndEdit (edit_item); if (edit_text_box == null) { edit_text_box = new ListViewLabelEditTextBox (); edit_text_box.BorderStyle = BorderStyle.FixedSingle; edit_text_box.EditingCancelled += new EventHandler (LabelEditCancelled); edit_text_box.EditingFinished += new EventHandler (LabelEditFinished); edit_text_box.TextChanged += new EventHandler (LabelTextChanged); edit_text_box.Visible = false; Controls.Add (edit_text_box); } item.EnsureVisible(); edit_text_box.Reset (); switch (owner.view) { case View.List: case View.SmallIcon: case View.Details: edit_text_box.TextAlign = HorizontalAlignment.Left; edit_text_box.Bounds = item.GetBounds (ItemBoundsPortion.Label); SizeF sizef = TextRenderer.MeasureString (item.Text, item.Font); edit_text_box.Width = (int)sizef.Width + 4; edit_text_box.MaxWidth = owner.ClientRectangle.Width - edit_text_box.Bounds.X; edit_text_box.WordWrap = false; edit_text_box.Multiline = false; break; case View.LargeIcon: edit_text_box.TextAlign = HorizontalAlignment.Center; edit_text_box.Bounds = item.GetBounds (ItemBoundsPortion.Label); sizef = TextRenderer.MeasureString (item.Text, item.Font); edit_text_box.Width = (int)sizef.Width + 4; edit_text_box.MaxWidth = item.GetBounds(ItemBoundsPortion.Entire).Width; edit_text_box.MaxHeight = owner.ClientRectangle.Height - edit_text_box.Bounds.Y; edit_text_box.WordWrap = true; edit_text_box.Multiline = true; break; } edit_item = item; edit_text_box.Text = item.Text; edit_text_box.Font = item.Font; edit_text_box.Visible = true; edit_text_box.Focus (); edit_text_box.SelectAll (); edit_args = new LabelEditEventArgs (owner.Items.IndexOf (edit_item)); owner.OnBeforeLabelEdit (edit_args); if (edit_args.CancelEdit) EndEdit (item); }
private void DrawListViewItem(Graphics g, ListViewItem item, ColumnHeader header, Font font, Color color, ListViewItemStates state) { bool hasImage = HasImage(item); // Draw Image if any if (hasImage && (header == null || header.Width > 4)) { Rectangle rImage = item.GetBounds(ItemBoundsPortion.Icon); int index = item.ImageIndex; if (index < 0) index = item.ImageList.Images.IndexOfKey(item.ImageKey); if (this.View != View.Details && this.View != View.List && this.StateImageList != null) rImage.X += this.StateImageList.ImageSize.Width + 3; else if (this.View == View.SmallIcon && this.CheckBoxes && this.Groups.Count == 0) rImage.X += this.SmallImageList.ImageSize.Width; else if (this.View == View.LargeIcon && (item.ImageList.ImageSize.Width < rImage.Width || item.ImageList.ImageSize.Height < rImage.Height)) { if (item.ImageList.ImageSize.Width < rImage.Width) rImage.X += (rImage.Width - item.ImageList.ImageSize.Width) / 2; if (item.ImageList.ImageSize.Height < rImage.Height) rImage.Y += (rImage.Height - item.ImageList.ImageSize.Height) / 2; } Region oldClip = null; if (header != null && header.Width < rImage.Width) { Rectangle rClip = rImage; rClip.Width = header.Width; oldClip = g.Clip; g.SetClip(rClip); } if (rImage.Width > 2) { g.DrawImage(item.ImageList.Images[index], rImage.Location); // item.ImageList.Draw(g, rImage.Location, index); } if (oldClip != null) g.Clip = oldClip; } // Draw text Rectangle rText = item.GetBounds(ItemBoundsPortion.Label); if (rText.Width > 2) { // Draw the item text for views other than the Details view. eTextFormat flags = eTextFormat.Left | eTextFormat.SingleLine | eTextFormat.NoPrefix; if (this.View == View.Tile && item.SubItems.Count > 1) flags |= eTextFormat.Top; else flags |= eTextFormat.VerticalCenter; if (this.View == View.LargeIcon) { flags = eTextFormat.HorizontalCenter | eTextFormat.WordBreak | eTextFormat.EndEllipsis | eTextFormat.NoClipping; } else if (this.View == View.Details && header != null) { flags |= eTextFormat.EndEllipsis; if (header.TextAlign == HorizontalAlignment.Center) flags |= eTextFormat.HorizontalCenter; else if (header.TextAlign == HorizontalAlignment.Right) flags |= eTextFormat.Right; rText.X += 2; } else if (this.View == View.List || this.View == View.SmallIcon) rText.X += 2; if (!BarFunctions.IsVista) rText.Inflate(0, 1); TextDrawing.DrawString(g, item.Text, font, color, rText, flags); if (this.View == View.Tile && item.SubItems.Count > 1) { Size sz = TextDrawing.MeasureString(g, item.Text, font); rText.Y += sz.Height; rText.Height -= sz.Height; Color c1 = item.SubItems[1].ForeColor; if (!c1.IsEmpty && c1 != color) color = c1; else color = SystemColors.ControlDarkDark; TextDrawing.DrawString(g, item.SubItems[1].Text, font, color, rText, flags); } } if (this.View == View.Details || this.StateImageList != null) { if (this.StateImageList != null) { if (item.StateImageIndex >= 0 && item.StateImageIndex < this.StateImageList.Images.Count) { Rectangle r = item.GetBounds(ItemBoundsPortion.Icon); if (this.View == View.Details || this.View == View.List) r.X -= 19; else if (this.View == View.LargeIcon && r.Width > this.StateImageList.ImageSize.Width) { r.X += (r.Width - this.StateImageList.ImageSize.Width) / 2; r.Y++; } else if (this.View == View.Tile && r.Height > this.StateImageList.ImageSize.Height) { r.Y += (r.Height - this.StateImageList.ImageSize.Height) / 2; r.X++; } this.StateImageList.Draw(g, r.Location, item.StateImageIndex); } } } if (this.CheckBoxes && (this.View == View.Details || this.View == View.List || this.View == View.SmallIcon || this.View == View.LargeIcon) && this.StateImageList == null) { Rectangle r = item.GetBounds(ItemBoundsPortion.Icon); if (this.View == View.LargeIcon) r.X += (r.Width - m_CheckSignSize.Width) / 2 - 4; else if (this.View == View.List) r.X -= 18; else if (this.View == View.SmallIcon && hasImage && this.Groups.Count > 0) r.X -= 20; else if (this.View == View.SmallIcon) r.X -= 3; else r.X -= 21; Office2007CheckBoxItemPainter p = PainterFactory.CreateCheckBoxItemPainter(null); Office2007CheckBoxColorTable ctt = GetCheckBoxColorTable(); Office2007CheckBoxStateColorTable ct = ctt.Default; if ((state & ListViewItemStates.Grayed) != 0 || !this.Enabled) ct = ctt.Disabled; //else if ((state & ListViewItemStates.Hot) != 0) // ct = ctt.MouseOver; p.PaintCheckBox(g, new Rectangle(r.X + 4, r.Y + (r.Height - m_CheckSignSize.Height) / 2, m_CheckSignSize.Width, m_CheckSignSize.Height), ct, item.Checked ? CheckState.Checked : CheckState.Unchecked); } }
/// <summary> /// Gets the bounds for a given sub-item /// </summary> /// <param name="item">The item parent of the sub-item for which we request the bounds.</param> /// <param name="index">The index of the sub-item.</param> /// <returns>The bounding rectangle of the sub-item.</returns> private Rectangle GetSubItemBounds(ListViewItem item, int index) { if (item == null) throw new ArgumentNullException("item"); if ((index < 0) || (index >= item.SubItems.Count)) throw new ArgumentOutOfRangeException("index"); // Retrieve the bounds of the entire ListViewItem (all sub-items) Rectangle lviBounds = item.GetBounds(ItemBoundsPortion.Entire); int left = lviBounds.Left; // Compute the X position of the sub-item. ColumnHeader col = base.Columns[index]; for (int i = 0; i < index; i++) left += base.Columns[i].Width; return new Rectangle(left, lviBounds.Top, base.Columns[index].Width, lviBounds.Height); }
/// <summary> /// Get bounds for a SubItem /// </summary> /// <param name="Item">Target ListViewItem</param> /// <param name="SubItem">Target SubItem index</param> /// <returns>Bounds of SubItem (relative to ListView)</returns> private Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { int[] order = Util.GetColumnOrder(); Rectangle subItemRect = Rectangle.Empty; if (SubItem >= order.Length) throw new IndexOutOfRangeException("SubItem " + SubItem + " out of range"); if (Item == null) throw new ArgumentNullException("Item"); Rectangle lviBounds = Item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; switch (m_lvEntries.BorderStyle) { case BorderStyle.Fixed3D: subItemX += SystemInformation.Border3DSize.Width; break; case BorderStyle.FixedSingle: subItemX += SystemInformation.BorderSize.Width; break; } ColumnHeader col; int i; for (i = 0; i < order.Length; i++) { col = m_lvEntries.Columns[order[i]]; if (col.Index == SubItem) break; subItemX += col.Width; } if (m_lvEntries.RightToLeftLayout) { subItemX = m_lvEntries.Width - subItemX - m_lvEntries.Columns[order[i]].Width; } // Adapt the bounds subItemX = subItemX - 1; int subItemT = lviBounds.Top + 2; int subItemW = m_lvEntries.Columns[order[i]].Width + 1; int subItemH = lviBounds.Height; if (SubItem == 0) { // Text indention because of the entry icon subItemX += 21; subItemW -= 21; } subItemRect = new Rectangle(subItemX, subItemT, subItemW, subItemH); return subItemRect; }
private bool HitImageTestAt(Point p, ListViewItem item) { Rectangle rcItem = item.GetBounds(ItemBoundsPortion.Entire); GraphicsUnit units = GraphicsUnit.Point; if (item.ImageList != null) { Image img = item.ImageList.Images[0]; RectangleF rcImageF = img.GetBounds(ref units); Rectangle rcImage = Rectangle.Round(rcImageF); rcImage.Width += item.IndentCount + item.Position.X; p.Offset(rcItem.Left, -rcItem.Top); if (rcImage.Contains(p)) { return true; } else { return false; } } return false; }
/// <summary>Calculates the boundaries of a cell in the list view</summary> /// <param name="item">Item in the list view from which to calculate the cell</param> /// <param name="subItem">Index der cell whose boundaries to calculate</param> /// <returns>The boundaries of the specified list view cell</returns> /// <exception cref="IndexOutOfRangeException"> /// When the specified sub item index is not in the range of valid sub items /// </exception> protected Rectangle GetSubItemBounds(ListViewItem item, int subItem) { int[] order = GetColumnOrder(); if(order == null) // No Columns return Rectangle.Empty; if(subItem >= order.Length) throw new IndexOutOfRangeException("SubItem " + subItem + " out of range"); // Determine the border of the entire ListViewItem, including all sub items Rectangle itemBounds = item.GetBounds(ItemBoundsPortion.Entire); int subItemX = itemBounds.Left; // Find the horizontal position of the sub item. Because the column order can vary, // we need to use Columns[order[i]] instead of simply doing Columns[i] here! ColumnHeader columnHeader; int i; for(i = 0; i < order.Length; ++i) { columnHeader = this.Columns[order[i]]; if(columnHeader.Index == subItem) break; subItemX += columnHeader.Width; } return new Rectangle( subItemX, itemBounds.Top, this.Columns[order[i]].Width, itemBounds.Height ); }
public Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { int[] columnOrder = this.GetColumnOrder(); if (SubItem >= columnOrder.Length) { throw new IndexOutOfRangeException("SubItem " + SubItem + " out of range"); } if (Item == null) { throw new ArgumentNullException("Item"); } Rectangle bounds = Item.GetBounds(ItemBoundsPortion.Entire); int left = bounds.Left; int index = 0; while (index < columnOrder.Length) { ColumnHeader header = base.Columns[columnOrder[index]]; if (header.Index == SubItem) { break; } left += header.Width; index++; } return new Rectangle(left, bounds.Top, base.Columns[columnOrder[index]].Width, bounds.Height); }
/// <summary> /// Find ListViewItem and SubItem Index at position (x,y) /// </summary> /// <param name="x">relative to ListView</param> /// <param name="y">relative to ListView</param> /// <param name="item">Item at position (x,y)</param> /// <returns>SubItem index</returns> public int GetSubItemAt(int x, int y, out ListViewItem item) { item = GetItemAt(x, y); if (item != null) { int[] order = GetColumnOrder(); Rectangle lviBounds = item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; foreach (int t in order) { ColumnHeader h = Columns[t]; if (x < subItemX + h.Width) { return h.Index; } subItemX += h.Width; } } return -1; }
public Rectangle GetSubItemBounds(ListViewItem item, int subItem) { int[] order = GetColumnOrder(); if (subItem >= order.Length) { throw new IndexOutOfRangeException("SubItem '" + subItem + "' is out of range."); } if (item == null) { throw new ArgumentNullException("item"); } Rectangle itemBounds = item.GetBounds(ItemBoundsPortion.Entire); int subItemX = itemBounds.Left; for (int i = 0; i < order.Length; i++) { ColumnHeader col = Columns[order[i]]; if (col.Index == subItem) { return new Rectangle(subItemX, itemBounds.Top, col.Width, itemBounds.Height); } subItemX += col.Width; } return Rectangle.Empty; }
/// <summary> /// Get bounds for a SubItem /// </summary> /// <param name="Item">Target ListViewItem</param> /// <param name="SubItem">Target SubItem index</param> /// <returns>Bounds of SubItem (relative to ListView)</returns> public Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { int[] order = GetColumnOrder(); Rectangle subItemRect = Rectangle.Empty; if (SubItem >= order.Length) throw new IndexOutOfRangeException("SubItem " + SubItem + " out of range"); if (Item == null) throw new ArgumentNullException("Item"); Rectangle lviBounds = Item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; ColumnHeader col; int i; for (i = 0; i < order.Length; i++) { col = this.Columns[order[i]]; if (col.Index == SubItem) break; subItemX += col.Width; } subItemRect = new Rectangle(subItemX, lviBounds.Top, this.Columns[order[i]].Width, lviBounds.Height); return subItemRect; }
protected virtual void DrawListViewSubItems (Graphics dc, ListView control, ListViewItem item) { int columns_count = control.Columns.Count; int count = Math.Min (item.SubItems.Count, columns_count); // 0th item already done (in this case) for (int i = 1; i < count; i++) DrawListViewSubItem (dc, control, item, i); // Fill in selection for remaining columns if Column.Count > SubItems.Count Rectangle sub_item_rect = item.GetBounds (ItemBoundsPortion.Label); if (item.Selected && (control.Focused || !control.HideSelection) && control.FullRowSelect) { for (int index = count; index < columns_count; index++) { ColumnHeader col = control.Columns [index]; sub_item_rect.X = col.Rect.X - control.h_marker; sub_item_rect.Width = col.Wd; dc.FillRectangle (control.Focused ? SystemBrushes.Highlight : SystemBrushes.Control, sub_item_rect); } } }
private static Rectangle GetIconsSpacerBounds(ListViewItem item) { if (item == null) return Rectangle.Empty; var xPosition = item.Bounds.X; for (var i = 1; i < item.SubItems.Count; i++) { var iPos = item.SubItems[i].Bounds.X + item.SubItems[i].Bounds.Width; if (item.Position.X > iPos && xPosition < iPos) xPosition = iPos; } var iconBounds = item.GetBounds(ItemBoundsPortion.Icon); var labelBounds = item.GetBounds(ItemBoundsPortion.Label); var width = iconBounds.X - xPosition + iconBounds.Width + (labelBounds.Width < 0 ? labelBounds.Width : 0); return new Rectangle(xPosition, item.Bounds.Y, width, item.Bounds.Height); }
/// <summary> /// �ړ��A�C�e���̑}���ʒu��\���������`�悵�܂��B /// </summary> /// <param name="listItem">�`�悷��ʒu�̃��X�g�A�C�e��</param> /// <param name="isUpper">���̕`��ꏊ��w�肵�܂��B���� listItem �̏㑤�ł���� true�A�����ł���� false ��w�肵�܂��B </param> private void drawInsertLine(ListViewItem listItem, bool isUpper) { using (var g = this.CreateGraphics()) { var rect = listItem.GetBounds(ItemBoundsPortion.ItemOnly); int y = isUpper ? rect.Top : rect.Bottom; Point ptL = new Point(0, y); Point ptR = new Point(rect.Width, y); g.DrawLine(new Pen(DRAG_TARGET_LINE_COLOR, 2), ptL, ptR); } }
/// <summary> /// Retrieve the bounds of a ListViewSubItem /// </summary> /// <param name="item">The Item containing the SubItem</param> /// <param name="subItemIndex">Index of the SubItem</param> /// <returns>Subitem's bounds</returns> protected Rectangle GetSubItemBounds(ListViewItem item, int subItemIndex) { Rectangle subItemRect = Rectangle.Empty; if (item == null) { throw new ArgumentNullException("Item", "The list view item cannot be null"); } int[] listColumns = GetCurrentColumnOrder(); if (listColumns != null) { if (subItemIndex >= listColumns.Length) { throw new IndexOutOfRangeException("Index " + subItemIndex + " is out of range of the columns in the listview"); } // Retrieve the bounds of the entire ListViewItem (all subitems) Rectangle itemBounds = item.GetBounds(ItemBoundsPortion.Entire); // Calculate the X position of the SubItem. // Because the columns may be reordered we have //to use Columns[listColumns[index]] instead of Columns[index] int subItemX = itemBounds.Left; int index = 0; for (; index < listColumns.Length; index++) { ColumnHeader column = this.Columns[listColumns[index]]; if (column.Index == subItemIndex) { break; } subItemX += column.Width; } subItemRect = new Rectangle(subItemX, itemBounds.Top, this.Columns[listColumns[index]].Width, itemBounds.Height); } return subItemRect; }
/// <summary> /// Determines if the specified Point is in the top half of the specified ListViewItem /// </summary> /// <param name="location">The point to check</param> /// <param name="itemToCheck">The ListViewItem whose bounds are to be checked</param> /// <returns>true if location is in the top half of itemToCheck, otherwise false</returns> private bool IsPointInTopHalfOfItem(Point location, ListViewItem itemToCheck) { var pt = PointToClient(location); var rc = itemToCheck.GetBounds(ItemBoundsPortion.Entire); return (pt.Y < (rc.Top + (rc.Height/2))); }
private Rectangle GetSubItemBounds(ListViewItem item, int subItem) { Rectangle subItemRect = Rectangle.Empty; if (item == null) { throw new ArgumentNullException("item"); } int[] order = this.GetColumnOrder(); if (order == null) { // No Columns return subItemRect; } if (subItem >= order.Length) { throw new ArgumentOutOfRangeException("SubItem " + subItem.ToString(CultureInfo.InvariantCulture) + " out of range"); } // Retrieve the bounds of the entire ListViewItem (all subitems) Rectangle bounds = item.GetBounds(ItemBoundsPortion.Entire); int subItemX = bounds.Left; ColumnHeader header = null; int process = 0; // Calculate the X position of the SubItem. for (process = 0; process <= order.Length - 1; process++) { header = this.Columns[order[process]]; if (header.Index == subItem) { break; } subItemX += header.Width; } subItemRect = new Rectangle(subItemX, bounds.Top, this.Columns[order[process]].Width, bounds.Height); return subItemRect; }
/// <summary> /// Find ListViewItem and SubItem Index at position (x,y) /// </summary> /// <param name="x">relative to ListView</param> /// <param name="y">relative to ListView</param> /// <param name="item">Item at position (x,y)</param> /// <returns>SubItem index</returns> public int GetSubItemAt(int x, int y, out ListViewItem item) { item = this.GetItemAt(x, y); if (item != null) { int[] order = GetColumnOrder(); Rectangle lviBounds; int subItemX; lviBounds = item.GetBounds(ItemBoundsPortion.Entire); subItemX = lviBounds.Left; for (int i = 0; i < order.Length; i++) { ColumnHeader h = this.Columns[order[i]]; if (x < subItemX + h.Width) { return h.Index; } subItemX += h.Width; } } return -1; }
/// <summary> /// Retrieve the bounds of a ListViewSubItem /// </summary> /// <param name="item">The Item containing the SubItem</param> /// <param name="subItem">Index of the SubItem</param> /// <returns>Subitem's bounds</returns> protected Rectangle GetSubItemBounds(ListViewItem item, int subItem) { Rectangle subItemRect = Rectangle.Empty; if (item == null) throw new ArgumentNullException("item", SmartResourceManager.GetString("SLV_003", "Workshare.Common.Exceptions.Resource", Assembly.GetAssembly(typeof(SmartListView)),System.Threading.Thread.CurrentThread.CurrentCulture)); int[] order = GetColumnOrder(); if (order == null) // No Columns return subItemRect; if (subItem >= order.Length) throw new ArgumentOutOfRangeException("subItem", subItem, SmartResourceManager.GetString("SLV_001", "Workshare.Common.Exceptions.Resource", Assembly.GetAssembly(typeof(SmartListView)),System.Threading.Thread.CurrentThread.CurrentCulture)); // Retrieve the bounds of the entire ListViewItem (all subitems) Rectangle lviBounds = item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; // Calculate the X position of the SubItem. // Because the columns can be reordered we have to use Columns[order[i]] instead of Columns[i] ! ColumnHeader col; int i; for (i=0; i<order.Length; i++) { col = this.Columns[order[i]]; if (col.Index == subItem) break; subItemX += col.Width; } subItemRect = new Rectangle(subItemX, lviBounds.Top-1, this.Columns[order[i]].Width, lviBounds.Height); return subItemRect; }
public Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { if (SubItem >= this.Columns.Count) throw new IndexOutOfRangeException("SubItem " + SubItem + " out of range"); else if (Item == null) return Rectangle.Empty; // throw new ArgumentNullException("Item"); int top, left, width, height; //Item.EnsureVisible(); Rectangle bounds = Item.GetBounds(ItemBoundsPortion.Entire); top = bounds.Top; width = this.Columns[SubItem].Width; height = bounds.Height; left = bounds.Left; for(int index=0;index<SubItem;index+=1) left += this.Columns[index].Width; return new Rectangle(new Point(left,top),new Size(width,height)); }
/// <summary> /// Retrieve the bounds of a ListViewSubItem /// </summary> /// <param name="Item">The Item containing the SubItem</param> /// <param name="SubItem">Index of the SubItem</param> /// <returns>Subitem's bounds</returns> protected Rectangle GetSubItemBounds(ListViewItem Item, int SubItem) { Rectangle subItemRect = Rectangle.Empty; if (Item == null) { Logger.LogError("Attempted to get NULL item"); throw new ArgumentNullException("Item"); } int[] order = GetColumnOrder(); if (order == null) // No Columns return subItemRect; if (SubItem >= order.Length) { Utilities.ErrorMessage errorMessage = new Utilities.ErrorMessage( "SUBITEM_OUTOFRANGE", "Workshare.PolicyDesigner.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly(), SubItem); Logger.LogError(errorMessage.LogString); throw new IndexOutOfRangeException(errorMessage.DisplayString); } // Retrieve the bounds of the entire ListViewItem (all subitems) Rectangle lviBounds = Item.GetBounds(ItemBoundsPortion.Entire); int subItemX = lviBounds.Left; // Calculate the X position of the SubItem. // Because the columns can be reordered we have to use Columns[order[i]] instead of Columns[i] ! ColumnHeader col; int i; for (i = 0; i < order.Length; i++) { col = this.Columns[order[i]]; if (col.Index == SubItem) break; subItemX += col.Width; } subItemRect = new Rectangle(subItemX, lviBounds.Top, this.Columns[order[i]].Width, lviBounds.Height); return subItemRect; }