网上截图程序及思路.doc_第1页
网上截图程序及思路.doc_第2页
网上截图程序及思路.doc_第3页
网上截图程序及思路.doc_第4页
网上截图程序及思路.doc_第5页
已阅读5页,还剩45页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#编写QQ截图控件(1)实现绘图工具栏控件之前写了一篇关于截图的文章(查看),只实现了简单的截图,接下的文章将介绍怎样一步步的实现一个完整的截图控件。这篇文章将介绍怎样实现绘图工具栏控件DrawToolsControl,先来了解一下这个工具栏控件包含些什么内容。因为只对截图实现添加一些简单的图形和文字绘制,所以只实现了添加矩形、椭圆、箭头、文字和线条,所以工具栏需要包含绘制矩形、椭圆、箭头、文字和线条按钮。因为还要实现撤销、保存截图等,所以工具栏还要添加撤销、保存、退出和保存当前图形的按钮。需要的按钮就这么多了,我们可以用ToolStrip来添加这些按钮,但是为了控件看起来更漂亮,需要对ToolStrip进行重绘,下面就具体怎样实现DrawToolsControl控件。第一步:继承UserControl控件,在上面添加一个ToolStrip,然后添加前面所说的所有按钮,调整好UserControl控件的大小,让它刚好可以显示完整的ToolStrip。第二步:添加相应按钮的点击事件,具体就不一一列出了。第三步:对UserControl进行重绘。先改变UserControl的Region,让它为圆角的样式,然后重绘背景和边框。第四步:对ToolStrip进行重绘。这是比较重要的,绘制好的ToolStrip还可以在其他地方使用。对ToolStrip实现重绘,需要继承ToolStripRenderer类实现一个新的ToolStripRendererEx类,根据需要,对OnRenderToolStripBackground、OnRenderButtonBackground和OnRenderSeparator方法进行重写,然后把ToolStrip的Renderer属性设置为新的ToolStripRendererEx就行了。来看看实现的关键代码:1、 UserControl设置Region和绘制代码:- protected override void OnPaint(PaintEventArgs e)/函数 base.OnPaint(e); Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; using (GraphicsPath path = GraphicsPathHelper.CreatePath( ClientRectangle, 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( ClientRectangle, 8, RoundStyle.All, true) g.DrawPath(pen, innerPath); - private void SetRegion()/函数 using (GraphicsPath path = GraphicsPathHelper.CreatePath( ClientRectangle, 8, RoundStyle.All, false) if (base.Region != null) base.Region.Dispose(); base.Region = new Region(path); -2、 ToolStripRendererEx重绘代码: protected override void OnRenderToolStripBackground( ToolStripRenderEventArgs e) Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; 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 OnRenderButtonBackground( ToolStripItemRenderEventArgs e) ToolStripButton item = e.Item as ToolStripButton; if (item != null) LinearGradientMode mode = e.ToolStrip.Orientation = Orientation.Horizontal ? LinearGradientMode.Vertical : LinearGradientMode.Horizontal; Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; Rectangle bounds = new Rectangle(Point.Empty, item.Size);if (item.BackgroundImage != null) Rectangle clipRect = item.Selected ? item.ContentRectangle : bounds; ControlPaintEx.DrawBackgroundImage(g, item.BackgroundImage, ColorTable.BackColorNormal, tem.BackgroundImageLayout, ounds, clipRect); if (item.CheckState = CheckState.Unchecked) if (item.Selected) Color color = ColorTable.BackColorHover; if (item.Pressed) color = ColorTable.BackColorPressed; RenderBackgroundInternal(g, bounds, color, ColorTable.BorderColor, ColorTable.BackColorNormal, RoundStyle.All, true, true, mode); return; else if (e.ToolStrip is ToolStripOverflow) using (Brush brush = new SolidBrush(ColorTable.BackColorNormal) g.FillRectangle(brush, bounds); return; else Color color = ControlPaint.Light(ColorTable.BackColorHover); if (item.Selected) color = ColorTable.BackColorHover; if (item.Pressed) color = ColorTable.BackColorPressed; RenderBackgroundInternal( e.Graphics, bounds, color, ColorTable.BorderColor, ColorTable.BackColorNormal, RoundStyle.All, true, true, mode); return; base.OnRenderButtonBackground(e); - 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); -单独的源码就先不传上来提供下载了,等整个项目写好后一起提供吧,看看DrawToolsControl的效果:C# 实现完整功能的截图控件(2)-实现颜色和字体选择控件 上一篇文章介绍了怎样实现绘图工具栏控件,这篇文章介绍截图控件需要用到的另一个控件,就是颜色和字体选择控件ColorSelector。有了它,在绘制图形和文字的时候,就可以选择不同的颜色和字体大小了。先来看下控件的最终效果,然后就来介绍怎样实现它。1、跟前面一样,还是继承UserControl控件,然后绘制它的背景和边框,因为前面有了,这里就不详细介绍了。 2、为了方便,实现一个ColorLabel控件用来显示颜色,ColorLabel控件继承Control,实现背景和边框的重绘,使它符合截图控件的整体风格。看看它的详细实现代码:public class ColorLabel : Control . Fields#region Fields private Color _borderColor = Color.FromArgb(65, 173, 236); #endregion Constructors#region Constructors public ColorLabel() : base() . SetStyles(); #endregion Properties#region Properties DefaultValue(typeof(Color),65, 173, 236) public Color BorderColor . get . return _borderColor; set . _borderColor = value; base.Invalidate(); protected override Size DefaultSize . get . return new Size(16, 16); #endregion Private Methods#region Private Methods private void SetStyles() . base.SetStyle( ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true); base.UpdateStyles(); #endregion OverideMethods#region OverideMethods protected override void OnPaint(PaintEventArgs e) . base.OnPaint(e); Graphics g = e.Graphics; Rectangle rect = ClientRectangle; using (SolidBrush brush = new SolidBrush(base.BackColor) . g.FillRectangle( brush, rect); ControlPaint.DrawBorder( g, rect, _borderColor, ButtonBorderStyle.Solid); rect.Inflate(-1, -1); ControlPaint.DrawBorder( g, rect, Color.White, ButtonBorderStyle.Solid); #endregion 3、在UserControl上添加上需要的一些控件,像ComboBox、ColorLabel等等,并把它们排列好,并添加相应的事件。4、给ColorSelector控件添加上相应的属性和事件,主要是ColorSelector的颜色样式(ColorTable)、选中的颜色(SelectedColor)和字体的大小(FontSize)这三个属性,选中颜色改变(ColorChanged)和字体大小选择改变(FontSizeChanged)这两个事件。看看代码:Events#region Events public event EventHandler ColorChanged . add . base.Events.AddHandler(EventColorChanged, value); remove . base.Events.RemoveHandler(EventColorChanged, value); public event EventHandler FontSizeChanged . add . base.Events.AddHandler(EventFontSizeChanged, value); remove . base.Events.RemoveHandler(EventFontSizeChanged, value); #endregionProperties#region Properties Browsable(false) DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) public CaptureToolStripColorTable ColorTable . get . if (_colorTable = null) . _colorTable = new CaptureToolStripColorTable(); return _colorTable; set . _colorTable = value; base.Invalidate(); SetColorLabelBorderColor(ColorTable.BorderColor); Browsable(false) public Color SelectedColor . get . return colorLabelSelected.BackColor; Browsable(false) public int FontSize . get . return int.Parse(comboBoxFontSize.Text); #endregion5、因为只有绘制文字的时候,才需要选择字体的大小,所以当不是绘制文字的时候,就把字体大小选择隐藏起来,绘制文字的时候就把它显示出来。用下面这两个函数来实现这个功能:Public Methods#region Public Methods public void Reset() . colorLabelSelected.BackColor = Color.Red; comboBoxFontSize.Text = 12; panelLeft.Visible = false; Width = 189; public void ChangeToFontStyle() . panelLeft.Visible = true; Width = 268; #endregion通过以上几个步骤,ColorSelector控件就实现了,源代码还是等到整个截图控件全部完成的时候一起放出来吧C# 实现完整功能的截图控件(3)-实现漂亮的快捷菜单 前面的两篇文章已经实现了两个截图控件所需要的控件:绘图工具栏控件和颜色、字体选择控件,这篇文章将介绍截图控件所需的最后一个控件快捷菜单,这个控件不需要做太多的工作,用ContextMenuStrip菜单控件就行了,需要做的只是对它进行美化,使它跟整个截图控件的控件风格保持一致,来看看最终的效果:介绍实现绘图工具栏控件的那篇文章中,介绍了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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论