private void DrawHierItem(PaintEventArgs e, HierItem it, ref int hierCount, ref float y, ref float maxwidth) { float x = hierCount * _indent; float oldx = x; Color color = ForeColor; Font font = GetFont(it, out color); //draw image Image img = GetImage(it, crtIndex, e.Graphics); if (img != null) { e.Graphics.DrawImageUnscaled(img, (int)x, (int)y); x += (img.Width + cstSpanImgAndTitle); } //draw title SizeF fontsize = e.Graphics.MeasureString(it.Title, font); using (Brush brush = new SolidBrush(color)) { e.Graphics.DrawString(it.Title, font, brush, x, y); maxwidth = Math.Max(maxwidth, x + fontsize.Width); } it._imgBounds = new RectangleF(oldx, y, img.Width, img.Height); it._titleBounds = new RectangleF(x, y, x + fontsize.Width, fontsize.Height); y += fontsize.Height; //draw properties if (!it._isclosed && it.Properties != null && it.Properties.Count > 0) { float maxLen = GetMaxLenOfPropertyName(e.Graphics, font, it.Properties.Keys); float px = x + cstOffsetProperty; y += cstSpanBetweenProperty; using (Brush brush = new SolidBrush(ForeColor)) { foreach (string name in it.Properties.Keys) { e.Graphics.DrawString(name, Font, brush, px, y); e.Graphics.DrawString(":" + it.Properties[name], Font, brush, px + maxLen, y); y += fontsize.Height; y += cstSpanBetweenProperty; // fontsize = e.Graphics.MeasureString(":" + it.Properties[name], Font); maxwidth = Math.Max(maxwidth, px + maxLen + fontsize.Width); } } } y += cstSpanBetweenItem; //draw children if (!it._isclosed && it.ChildCount > 0) { hierCount++; foreach (HierItem child in it.Children) { DrawHierItem(e, child, ref hierCount, ref y, ref maxwidth); } hierCount--; } }
private Font GetFont(HierItem it, out Color color) { if (it.IsHotlink) { color = Color.Blue; return(new Font(Font.FontFamily, Font.Size, FontStyle.Underline)); } else { color = ForeColor; return(Font); } }
private void GetHieritemAt(Point point, HierItem parent, ref HierItem item) { if (parent._imgBounds.Contains(point) || parent._titleBounds.Contains(point)) { item = parent; return; } if (parent.ChildCount > 0) { foreach (HierItem it in parent.Children) { GetHieritemAt(point, it, ref item); } } }
private void DrawHierItems(PaintEventArgs e) { if (_hierItems == null || _hierItems.Count == 0) { e.Graphics.DrawString("没有查询到任何要素!", Font, Brushes.Black, 4, 10); return; } float y = 0; float maxwidth = 0; DrawPageInfos(e, ref y, true, ref maxwidth); // int hierCount = 0; int endIdx = Math.Min(_hierItems.Count, _currentPage * _countPerPage + _countPerPage); List <HierItem> drawedItems = null; if (OnAfterDrawHierItems != null) { drawedItems = new List <HierItem>(); } crtIndex = 1; for (int i = _currentPage * _countPerPage; i < endIdx; i++) { HierItem it = _hierItems[i]; if (it == null) { continue; } DrawHierItem(e, it, ref hierCount, ref y, ref maxwidth); if (drawedItems != null) { drawedItems.Add(it); } crtIndex++; } // if (OnAfterDrawHierItems != null) { OnAfterDrawHierItems(this, drawedItems.ToArray()); } // DrawPageInfos(e, ref y, false, ref maxwidth); // Height = (int)y; Width = (int)Math.Max(Width, maxwidth); }
protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); if (IsPageOperate(e)) { return; } if (IsDisplayOperate(e)) { return; } if (IsClearOperate(e)) { return; } HierItem findItem = null; int endIdx = Math.Min(_hierItems.Count, _currentPage * _countPerPage + _countPerPage); for (int i = _currentPage * _countPerPage; i < endIdx; i++) { HierItem it = _hierItems[i]; GetHieritemAt(e.Location, it, ref findItem); if (findItem != null) { break; } } if (findItem != null) { if (findItem._imgBounds.Contains(e.Location)) { findItem._isclosed = !findItem._isclosed; } else if (OnClickHierItem != null) { OnClickHierItem(this, findItem); } Invalidate(); } }
private Image GetImage(HierItem it, int i, Graphics g) { if (it.Image != null) { return(it.Image); } if (_imageList != null && _imageList.Images != null && it.ImageIndex >= 0 && it.ImageIndex < _imageList.Images.Count) { return(_imageList.Images[it.ImageIndex]); } Image img = imageList1.Images[0]; SizeF fontsize = g.MeasureString(i.ToString(), Font); using (Graphics tg = Graphics.FromImage(img)) { tg.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; tg.DrawImage(img, 0, 0); tg.DrawString(i.ToString(), Font, Brushes.Red, (img.Width - fontsize.Width) / 2, (img.Height - fontsize.Height) / 2 - 1); } return(img); }