开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 5290|回复: 0

【教程】关于SpreadsheetControl和RichEditControl的CommandService的用法

[复制链接]

0

精华

5273

贡献

5991

赞扬

管理员

帖子
1172
软币
22475
在线时间
4451 小时
注册时间
2013-6-7

黄马甲

发表于 2018-3-30 17:09:23 | 显示全部楼层 |阅读模式
我们在使用SpreadsheetControl或者RichEditControl的时候可能会需要对此类控件的系统事件做一些定制和干预,比如我们需要在文档保存前判断一下文档内容的大小,或者在文件打开的判断一下内容是否包含某些敏感词等等,该如何操作呢?
这时候我们就需要用到它们各自的CommandService了,话不多说,直接上代码!

SpreadsheetControl:
[C#] 纯文本查看 复制代码
public static class MyCommandHelper
{
    public static void SetMyNewCommandFactory(SpreadsheetControl control)
    {
        ISpreadsheetCommandFactoryService service = (ISpreadsheetCommandFactoryService)control.GetService(typeof(ISpreadsheetCommandFactoryService));
        CustomService customService = new CustomService(service);
        control.ReplaceService<ISpreadsheetCommandFactoryService>(customService);
        customService.Control = control;
    }
}
public class CustomService : SpreadsheetCommandFactoryServiceWrapper
{
    public CustomService(ISpreadsheetCommandFactoryService service) : base(service) { }
    public SpreadsheetControl Control { get; set; }
    public override SpreadsheetCommand CreateCommand(SpreadsheetCommandId id)
    {
        if (id == SpreadsheetCommandId.FileSave)
            return new CustomSaveDocumentCommandCommand(Control);
        return base.CreateCommand(id);
    }
}
public class CustomSaveDocumentCommandCommand : SaveDocumentCommand
{
    public CustomSaveDocumentCommandCommand(ISpreadsheetControl control) : base(control) { }
    protected override void ExecuteCore()
    {
        if (Control.ActiveSheet.Name == "test")
            MessageBox.Show("表名不合法不让保存!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        else
            base.ExecuteCore();
    }
}

//调用:
MyCommandHelper.SetMyNewCommandFactory(spreadsheetControl1);



RichEditControl:
[C#] 纯文本查看 复制代码
 
public static class MyCommandHelper
{
    public static void SetMyNewCommandFactory(RichEditControl control)
    {
        var myCommandFactory = new CustomRichEditCommandFactoryService(control, control.GetService<IRichEditCommandFactoryService>());
        control.ReplaceService<IRichEditCommandFactoryService>(myCommandFactory);
    }
}
public class CustomRichEditCommandFactoryService : IRichEditCommandFactoryService
{
    readonly IRichEditCommandFactoryService service;
    readonly RichEditControl control;
    public CustomRichEditCommandFactoryService(RichEditControl control,
        IRichEditCommandFactoryService service)
    {
        this.control = control;
        this.service = service;
    }
    public RichEditCommand CreateCommand(RichEditCommandId id)
    {
        if (id == RichEditCommandId.FileSave)
            return new CustomSaveDocumentCommand(control);
        return service.CreateCommand(id);
    }
}
public class CustomSaveDocumentCommand : SaveDocumentCommand
{
    public CustomSaveDocumentCommand(IRichEditControl control) : base(control) { }
    protected override void UpdateUIStateCore(ICommandUIState state)
    {
        base.UpdateUIStateCore(state);
        state.Enabled = Control.Document.Paragraphs.Count < 7;
    }
    protected override void ExecuteCore()
    {
        if (Control.Document.Paragraphs.Count > 7)
            MessageBox.Show("段落太多不让保存!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        else
            base.ExecuteCore(); 
    }
}

//调用:
MyCommandHelper.SetMyNewCommandFactory(richEditControl1);

大家还可以通过判断它们各自的CommandId来实现不同的功能干预。是不是很简单呢?

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-11-25 12:18

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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