翻譯|使用教程|編輯:況魚杰|2020-10-14 13:59:00.480|閱讀 482 次
概述:TX Text Control帶有預配置的對話框,用于選擇本地圖像并將其插入到文檔中。本文介紹如何實現擴展方法以使用自定義對話框插入圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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);
 
 
以下調用打開帶有“所有支持的格式”選項的對話框:
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);
 
 
關注慧聚IT微信公眾號 ???,了解產品的最新動態及最新資訊。
	 
 
	
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: