[转]控件的设置模式行为ControlDesigner

写在前面:有没有发现,一些设计得好的自定义winform控件,可以在拖控件到界面时,点击右键或右上角的小图标就可以设置一些重要的属性。这是比较设置模式行为ControlDesigner来实现的,你可以在自定义控件的class上面增加一些标记来达到这个功能。下面使用ControlDesigner的派生类ParentControlDesigner来实现Ide工具中的右键设置属性功能。

效果图
下图中,放着两个自定义控件SmartLine的实例,是两条直线,一条是水平的一条是垂直的,在控件上面点击右键,可以选择设置直线是水平还是垂直。使用控件右上角的小图标,也可以达到这样的效果。
 

源代码
事实上,你需要先对SmartLine的类,进行设置,设置它的设置模式Designer为SmartLineControlDesigner,而后者就是要介绍的源代码。设置方法如下:
    [Designer("LineProject.SmartLineControlDesigner", typeof(System.ComponentModel.Design.IDesigner))]
public class SmartLine : Control
写自定义控件的Designer其实是非常的简单的,找出你要设置的主要的属性,并修改下面的代码就可以了。下面的代码是针对Orientation布局属性而设置的Designer,简单的修改之后,你就可以定义自己的Designer了。最主要的方法是override DesignerVerbCollection Verbs这个方法是通过ControlDesigner继承下来的。重写时注意增加事件委托就行了。基本上,你可以抄下面的代码就可以了。而new SmartLine Component部分修改为你需要使用的控件。

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;

namespace LineProject
{
    /// <summary>
    /// 直线描述
    /// </summary>
    public class SmartLineControlDesigner : ParentControlDesigner
    {

ContractedBlock.gif ExpandedBlockStart.gif Code
       DesignerVerbCollection verbs;
        IComponentChangeService componentChangeService;
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 重写右键菜单
        
/// </summary>

        public override DesignerVerbCollection Verbs
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (verbs == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    verbs 
= new DesignerVerbCollection();
                    DesignerVerb enableAutoScrollScrollVerb 
= new DesignerVerb("水平放置直线"new EventHandler(OnOrientationChange));
                    DesignerVerb disableAutoScrollVerb 
= new DesignerVerb("垂直放置直线"new EventHandler(OnOrientationChange));                   
                   
                    verbs.Add(enableAutoScrollScrollVerb);
                    verbs.Add(disableAutoScrollVerb);
                }

                UpdateVerbsStatus();
                
return verbs;
            }

        }

ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 获取控件
        
/// </summary>

        public new SmartLine Component
ExpandedBlockStart.gifContractedBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return (SmartLine)base.Component; }
        }

ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 更新状态
        
/// </summary>

        protected void UpdateVerbsStatus()
ExpandedBlockStart.gifContractedBlock.gif        
{            
            verbs[
0].Enabled = Component.Orientation != Orientation.Horizontal;
            verbs[
1].Enabled = !verbs[0].Enabled;
        }

ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 控件改变服务
        
/// </summary>

        public IComponentChangeService ComponentChangeService
ExpandedBlockStart.gifContractedBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (componentChangeService == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    componentChangeService 
= (IComponentChangeService)GetService(typeof(IComponentChangeService));
                }

                
return componentChangeService;
            }

        }

       
ExpandedBlockStart.gifContractedBlock.gif        
/**//// <summary>
        
/// 界面布局改变
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        void OnOrientationChange(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Component.Orientation 
= (Component.Orientation == Orientation.Horizontal) ? Orientation.Vertical : Orientation.Horizontal;
                ComponentChangeService.OnComponentChanged(Component, 
nullnullnull);
            }

            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                UpdateVerbsStatus();
            }

        }

    }
}

posted on 2009-02-02 15:03  Jrong 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Jrong/archive/2009/02/02/1382491.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值