我是初學C#,在公司寫一個郵件發送程式時,需要把簡體轉換成繁體。
可是按照VS2005中自帶的轉換不是內碼轉換,最后使用了Word2003中的物件進行了編程,代碼非常簡單,希望哪里不妥直接指出。別忘了加個word引用,我的版本是word object library11.0
1 using Word;
2
3 Object template = Type.Missing;
4 Object newTemplate = Type.Missing;
5 Object documentType = Type.Missing;
6 Object visible = Type.Missing;
7 Microsoft.Office.Interop.Word.Application aWord = null;
8 Document doc = null;
9 Object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
10 Object originalFormat = Type.Missing;
11 Object routeDocument = Type.Missing;
12
13 string sAsInputtemp = asInput;
14 try
15 {
16 aWord = new Microsoft.Office.Interop.Word.Application();
17 doc = aWord.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
18 aWord.Selection.TypeText(asInput);
19 //繁体文字转换成為简体文字,逐詞转换
20 aWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true);
21 aWord.ActiveDocument.Select();
22 sAsInputtemp = aWord.Selection.Text;
23 }
24 catch (COMException ex) { throw ex; }
25 finally
26 {
27 doc.Close(ref saveChanges,
28 ref originalFormat, ref routeDocument);
29 aWord.Quit(ref saveChanges,
30 ref originalFormat, ref routeDocument);
31 }
上面是關鍵代碼,sAsInputtemp 是已經轉換好的文字信息。大家可以隨意修改。
里面的監視剪貼板還沒有寫上去,代碼也很簡單!
Code
/**//*
* Created by SharpDevelop.
* User: Administrator
* Date: 2006/10/30
* Time: ?? 09:00
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using FormApp=System.Windows.Forms.Application;
using Word;
using System.Runtime.InteropServices;
namespace ConvertClipboard
{
/**//// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm
{
public bool bCheck = true;
private bool bShow = true;
private string sTemp = null;
public static int MouseX;
public static int MouseY;
public static bool isMove;
public static int CurrX;
public static int CurrY;
[STAThread]
//監視剪貼板
//主程式
public static void Main(string[] args)
{
FormApp.EnableVisualStyles();
FormApp.SetCompatibleTextRenderingDefault(false);
FormApp.Run(new MainForm());
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
/**//*
Writer:20061008James
Purpose:简繁体文字转换
OutPut:string
Example:Null
Memo:Null
*/
private string ConverTCSC(string asInput)
{
Object template = Type.Missing;
Object newTemplate = Type.Missing;
Object documentType = Type.Missing;
Object visible = Type.Missing;
Word.Application aWord = null;
Document doc = null;
Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
Object originalFormat = Type.Missing;
Object routeDocument = Type.Missing;
string sAsInputtemp = asInput;
try
{
aWord = new Word.Application();
doc = aWord.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
aWord.Selection.TypeText(asInput);
//繁体文字转换成為简体文字,逐詞转换
aWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionTCSC, true, true);
aWord.ActiveDocument.Select();
sAsInputtemp = aWord.Selection.Text;
}
catch (COMException ex) { throw ex; }
finally
{
doc.Close(ref saveChanges,
ref originalFormat, ref routeDocument);
aWord.Quit(ref saveChanges,
ref originalFormat, ref routeDocument);
}
return sAsInputtemp;
}
/**//*
Writer:20061008James
Purpose:简繁体文字转换
OutPut:string
Example:Null
Memo:Null
*/
private string ConverSCTC(string asInput)
{
Object template = Type.Missing;
Object newTemplate = Type.Missing;
Object documentType = Type.Missing;
Object visible = Type.Missing;
Word.Application aWord = null;
Document doc = null;
Object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
Object originalFormat = Type.Missing;
Object routeDocument = Type.Missing;
string sAsInputtemp = asInput;
try
{
aWord = new Word.Application();
doc = aWord.Documents.Add(ref template, ref newTemplate, ref documentType, ref visible);
aWord.Selection.TypeText(asInput);
//简体文字转换成為繁体文字,逐詞转换
aWord.Selection.Range.TCSCConverter(WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC, true, true);
aWord.ActiveDocument.Select();
sAsInputtemp = aWord.Selection.Text;
}
catch (COMException ex) { throw ex; }
finally
{
doc.Close(ref saveChanges,
ref originalFormat, ref routeDocument);
aWord.Quit(ref saveChanges,
ref originalFormat, ref routeDocument);
}
return sAsInputtemp;
}
void BtnCSClick(object sender, System.EventArgs e)
{
lablMessage.Visible = false;
sTemp = Clipboard.GetText();
if (!string.IsNullOrEmpty(sTemp))
{
Clipboard.SetText(ConverTCSC(sTemp));
//MessageBox.Show(Clipboard.GetText());
lablMessage.Visible=true;
}
else
{
lablMessage.Visible = true;
lablMessage.Text="提示:剪貼板中無有效文字信息。";
}
}
void BtnTSClick(object sender, System.EventArgs e)
{
lablMessage.Visible = false;
sTemp = Clipboard.GetText();
if (!string.IsNullOrEmpty(sTemp))
{
Clipboard.SetText(ConverSCTC(sTemp));
//MessageBox.Show(Clipboard.GetText());
lablMessage.Visible = true;
}
else
{
lablMessage.Visible = true;
lablMessage.Text="提示:剪貼板中無有效文字信息。";
}
}
void BtnShowClick(object sender, System.EventArgs e)
{
lablMessage.Visible = false;
ComLabl();
}
//托盤顯示
void Button1Click(object sender, System.EventArgs e)
{
lablMessage.Visible = false;
HideForm();
}
//隱藏窗體
private void HideForm()
{
bShow = false;
ComLabl();
this.MinimizeBox = false;
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
this.notiConvert.Visible = true;
}
//退出程序
void Button2Click(object sender, System.EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
//程序初始化
void MainFormLoad(object sender, System.EventArgs e)
{
lablMessage.Visible = false;
}
//雙擊托盤顯示
void NotiConvertDoubleClick(object sender, System.EventArgs e)
{
this.WindowState = FormWindowState.Normal;
this.MinimizeBox = true;
if (this.Location.X <= 0 || this.Location.Y <= 0)
{
MainForm.ActiveForm.Left = 300;
MainForm.ActiveForm.Top = 500;
}
this.ShowInTaskbar = true;
}
/**//*
*顯示菜單
*/
private void ComLabl()
{
if (bShow)
{
btnShow.Text = "取消前端顯示";
this.TopMost = true;
bShow = !bShow;
}
else
{
btnShow.Text = "前端顯示";
this.TopMost = false;
bShow = !bShow;
}
}
//當鼠標按下時的動作
void MainFormMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = true;
MouseX = e.X;
MouseY = e.Y;
}
}
//當鼠標移動時的動作
void MainFormMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (isMove == true)
{
CurrX = MainForm.ActiveForm.Left - MouseX + e.X;
CurrY = MainForm.ActiveForm.Top - MouseY + e.Y;
MainForm.ActiveForm.Left = CurrX;
MainForm.ActiveForm.Top = CurrY;
}
}
//當鼠標UP的動作
void MainFormMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) { isMove = false; }
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
FormApp.Exit();
}
private void CatchToolStripMenuItem_Click(object sender, EventArgs e)
{
catchToolStripMenuItem.Checked =! catchToolStripMenuItem.Checked;
if (catchToolStripMenuItem.Checked.Equals(true))
{
notiConvert.Icon = new Icon("app.ico");
}
else
{
notiConvert.Icon = new Icon("apq.ico");
}
}
private void btnTest_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show(e.Location.ToString());
}
private void notiConvert_Click(object sender, EventArgs e)
{
notiConvert.ContextMenuStrip = contextMenuConvert;
}
}
}