TX Text Control系列教程— WPF:自定义插入图像对话框

TX Text Control带有预配置的对话框,用于选择本地图像并将其插入到文档中。本文介绍如何实现扩展方法以使用自定义对话框插入图像。

TX Text Control .NET for WPF 分标准,专业,及企业三个版本,是一套专业的文字处理控件,它以可重复使用控件的形式为程序开发人员提供了丰富的文字处理功能。TX Text Control .NET for WPF教程是个连载教程,后期会持续更新,如果有任何疑问都可以在评论留言或者联系客服

TX Text Control .NET for WPF试用版


TX Text Control带有预配置的对话框,用于选择本地图像并将其插入到文档中。本文介绍如何实现扩展方法以使用自定义对话框插入图像。

扩展方式

以下扩展将方法CustomDialog添加到ImageCollection中。

public static class TextControlExtensions{    public static void CustomDialog (        this TXTextControl.ImageCollection images,        string preferredFormat = null,        bool showAll = true,        OpenFileDialog openFileDialog = null)    {        // create a new open file dialog or use the given one        OpenFileDialog dlg = (openFileDialog == null)            new OpenFileDialog() : openFileDialog;        // retrieve supported filters from Text Control        string sImportFormats = images.ImportFilters;        // add an "All Supported Formats" entry with all extensions        if (showAll) {            var sAllImportFormats = String.Join(";",                images.ImportFilters.Split('|')                    .Where((value, index) => index % 2 == 1)                    .ToArray());            sImportFormats = "All Supported Formats|" +                sAllImportFormats + "|" + sImportFormats;        }        // set the filters for the dialog        dlg.Filter = sImportFormats;        // select the pre-selected filter        if (preferredFormat != null) {            var saFinalFilters = dlg.Filter.Split('|').Where(                (value, index) => index % 2 == 1).ToArray();            // set the index (0-based)            dlg.FilterIndex = Array.FindIndex(saFinalFilters, m => m == preferredFormat) + 1;        }        // if file is opened by user, create a new image and insert it        if (dlg.ShowDialog() == true) {            TXTextControl.Image image = new TXTextControl.Image(                System.Drawing.Image.FromFile(dlg.FileName));            images.Add(image, -1); // at input position        }    }}

此方法接受3个参数:

参数 值类型 值说明
首选格式 String 应该预先选择的首选格式。 样本:“ *。png”。
显示所有 bool 指定是否应将其他“所有支持的格式”选项添加到列表中。
打开文件对话框 OpenFileDialog 可以预先配置一个OpenFileDialog对象以进行其他自定义。

扩展方法用法

以下代码将打开一个自定义对话框,该对话框具有首选格式PNG,并且没有“所有支持的格式”选项:

textControl1.Images.CustomDialog("*.png", false);

TX Text Control系列教程— WPF:自定义插入图像对话框

以下调用打开带有“所有支持的格式”选项的对话框:

textControl1.Images.CustomDialog(null, true);

如果要更改标题,默认目录和其他选项,则可以将预配置的OpenFileDialog传递给CustomDialog方法:

OpenFileDialog dlg = new OpenFileDialog();dlg.Title = "My custom title";dlg.InitialDirectory = "c:\";textControl1.Images.CustomDialog(null, false, dlg);

TX Text Control系列教程— WPF:自定义插入图像对话框

如果您对Text Control感兴趣,可以咨询在线客服>>购买正版授权软件。

关注慧聚IT微信公众号 了解产品的最新动态及最新资讯。

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年9月11日
下一篇 2020年9月11日

相关推荐

发表回复

登录后才能评论