【转】C# 实现完整功能的截图控件(3)-实现漂亮的快捷菜单
2010-09-27 08:52
621 查看
前面的两篇文章已经实现了两个截图控件所需要的控件:绘图工具栏控件和颜色、字体选择控件,这篇文章将介绍截图控件所需的最后一个控件——快捷菜单,这个控件不需要做太多的工作,用ContextMenuStrip菜单控件就行了,需要做的只是对它进行美化,使它跟整个截图控件的控件风格保持一致,来看看最终的效果:
介绍实现绘图工具栏控件的那篇文章中,介绍了ToolStrip的美化,因为ContextMenuStrip也是通过继承ToolStrip来实现的控件,所以ContextMenuStrip的美化跟它一样,只需重写ToolStripRenderer的几个绘制方法就行了。还是利用ToolStrip的美化的时候实现的类ToolStripRendererEx,重写相应的方法,来看看代码:
最后,把ContextMenuStrip的Renderer设为ToolStripRendererEx就行了。下一篇文章将介绍实现完整的截图控件,并提供完整的源代码下载。
介绍实现绘图工具栏控件的那篇文章中,介绍了ToolStrip的美化,因为ContextMenuStrip也是通过继承ToolStrip来实现的控件,所以ContextMenuStrip的美化跟它一样,只需重写ToolStripRenderer的几个绘制方法就行了。还是利用ToolStrip的美化的时候实现的类ToolStripRendererEx,重写相应的方法,来看看代码:
protected override void OnRenderToolStripBackground( ToolStripRenderEventArgs e) ...{ Color baseColor = ColorTable.BackColorNormal; ToolStrip toolStrip = e.ToolStrip; Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; if (toolStrip is ToolStripDropDown) ...{ RegionHelper.CreateRegion(e.ToolStrip, e.AffectedBounds); Rectangle rect = e.AffectedBounds; using (GraphicsPath path = GraphicsPathHelper.CreatePath( rect, 8, RoundStyle.All, false)) ...{ using (SolidBrush brush = new SolidBrush( ColorTable.BackColorNormal)) ...{ g.FillPath(brush, path); } using (Pen pen = new Pen(ColorTable.BorderColor)) ...{ g.DrawPath(pen, path); using (GraphicsPath innerPath = GraphicsPathHelper.CreatePath( rect, 8, RoundStyle.All, true)) ...{ g.DrawPath(pen, innerPath); } } } } else ...{ LinearGradientMode mode = e.ToolStrip.Orientation == Orientation.Horizontal ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal; RenderBackgroundInternal( g, e.AffectedBounds, ColorTable.BackColorHover, ColorTable.BorderColor, ColorTable.BackColorNormal, RoundStyle.All, false, true, mode); } } protected override void OnRenderSeparator( ToolStripSeparatorRenderEventArgs e) ...{ Rectangle rect = e.Item.ContentRectangle; if (e.ToolStrip is ToolStripDropDown) ...{ if (e.Item.RightToLeft == RightToLeft.Yes) ...{ //rect.X -= OffsetMargin + 4; } else ...{ rect.X += OffsetMargin + 4; } rect.Width -= OffsetMargin + 8; } RenderSeparatorLine( e.Graphics, rect, ColorTable.BackColorPressed, ColorTable.BackColorNormal, SystemColors.ControlLightLight, e.Vertical); } protected override void OnRenderMenuItemBackground( ToolStripItemRenderEventArgs e) ...{ if (!e.Item.Enabled) ...{ return; } Graphics g = e.Graphics; Rectangle rect = new Rectangle(Point.Empty, e.Item.Size); g.SmoothingMode = SmoothingMode.AntiAlias; if (e.Item.RightToLeft == RightToLeft.Yes) ...{ rect.X += 4; } else ...{ rect.X += OffsetMargin + 4; } rect.Width -= OffsetMargin + 8; rect.Height--; if (e.Item.Selected) ...{ RenderBackgroundInternal( g, rect, ColorTable.BackColorHover, ColorTable.BorderColor, ColorTable.BackColorNormal, RoundStyle.All, true, true, LinearGradientMode.Vertical); } else ...{ base.OnRenderMenuItemBackground(e); } } protected override void OnRenderImageMargin( ToolStripRenderEventArgs e) ...{ if (e.ToolStrip is ToolStripDropDownMenu) ...{ Rectangle rect = e.AffectedBounds; Graphics g = e.Graphics; rect.Width = OffsetMargin; if (e.ToolStrip.RightToLeft == RightToLeft.Yes) ...{ rect.X -= 2; } else ...{ rect.X += 2; } rect.Y += 1; rect.Height -= 2; g.SmoothingMode = SmoothingMode.AntiAlias; using (LinearGradientBrush brush = new LinearGradientBrush( rect, ColorTable.BackColorHover, Color.White, 90f)) ...{ Blend blend = new Blend(); blend.Positions = new float[] ...{ 0f, .2f, 1f }; blend.Factors = new float[] ...{ 0f, 0.1f, .9f }; brush.Blend = blend; rect.Y += 1; rect.Height -= 2; using (GraphicsPath path = GraphicsPathHelper.CreatePath( rect, 8, RoundStyle.All, false)) ...{ g.FillPath(brush, path); } } g.TextRenderingHint = TextRenderingHint.AntiAlias; StringFormat sf = new StringFormat(StringFormatFlags.NoWrap); Font font = new Font( e.ToolStrip.Font.FontFamily, 11, FontStyle.Bold); sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; sf.Trimming = StringTrimming.EllipsisCharacter; g.TranslateTransform(rect.X, rect.Bottom); g.RotateTransform(270f); if (!string.IsNullOrEmpty(MenuLogoString)) ...{ Rectangle newRect = new Rectangle( rect.X, rect.Y, rect.Height, rect.Width); using (Brush brush = new SolidBrush(ColorTable.ForeColor)) ...{ g.DrawString( MenuLogoString, font, brush, newRect, sf); } } g.ResetTransform(); return; } base.OnRenderImageMargin(e); } protected override void OnRenderItemImage( ToolStripItemImageRenderEventArgs e) ...{ Graphics g = e.Graphics; g.InterpolationMode = InterpolationMode.HighQualityBilinear; if (e.Item is ToolStripMenuItem) ...{ ToolStripMenuItem item = (ToolStripMenuItem)e.Item; if (item.Checked) ...{ return; } Rectangle rect = e.ImageRectangle; if (e.Item.RightToLeft == RightToLeft.Yes) ...{ rect.X -= OffsetMargin + 2; } else ...{ rect.X += OffsetMargin + 2; } ToolStripItemImageRenderEventArgs ne = new ToolStripItemImageRenderEventArgs( e.Graphics, e.Item, e.Image, rect); base.OnRenderItemImage(ne); return; } base.OnRenderItemImage(e); } protected override void OnRenderItemText( ToolStripItemTextRenderEventArgs e) ...{ e.TextColor = ColorTable.ForeColor; if (!(e.ToolStrip is MenuStrip) && (e.Item is ToolStripMenuItem)) ...{ Rectangle rect = e.TextRectangle; if (e.Item.RightToLeft == RightToLeft.Yes) ...{ rect.X -= 16; } else ...{ rect.X += 16; } e.TextRectangle = rect; } base.OnRenderItemText(e); }
最后,把ContextMenuStrip的Renderer设为ToolStripRendererEx就行了。下一篇文章将介绍实现完整的截图控件,并提供完整的源代码下载。
相关文章推荐
- C# 实现完整功能的截图控件(1)-实现绘图工具栏控件 控件实现了截图、绘制矩形、圆形、箭头、线条、文字,还需要可以撤销绘制步骤、保存图形等功能,基本实现了跟QQ2008截图一样的功能,还可以更
- C# 实现完整功能的截图控件(1)-实现绘图工具栏控件
- C# 实现完整功能的截图控件(2)-实现颜色和字体选择控件
- 【转】C# 实现完整功能的截图控件(1)-实现绘图工具栏控件
- 【转】C# 实现完整功能的截图控件(2)-实现颜色和字体选择控件
- 【转】C# 实现完整功能的截图控件(4)-完整版
- C# 实现完整功能的截图控件(1)-实现绘图工具栏控件(转载)
- C# 实现完整的仿QQ截图控件
- C# 实现完整的仿QQ截图控件
- C#中使用repeater控件实现List的clear,add,update,delete功能之二数据库
- 怎样用C#实现完整文档打印功能
- C# 中代码Dialog控件实现(打开,保存,改变字体,颜色,打印功能)
- jquery.Table实现的翻页功能比较完整漂亮,本想扩展个模版DIV
- C#初学者——鹰眼功能的完整实现
- 怎样用C#实现完整文档打印功能
- Qt实现截图选择框(仅实现选择框创建,非完整的截图功能)并发射信号给调用的槽函数
- 怎样用C#实现完整文档打印功能
- 怎样用C#实现完整文档打印功能
- C# GDI+ 简单绘图 (三) 实现仿QQ截图功能
- C#第十周任务之最后一项之创建一个如下的窗体,并在窗体上放置一个菜单、一个工具栏控件。菜单内容如第二个图所示。工具栏上有两个按钮,分别对应“打开文本文件”、“保存文本文件”。菜单和工具栏具体功能实现可