http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 18320|回复: 3

[C#皮肤] C#皮肤-ContextMenuStrip 控件实现

[复制链接]
发表于 2013-3-5 12:52:47 | 显示全部楼层 |阅读模式
      C#皮肤-ContextMenuStrip 控件实现

导读部分
-------------------------------------------------------------------------------------------------------------
C#皮肤-实现原理系列文章导航
http://www.sufeinet.com/thread-2-1-1.html

看看效果先

这个控件是在系统控件ContextMenuStrip的基础之上来实现的,其实就是一个快捷菜单,也可以当成是左键菜单 来使用,具本的用户看文章最好面的详细介绍,我们先来看一下实现后的效果
1.当成下拉和快捷菜单使用时的效果
ContextMenuStrip1.png        ContextMenuStrip2.png
2.当成右键菜单使用时效果
ContextMenuStrip3.png       ContextMenuStrip4.png   
来看看实现


我们先来看看实现 的情况 吧,
我们第一步要打开我们的项目,然后右击项目,新添加一个Component组件。然后继承一下ContextMenuStrip类
代码如下
[code=csharp]public class ContextMenuStrip : System.Windows.Forms.ContextMenuStrip[/code]
接来来一步我们来设置一下这个快捷菜单 吧
[code=csharp]//可以使用一个图标来表示控件 ContextMenuStrip快捷菜单  
    [ToolboxBitmap(typeof(System.Windows.Forms.ContextMenuStrip))][/code]
我们来看一下两个基本的构造器是怎么来实现 的吧,多说没有用,代码很简单一起来看一下吧
[code=csharp]//构造器
        public ContextMenuStrip()
            : base()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            //Office格式颜色
            ProfessionalColorTable ColorTable = new ProfessionalColorTable();
            ColorTable.UseSystemColors = true;
            //是否为设计模式
            if (!DesignMode)
            {
                this.Renderer = new ToolStripRenderer(ColorTable);
            }
            else
            {
                this.Renderer = new ToolStripProfessionalRenderer(ColorTable);
            }
            this.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.Font = new Font(this.Font.FontFamily, 10);
        }

        public ContextMenuStrip(IContainer container)
            : base(container)
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            ProfessionalColorTable ColorTable = new ProfessionalColorTable();
            ColorTable.UseSystemColors = true;
            if (!DesignMode)
            {
                this.Renderer = new ToolStripRenderer(ColorTable);
            }
            else
            {
                this.Renderer = new ToolStripProfessionalRenderer(ColorTable);
            }
            this.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
            this.Font = new Font(this.Font.FontFamily, 10);
        }[/code]

大家可以看到我在构造的时候预设了一些东东哦,呵呵,可以打开皮肤的源代码查看一下,或是单步调试一下看看效果
再来得写一下重写OnCreateControl()方法
[code=csharp]/// <summary>
        /// 重写OnCreateControl
        /// </summary>
        protected override void OnCreateControl()
        {
            base.OnCreateControl();

            if (!DesignMode)
            {
                int Rgn = Win32.CreateRoundRectRgn(1, 1, ClientSize.Width , Height , 7, 7);
                Win32.SetWindowRgn(this.Handle, Rgn, true);
            }

            int result = Win32.SetClassLong(this.Handle, Win32.GCL_STYLE, 0);
        }
[/code]
为了更好的打配我们的皮肤和效果我们得重写一下重写OnPaint事件
[code=csharp]/// <summary>
        /// 重写OnPaint事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            if (!DesignMode)
            {
                Graphics g = e.Graphics;

                SolidBrush bruch = new SolidBrush(Shared.MainForm.MainFormBackGroundColor);
               
                g.FillRectangle(bruch, 0, 0, 25, this.Height);

                bruch = new SolidBrush(Color.White);
                g.FillRectangle(bruch, 27, 0, this.Width - 27, this.Height);

                Pen pen = new Pen(Shared.MainForm.MainFormBackGroundColor2, 1f);
                e.Graphics.DrawPath(pen, CreateRoundedRectanglePath(new Rectangle(1, 1, ClientSize.Width-3, Height-3), 5));
            }

            base.OnPaint(e);

        }[/code]
这里面用到的CreateRoundedRectanglePath()方法代码如下,大家知道GraphicsPath是代表的一系统列的相互连接在一起的直线或 是曲线。处理的方式代码如下所示
[code=csharp]/// <summary>
        /// CreateRoundedRectanglePath
        /// </summary>
        /// <param name="rect">Rectangle</param>
        /// <param name="cornerRadius"int></param>
        /// <returns></returns>
        internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
        {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
            roundedRect.CloseFigure();
            return roundedRect;
        } [/code]

代码不是很多,有兴趣的可以调试看一下

下面我主要来说一下用做快捷菜单和右键菜单时的使用方法

第一种是当成右键菜单
    当我们把皮肤的源代码放入自己的项目里时,只要生成一下就会在工具箱里出现一个名为CRD.WinUI组件的选项卡,下面有这样一系统的控件

ContextMenuStrip5.png
我们所看到的当前选中的就是ContextMenuStrip 控件,我们只 要把他拉到我们的窗体或是用户控件上就OK了,使用方法和系统的是一样的,找到要使用这个菜单的控件在控件的ContextMenuStrip属性选择拉过来的控件就行了。

第二种是当成快捷菜单来使用也就是下拉菜单的形式来使用
  我就是我们的皮肤主窗体里标题栏上的菜单与这个配合使用的方法来看看吧
我们先是要看控件拉到主窗体之上,然后把控件的对象指定给主窗体的this.SkinContextMenuStrip1属性就可以了,具体this.SkinContextMenuStrip1和上面的菜单 是怎么配合的这个就大家参考主窗体的实现或是直接去看源代码
代码如下
[code=csharp]//指定菜单
            this.SkinContextMenuStrip1 = contextMenuStrip2;[/code]

本帖被以下淘专辑推荐:

  • · 皮肤|主题: 15, 订阅: 0


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-3-5 14:09:19 | 显示全部楼层
嗯。不错哦。
发表于 2013-3-5 19:25:47 | 显示全部楼层
还可以
发表于 2015-4-23 18:23:45 | 显示全部楼层
站长积累了不少好东西呀,
我现在作一个小的应用,跟站长学了不少。非常感谢哦。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 03:05

© 2014-2021

快速回复 返回顶部 返回列表