protected override void OnPaint(PaintEventArgs e) { imageLocationX = 8; imageLocationY = (int)Math.Round((float)(this.ClientRectangle.Height - (int)Math.Round(this.Font.SizeInPoints)) / 2); imageLocationY = imageLocationY - 2; textLocationX = 26; textLocationY = (int)Math.Round((float)(this.ClientRectangle.Height - (int)Math.Round(this.Font.SizeInPoints)) / 2); textLocationY = textLocationY - 1; fillRect = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height); drawRect = new Rectangle(0, 0, this.Bounds.Width - 2, this.Bounds.Height - 2); textBrush = new SolidBrush(this.ForeColor); imageRect = new Rectangle(imageLocationX, imageLocationY, 16, 16); backBrush = new LinearGradientBrush(drawRect, Color.White, Color.FromArgb(236, 233, 217), LinearGradientMode.ForwardDiagonal); backBrush_Selected = new LinearGradientBrush(drawRect, Color.FromArgb(241, 243, 236), Color.FromArgb(188, 196, 166), LinearGradientMode.ForwardDiagonal); drawPen = new Pen(SystemColors.ActiveCaption); drawPen_Selected = new Pen(SystemColors.ActiveCaption); if (this.Selected) { e.Graphics.FillPath(backBrush_Selected, DrawingTool.RoundedRect(fillRect, 3)); e.Graphics.DrawPath(drawPen_Selected, DrawingTool.RoundedRect(drawRect, 3)); } else { e.Graphics.FillPath(backBrush, DrawingTool.RoundedRect(fillRect, 3)); e.Graphics.DrawPath(drawPen, DrawingTool.RoundedRect(drawRect, 3)); } if (this.Image != null) { e.Graphics.DrawImage(this.Image, imageRect); e.Graphics.DrawString(this.Text, this.Font, textBrush, new Point(textLocationX + 14, textLocationY), stringFormat); } e.Graphics.DrawString(this.ShowText, this.Font, textBrush, new Point(textLocationX, textLocationY), stringFormat); }
/// <summary> /// 绘制项 /// </summary> /// <param name="g"></param> /// <param name="item"></param> /// <param name="state"></param> /// <param name="bounds"></param> internal virtual void DrawItem(Graphics g, ShengListViewItem item) { Rectangle bounds = LayoutManager.GetItemBounds(item); g.SetClip(bounds); DrawItemBackground(g, bounds); Rectangle backgroundRect = bounds; backgroundRect.Width -= 1; backgroundRect.Height -= 1; // Paint background Selected if ((LayoutManager.Focused && ((item.State & ShengListViewItemState.Selected) == ShengListViewItemState.Selected)) || (LayoutManager.Focused == false && ((item.State & ShengListViewItemState.Selected) == ShengListViewItemState.Selected) && ((item.State & ShengListViewItemState.Hovered) == ShengListViewItemState.Hovered))) { using (Brush bSelected = new LinearGradientBrush(backgroundRect, Theme.SelectedColorStart, Theme.SelectedColorEnd, LinearGradientMode.Vertical)) { // ImageListViewUtility.FillRoundedRectangle(g, bSelected, bounds, 4); g.FillPath(bSelected, DrawingTool.RoundedRect(backgroundRect, _radius)); } } // Paint background unfocused else if (LayoutManager.Focused == false && ((item.State & ShengListViewItemState.Selected) == ShengListViewItemState.Selected)) { using (Brush bGray64 = new LinearGradientBrush(backgroundRect, Theme.UnFocusedColorStart, Theme.UnFocusedColorEnd, LinearGradientMode.Vertical)) { // ImageListViewUtility.FillRoundedRectangle(g, bGray64, bounds, 4); g.FillPath(bGray64, DrawingTool.RoundedRect(backgroundRect, _radius)); } } // Paint background Hovered //如果正处于框选状态,不绘制Hover状态,减小闪烁 if (LayoutManager.MouseSelecting == false && (item.State & ShengListViewItemState.Hovered) == ShengListViewItemState.Hovered) { using (Brush bHovered = new LinearGradientBrush(backgroundRect, Theme.HoverColorStart, Theme.HoverColorEnd, LinearGradientMode.Vertical)) { // ImageListViewUtility.FillRoundedRectangle(g, bHovered, bounds, 4); g.FillPath(bHovered, DrawingTool.RoundedRect(backgroundRect, _radius)); } } DrawItemBorder(g, bounds, item); // Focus rectangle if (LayoutManager.Focused && ((item.State & ShengListViewItemState.Focused) == ShengListViewItemState.Focused)) { ControlPaint.DrawFocusRectangle(g, bounds); } DrawItemContent(g, bounds, item); }
/// <summary> /// 绘制项的边框 /// </summary> /// <param name="g"></param> /// <param name="bounds"></param> internal virtual void DrawItemBorder(Graphics g, Rectangle bounds) { Rectangle backgroundRect = bounds; backgroundRect.Width -= 1; backgroundRect.Height -= 1; using (Pen pWhite128 = new Pen(Color.FromArgb(128, Theme.ItemBorderColor))) { // ImageListViewUtility.DrawRoundedRectangle(g, pWhite128, bounds.Left, bounds.Top , bounds.Width - 1, bounds.Height - 1, _radius); g.DrawPath(pWhite128, DrawingTool.RoundedRect(backgroundRect, _radius)); } }
protected override void OnPaint(PaintEventArgs e) { Color colorMenuHighLight = ControlPaint.LightLight(SystemColors.MenuHighlight); #region 定义 //显示图像的位置X坐标 int imageLocationX = 4; //显示图像的位置Y坐标 int imageLocationY = 2; //显示文本的位置X坐标 int textLocationX = 6; //显示文本的位置Y坐标 //int textLocationY = 4; int textLocationY = (int)Math.Round((float)(this.ContentRectangle.Height - (int)Math.Round(this.Font.SizeInPoints)) / 2); //子菜单显示文本位置X坐标 int textLocationX_DropDown = 33; //子菜单显示图像的位置X坐标 int imageLocationX_DropDown = 5; //子菜单显示图像的位置Y坐标 int imageLocationY_DropDown = 3; //文本填充 SolidBrush textBrush = new SolidBrush(this.ForeColor); //显示图像的Rectangle Rectangle imageRect = new Rectangle(imageLocationX, imageLocationY, 16, 16); //子菜单显示图像的Rectangle Rectangle imageRect_DropDown = new Rectangle(imageLocationX_DropDown, imageLocationY_DropDown, 16, 16); //菜单背景填充 SolidBrush backBrush_Normal = new SolidBrush(SystemColors.ControlLightLight); //菜单背景填充 选中状态 //LinearGradientBrush backBrush_Selected = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), // Color.FromArgb(255, 246, 204), Color.FromArgb(255, 194, 115)); LinearGradientBrush backBrush_Selected = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), Color.White, colorMenuHighLight); //菜单背景填充 按下状态 //LinearGradientBrush backBrush_Pressed = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), // Color.White, Color.LightSkyBlue); LinearGradientBrush backBrush_Pressed = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), Color.White, colorMenuHighLight); //子菜单左侧边条的填充 LinearGradientBrush leftBrush_DropDown = new LinearGradientBrush(new Point(0, 0), new Point(25, 0), Color.White, Color.FromArgb(233, 230, 215)); //填充Rectangle 顶层 Rectangle fillRect = new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height); //子菜单填充Rectangle Rectangle fillRect_DropDown = new Rectangle(2, 0, this.Bounds.Width - 4, this.Bounds.Height); //边框Rectangle 顶层 Rectangle drawRect = new Rectangle(0, 0, this.Bounds.Width - 1, this.Bounds.Height - 1); //子菜单边框Rectangle Rectangle drawRect_DropDown = new Rectangle(3, 0, this.Bounds.Width - 6, this.Bounds.Height - 2); //子菜单左侧与内容的分隔条 Pen leftLine = new Pen(Color.FromArgb(197, 194, 184)); //边框画笔 顶层 //Pen drawPen = new Pen(Color.FromArgb(255, 192, 111)); Pen drawPen = new Pen(SystemColors.GradientActiveCaption); //按下时的边框画笔 顶层 //Pen drawPen_Pressed = new Pen(Color.SkyBlue); Pen drawPen_Pressed = new Pen(SystemColors.GradientActiveCaption); //边框画笔 //Pen drawPen_DropDown = new Pen(Color.FromArgb(255, 192, 111)); Pen drawPen_DropDown = new Pen(SystemColors.GradientActiveCaption); #endregion #region 如果不可用,把文本填充色改成灰色,图片灰掉 //如果不可用,把文本填充色改成灰色 if (this.Enabled) { textBrush.Color = this.ForeColor; } else { textBrush.Color = Color.LightGray; } #endregion #region 顶层菜单 //如果是顶层菜单 if (!this.IsOnDropDown) { //如果是按下状态 if (this.Pressed) { e.Graphics.FillRectangle(backBrush_Pressed, fillRect); e.Graphics.DrawRectangle(drawPen_Pressed, drawRect); } //如果是选中状态 else if (this.Selected) { e.Graphics.FillRectangle(backBrush_Selected, fillRect); e.Graphics.DrawRectangle(drawPen, drawRect); } //绘制图像和文本 if (this.Image != null) { if (this.DisplayStyle == ToolStripItemDisplayStyle.ImageAndText) { if (this.Enabled) { e.Graphics.DrawImage(this.Image, imageRect); } else { ControlPaint.DrawImageDisabled(e.Graphics, this.Image, imageRect.X, imageRect.Y, this.BackColor); } e.Graphics.DrawString(this.Text, this.Font, textBrush, new Point(textLocationX + 14, textLocationY), stringFormat); } else if (this.DisplayStyle == ToolStripItemDisplayStyle.Image) { if (this.Enabled) { e.Graphics.DrawImage(this.Image, imageRect); } else { ControlPaint.DrawImageDisabled(e.Graphics, this.Image, imageRect.X, imageRect.Y, this.BackColor); } } else if (this.DisplayStyle == ToolStripItemDisplayStyle.Text) { e.Graphics.DrawString(this.Text, this.Font, textBrush, new Point(textLocationX, textLocationY), stringFormat); } } else { e.Graphics.DrawString(this.Text, this.Font, textBrush, new Point(textLocationX, textLocationY), stringFormat); } } #endregion #region 如果不是顶层菜单 //如果不是顶层菜单 else { #region 如果是选中或是按下状态 //如果是选中或是按下状态 if (this.Selected || this.Pressed) { //e.Graphics.FillRectangle(backBrush_Selected,fillRect_DropDown); e.Graphics.FillRectangle(backBrush_Normal, fillRect_DropDown); e.Graphics.FillRectangle(leftBrush_DropDown, 0, 0, 25, this.Height); e.Graphics.DrawLine(leftLine, 25, 0, 25, this.Height); //如果可用 if (this.Enabled) { //GraphPlotting.FillRoundRect(e.Graphics, backBrush_Selected, drawRect_DropDown, 0, 2); //GraphPlotting.DrawRoundRect(e.Graphics, drawPen_DropDown, drawRect_DropDown, 2); e.Graphics.FillPath(backBrush_Selected, DrawingTool.RoundedRect(drawRect_DropDown, 3)); e.Graphics.DrawPath(drawPen_DropDown, DrawingTool.RoundedRect(drawRect_DropDown, 3)); } if (this.Image != null) { //子菜单这里可以写在一起 //因为不管有没有图,文本的位置是不变的 if (this.DisplayStyle == ToolStripItemDisplayStyle.ImageAndText || this.DisplayStyle == ToolStripItemDisplayStyle.Image ) { if (this.Enabled) { e.Graphics.DrawImage(this.Image, imageRect_DropDown); } else { ControlPaint.DrawImageDisabled(e.Graphics, this.Image, imageRect_DropDown.X, imageRect_DropDown.Y, this.BackColor); } } } e.Graphics.DrawString(this.Text, this.Font, textBrush, new Point(textLocationX_DropDown, textLocationY), stringFormat); } #endregion #region 如果未选中也未按下 //如果未选中也未按下 else { e.Graphics.FillRectangle(backBrush_Normal, fillRect_DropDown); e.Graphics.FillRectangle(leftBrush_DropDown, 0, 0, 25, this.Height); e.Graphics.DrawLine(leftLine, 25, 0, 25, this.Height); if (this.Image != null) { if (this.DisplayStyle == ToolStripItemDisplayStyle.ImageAndText || this.DisplayStyle == ToolStripItemDisplayStyle.Image) { if (this.Enabled) { e.Graphics.DrawImage(this.Image, imageRect_DropDown); } else { ControlPaint.DrawImageDisabled(e.Graphics, this.Image, imageRect_DropDown.X, imageRect_DropDown.Y, this.BackColor); } } } e.Graphics.DrawString(this.Text, this.Font, textBrush, new Point(textLocationX_DropDown, textLocationY), stringFormat); } #endregion #region 处理Checked = true // ControlPaint.draw // MenuGlyph. if (this.Checked) { ControlPaint.DrawMenuGlyph (e.Graphics, imageLocationX_DropDown, imageLocationY_DropDown, 16, 16, MenuGlyph.Checkmark, Color.Black, SystemColors.GradientActiveCaption); } #endregion #region 如果还有子菜单,画向右箭头 if (this.DropDownItems.Count > 0) { ControlPaint.DrawMenuGlyph (e.Graphics, this.Width - 20, imageLocationY_DropDown, 16, 16, MenuGlyph.Arrow, Color.Black, Color.Transparent); } #endregion } #endregion #region 释放资源 //释放资源 textBrush.Dispose(); backBrush_Normal.Dispose(); backBrush_Selected.Dispose(); backBrush_Pressed.Dispose(); leftBrush_DropDown.Dispose(); leftLine.Dispose(); drawPen.Dispose(); drawPen_Pressed.Dispose(); drawPen_DropDown.Dispose(); #endregion }