一.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);
}

三.实例如下

TX Textcontrol 使用总结三——禁用右键、模版合并-LMLPHP

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演示,不做代码规范处理......

04-14 04:35