/// <summary>修正ListView的Dpi</summary> /// <param name="lv"></param> public static void FixDpi(this ListView lv) { if (Dpi == 0) { Dpi = (Int32)lv.CreateGraphics().DpiX; } foreach (ColumnHeader item in lv.Columns) { item.Width *= Dpi / 96; } }
private void listView_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { ListViewItem item = listView.GetItemAt(e.X, e.Y); if (item != null && e.X >= nameColumn.Width) { BuildEventInfo info = item.Tag as BuildEventInfo; Graphics g = listView.CreateGraphics(); if (g.MeasureString(info.Value, listView.Font).Width > valueColumn.Width) { toolTip.SetToolTip(listView, info.Value); return; } } toolTip.SetToolTip(listView, ""); }
public static void AdjustTileToWidth(this ListView lvw, int maxLines = 1, int iconSpacing = 4) { const string str = "Wg"; var lvTVInfo = new NativeMethods.LVTILEVIEWINFO(0) { IconTextSpacing = iconSpacing, MaxTextLines = maxLines }; var sb = new StringBuilder(str); for (var i = 0; i < maxLines; i++) { sb.Append("\r" + str); } using (var g = lvw.CreateGraphics()) lvTVInfo.TileSize = new Size(lvw.ClientSize.Width, Math.Max(lvw.LargeImageList.ImageSize.Height, TextRenderer.MeasureText(g, sb.ToString(), lvw.Font).Height)); NativeMethods.SendMessage(lvw.Handle, NativeMethods.ListViewMessage.SetTileViewInfo, 0, lvTVInfo); //var lvTVInfo = new NativeMethods.LVTILEVIEWINFO(0) { TileWidth = lvw.ClientSize.Width }; //NativeMethods.SendMessage(lvw.Handle, NativeMethods.ListViewMessage.SetTileViewInfo, 0, lvTVInfo); //NativeMethods.SendMessage(lvw.Handle, (uint)NativeMethods.ListViewMessage.SetExtendedListViewStyle, new IntPtr(0x200000), new IntPtr(0x200000)); }
public override int ListViewGetHeaderHeight (ListView listView, Font font) { if (!RenderClientAreas) return base.ListViewGetHeaderHeight (listView, font); VisualStyleElement element = VisualStyleElement.Header.Item.Normal; if (!VisualStyleRenderer.IsElementDefined (element)) return base.ListViewGetHeaderHeight (listView, font); Control control = null; Graphics g; if (listView == null) { control = new Control (); g = control.CreateGraphics (); } else g = listView.CreateGraphics (); int result = new VisualStyleRenderer (element).GetPartSize (g, ThemeSizeType.True).Height; g.Dispose (); if (listView == null) control.Dispose (); return result; }
/// <summary> /// /// </summary> /// <param name="lv"></param> /// <returns></returns> public static Int32[] GetMaximumColumnWidths(ListView lv) { Int32 index = 0; Int32[] widthArray = new Int32[lv.Columns.Count]; foreach (ColumnHeader col in lv.Columns) { widthArray[index] = col.Width; index++; } System.Drawing.Graphics g = lv.CreateGraphics(); Int32 offset = Convert.ToInt32(Math.Ceiling(g.MeasureString(" ", lv.Font).Width)); Int32 width = 0; foreach (ListViewItem item in lv.Items) { index = 0; foreach (ListViewItem.ListViewSubItem subItem in item.SubItems) { width = Convert.ToInt32(Math.Ceiling(g.MeasureString(subItem.Text, item.Font).Width)) + offset; if (width > widthArray[index]) { widthArray[index] = width; } index++; } } g.Dispose(); return widthArray; }
/// <summary> /// 在ListView的子项里绘制一个checkbox /// </summary> /// <param name="listView"></param> /// <param name="subItem"></param> /// <param name="isChecked"></param> private void drawCheckBox(ListView listView, ListViewItem.ListViewSubItem subItem, bool isChecked) { Graphics graph = listView.CreateGraphics(); Image icon = getCheckBoxImage(isChecked); Brush brush = new SolidBrush(subItem.BackColor); graph.FillRectangle(brush, subItem.Bounds.X + 1, subItem.Bounds.Y + 1, subItem.Bounds.Width - 2, subItem.Bounds.Height - 2); graph.DrawImage(icon, new Point(subItem.Bounds.X + 5, subItem.Bounds.Y)); graph.Dispose(); }
private void AutoResizeColumnWidth(ListView lv) { int count = lv.Columns.Count; int MaxWidth = 0; Graphics graphics = lv.CreateGraphics(); Font font = lv.Font; ListView.ListViewItemCollection items = lv.Items; string str; int width; lv.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); for (int i = 0; i < count; i++) { str = lv.Columns[i].Text; MaxWidth = lv.Columns[i].Width; foreach (ListViewItem item in items) { if (item.SubItems.Count > i) { str = item.SubItems[i].Text; width = (int)graphics.MeasureString(str, font).Width; if (width > MaxWidth) { MaxWidth = width; } } } if (i == 0) { lv.Columns[i].Width = lv.SmallImageList.ImageSize.Width + MaxWidth; } else if (i == 1) { lv.Columns[i].Width = 60; } else { lv.Columns[i].Width = MaxWidth; } } }