翻譯|使用教程|編輯:況魚杰|2020-10-14 13:59:00.480|閱讀 482 次
概述:TX Text Control帶有預(yù)配置的對(duì)話框,用于選擇本地圖像并將其插入到文檔中。本文介紹如何實(shí)現(xiàn)擴(kuò)展方法以使用自定義對(duì)話框插入圖像。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
TX Text Control .NET for WPF 分標(biāo)準(zhǔn),專業(yè),及企業(yè)三個(gè)版本,是一套專業(yè)的文字處理控件,它以可重復(fù)使用控件的形式為程序開發(fā)人員提供了豐富的文字處理功能。TX Text Control .NET for WPF教程是個(gè)連載教程,后期會(huì)持續(xù)更新,如果有任何疑問都可以在評(píng)論留言或者聯(lián)系。
點(diǎn)擊下載TX Text Control .NET for WPF試用版
TX Text Control帶有預(yù)配置的對(duì)話框,用于選擇本地圖像并將其插入到文檔中。本文介紹如何實(shí)現(xiàn)擴(kuò)展方法以使用自定義對(duì)話框插入圖像。
擴(kuò)展方式
以下擴(kuò)展將方法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個(gè)參數(shù):
參數(shù)
值類型
值說(shuō)明
首選格式
String
應(yīng)該預(yù)先選擇的首選格式。 樣本:“ *。png”。
顯示所有
bool
指定是否應(yīng)將其他“所有支持的格式”選項(xiàng)添加到列表中。
打開文件對(duì)話框
OpenFileDialog
可以預(yù)先配置一個(gè)OpenFileDialog對(duì)象以進(jìn)行其他自定義。
擴(kuò)展方法用法
以下代碼將打開一個(gè)自定義對(duì)話框,該對(duì)話框具有首選格式PNG,并且沒有“所有支持的格式”選項(xiàng):
textControl1.Images.CustomDialog("*.png", false);
以下調(diào)用打開帶有“所有支持的格式”選項(xiàng)的對(duì)話框:
textControl1.Images.CustomDialog(null, true);
如果要更改標(biāo)題,默認(rèn)目錄和其他選項(xiàng),則可以將預(yù)配置的OpenFileDialog傳遞給CustomDialog方法:
OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "My custom title"; dlg.InitialDirectory = "c:\\"; textControl1.Images.CustomDialog(null, false, dlg);
關(guān)注慧聚IT微信公眾號(hào) ???,了解產(chǎn)品的最新動(dòng)態(tài)及最新資訊。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn
文章轉(zhuǎn)載自: