private void InitElement(MHtmlElement parent, MHtmlElement element) { element.Parent = parent; if (element.ForeColor.IsEmpty) { element.ForeColor = parent.ForeColor; } if (element.Font == null) { element.Font = parent.Font; } if (element.Rectangle.IsEmpty) { element.Location = new Point(parent.X + parent.Padding.Left, parent.Y + parent.Padding.Top); element.Size = new Size(parent.Width - parent.X - parent.Padding.Right, parent.Height - parent.Y - parent.Padding.Bottom); } if (element.RelativePosition.IsEmpty) { element.RelativePosition = new Point(parent.RelativePosition.X + parent.Padding.Left + element.X, parent.RelativePosition.Y + parent.Padding.Top + element.Y); } if (element.Items != null) { foreach (var item in element.Items) { this.InitElement(element, item); } } }
protected override void OnPaint(PaintEventArgs e) { if (this.Root == null) { base.OnPaint(e); return; } Hovers.Clear(); Clicks.Clear(); Leaves.Clear(); var container = new MHtmlElement(); container.Align = TextAlignment.MiddleCenter; container.BackColor = this.BackColor; container.BackColorGradient = Color.Empty; container.Items = null; container.Border = null; container.ForeColor = this.ForeColor; container.Gradient = GradientMode.None; container.ID = this.Name; container.Radius = 0; container.RadiusMode = RadiusMode.None; container.Location = new Point(0, 0); container.Size = new Size(this.Width, this.Height); container.Text = this.Text; this.InitElement(container, Root); this.DrawElement(this.Root, e.Graphics); }
internal void DrawElement(MHtmlElement element, Graphics graphics) { var rect = new Rectangle(element.RelativePosition, element.Rectangle.Size); if (rect.IsEmpty) { return; } #region 边框和背景 // 圆角模式,忽略边框样式 if (element.RadiusMode != RadiusMode.None && element.Radius > 0) { var color1 = element.BackColor.IsEmpty ? Color.Transparent : element.BackColor; var color2 = element.BackColorGradient; RadiusDrawable.DrawRadius(graphics, rect, element.RadiusMode, element.Radius, element.BackColor, element.BackColorGradient, element.Gradient, element.Border.Color, element.Border.Width); } // 有边框 else if (element.Border != null && !element.Border.IsEmpty) { if (!element.BackColor.IsEmpty) { using (var brush = new SolidBrush(element.BackColor)) { graphics.FillRectangle(brush, element.Rectangle); } } var bl = element.Border.Left == 0 ? element.Border.Width : element.Border.Left; var bt = element.Border.Top == 0 ? element.Border.Width : element.Border.Top; var br = element.Border.Right == 0 ? element.Border.Width : element.Border.Right; var bb = element.Border.Bottom == 0 ? element.Border.Width : element.Border.Bottom; using (var pen = new Pen(element.Border.Color)) { pen.DashStyle = element.Border.GetDashStyle(); if (bl > 0) { pen.Width = bl; graphics.DrawLine(pen, rect.X, rect.Y, rect.X, rect.Y + rect.Height); } if (bt > 0) { pen.Width = bt; graphics.DrawLine(pen, rect.X, rect.Y, rect.X + rect.Width, rect.Y); } if (br > 0) { pen.Width = br; graphics.DrawLine(pen, rect.X + rect.Width, rect.Y, rect.X + rect.Width, rect.Y + rect.Height); } if (bb > 0) { pen.Width = bb; graphics.DrawLine(pen, rect.X, rect.Y + rect.Height, rect.X + rect.Width, rect.Y + rect.Height); } } } else { // 没有边框,直接处理背景 if (!element.BackColor.IsEmpty) { using (var brush = new SolidBrush(element.BackColor)) { graphics.FillRectangle(brush, rect); } } } #endregion if (!string.IsNullOrEmpty(element.Text)) { var textRect = new Rectangle(rect.X + element.Padding.Left, rect.Y + element.Padding.Top, rect.Width - element.Padding.Left - element.Padding.Right, rect.Height - element.Padding.Top - element.Padding.Bottom); GDIHelper.DrawString(graphics, textRect, element.Font, element.Text, element.ForeColor, element.Align); } if (element.HasHover) { Hovers.Add(element); } if (element.HasClick) { Clicks.Add(element); } if (element.HasLeave) { Leaves.Add(element); } if (element.Items != null) { foreach (var item in element.Items) { this.DrawElement(item, graphics); } } }