TX Textcontrol 使用总结三——禁用右键、模版合并
一.Tx Textcontrol如何禁用右键快捷菜单?
==》
添加txContent_TextContextMenuOpening事件,实现方式如下所示:
private void txContent_TextContextMenuOpening(object sender, TextContextMenuEventArgs e)
{
e.Cancel = true;
}
二.模版合并
1.在TxControl内容结尾处进行追加模版信息
如下所示:
int length = txContent.Text.Length;
this.txContent.Select(length, 0);
if (FilePath.Contains(".tx"))
this.txContent.Selection.Load(FilePath, TXTextControl.StreamType.InternalFormat);
else
this.txContent.Selection.Load(FilePath, TXTextControl.StreamType.MSWord);
2.在鼠标所在位置合并
实现方式如下:
/// <summary>
/// 光标处添加模版
/// </summary>
/// <param name="bytes">模版信息</param>
private void InsetTemplateToInputPosition(byte[] bytes)
{
if (bytes == null) return;
int input = this.txContent.InputPosition.TextPosition;
PubHelper.Instance.CreateDir(Application.StartupPath + "\\temp");
string path = Application.StartupPath + "\\temp\\" + Guid.NewGuid() + ".tx";
try
{
path = Helper.Instance.ByteConvertWord(bytes, path);
this.txContent.Selection.Load(path, TXTextControl.StreamType.InternalFormat);
}
catch
{
return;
}
finally
{
Helper.Instance.DeleteFile(path);
}
}
--------------------------------辅助方法如下所示-----------------------
/// <summary>
/// word文件转换二进制数据(用于保存数据库)
/// </summary>
/// <param name="wordPath">word文件路径</param>
/// <returns>二进制</returns>
public byte[] WordConvertByte(string wordPath)
{
if (!File.Exists(wordPath))
{
return null;
}
byte[] bytContent = null;
System.IO.FileStream fs = null;
System.IO.BinaryReader br = null;
try
{
fs = new FileStream(wordPath, System.IO.FileMode.Open);
br = new BinaryReader((Stream)fs);
bytContent = br.ReadBytes((Int32)fs.Length);
fs.Close();
br.Close();
}
catch
{
fs.Close();
br.Close();
return null;
}
return bytContent;
}
/// <summary>
///二进制数据转换为word文件
/// </summary>
/// <param name="data">二进制数据</param>
/// <param name="fileName">word文件名</param>
/// <returns>word保存的相对路径</returns>
public string ByteConvertWord(byte[] data, string fileName)
{
if (data == null) return string.Empty;
FileStream fs = null;
BinaryWriter br = null;
try
{
fs = new FileStream(fileName, FileMode.OpenOrCreate);
br = new BinaryWriter(fs);
br.Write(data, 0, data.Length);
br.Close();
fs.Close();
}
catch
{
br.Close();
fs.Close();
return string.Empty;
}
return fileName;
}
/// <summary>
/// 删除指定的文件
/// </summary>
/// <param name="fileName">文件路径+名称</param>
public void DeleteFile(string fileName)
{
if (File.Exists(fileName)) File.Delete(fileName);
}
3.换页合并模版(合并模版时内容不满一页的直接进入下一页)
实现方式如下:
int length = txContent.Text.Length;
this.txContent.InputPosition = new TXTextControl.InputPosition(length,
TXTextControl.TextFieldPosition.OutsideTextField);
this.txContent.Sections.Add(TXTextControl.SectionBreakKind.BeginAtNewPage);
this.txContent.Select(this.txContent.GetPages()[this.txContent.Pages - 1].Start + length, 0);
LoadTx();
==》
private void LoadTx()
{
if (FilePath.Contains(".tx"))
this.txContent.Selection.Load(FilePath, TXTextControl.StreamType.InternalFormat);
else
this.txContent.Selection.Load(FilePath, TXTextControl.StreamType.MSWord);
}
public void CombineTxToNextPage(TextControl control, string filePath)
{
if (!File.Exists(filePath)) return;
int length = control.Text.Length;
control.InputPosition = new TXTextControl.InputPosition(length,
TXTextControl.TextFieldPosition.OutsideTextField);
control.Sections.Add(TXTextControl.SectionBreakKind.BeginAtNewPage);
control.Select(control.GetPages()[control.Pages - 1].Start + length, 0);
//使用 Selection.Load 方法加载第二个文档
control.Selection.Load(filePath, TXTextControl.StreamType.InternalFormat);
}
三.实例如下
using System;
using System.Windows.Forms;
using TXTextControl;
namespace CombineDocs
{
public partial class Form1 : Form
{
#region load
private string FilePath = "..\\..\\ok.tx";
public Form1()
{
InitializeComponent();
}
private void LoadTx()
{
if (FilePath.Contains(".tx"))
this.txContent.Selection.Load(FilePath, TXTextControl.StreamType.InternalFormat);
else
this.txContent.Selection.Load(FilePath, TXTextControl.StreamType.MSWord);
}
private void Form1_Load(object sender, EventArgs e)
{
LoadTx();
}
#endregion
private void 加载1月总结文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
int length = txContent.Text.Length;
this.txContent.Select(length, 0);
LoadTx();
}
private void 加载2月计划文档ToolStripMenuItem_Click(object sender, EventArgs e)
{
int length = txContent.Text.Length;
this.txContent.InputPosition = new TXTextControl.InputPosition(length,
TXTextControl.TextFieldPosition.OutsideTextField);
this.txContent.Sections.Add(TXTextControl.SectionBreakKind.BeginAtNewPage);
this.txContent.Select(this.txContent.GetPages()[this.txContent.Pages - 1].Start + length, 0);
LoadTx();
}
private void txContent_TextContextMenuOpening(object sender, TextContextMenuEventArgs e)
{
e.Cancel = true;
}
}
}
注意:此处只做Demo演示,不做代码规范处理......
最新文章
- applicationContext.xml
- 关于在DataGrid.RowDetailsTemplate中的控件查找不到的问题
- CSS3 transform 属性详解(skew, rotate, translate, scale)
- FSL - MELODIC
- mysql中的if判断
- 30天轻松学习javaweb_tomcat的虚拟目录设置
- Validform使用
- Spring Annotation注解进行aop的学习
- iOS开发工具箱
- Arrays和Collections 对于sort的不同实现原理
- Go语言库之strconv包(转载自--http://blog.csdn.net/alvine008/article/details/51283189)
- Python36 二进制文件读写问题
- VsVim的快捷键
- mysql 5.7 线程阻塞处理
- ActiveX多线程回调JavaScript
- Populating Next Right Pointers in Each Node II leetcode java
- Java lambda 表达式
- 【Docker】docker 入门以及一些常用指令
- NPM Scripts -- onchange parallelshell
- 【学习笔记】dp基础
热门文章
- Sublime Text插件:HTML+CSS+JAVASCRIPT+JSON快速格式化
- gpg --verify之";Can&#39;t check signature: No public key";
- BIOS设置
- JavaScript substring() 方法
- C++ Primer : 第九章 : vector变长、string的其他操作以及容器适配器
- SQLServer如何用T—SQL命令查询一个数据库中有哪些表
- puppet安装配置及使用
- URAL 1077 Travelling Tours(统计无向图中环的数目)
- ed编辑器使用
- SQL Debugging