
右键单击WpfApplication1解决方案。然后单击“Add”菜单并选择“User Control…”。

wpf项目中将会增加一个新的窗体。
选择“User Control”项。不是“User Control(WPF)”项。
双击解决方案面板中的UserControl1.CS。
打开“工具箱”面板,然后单击菜单中的“ Choose Items…”。



输入打开word文档的C#代码,并保护word文档的修改如下所示:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace WpfApplication1 { public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public void Open() { axEDOffice1.OpenFileDialog(); } public void Protect() { if (axEDOffice1.GetCurrentProgID() == “Word.Application”) { axEDOffice1.ProtectDoc(2); } } public void Print() { axEDOffice1.PrintPreview(); } public void Close() { axEDOffice1.ExitOfficeApp(); } } } |
最后,你需要为UserControl编写主机窗口。切换到Windows1.xaml文件,然后添加打开、保护、打印和关闭的按钮,如图所示。

添加以下c#代码来关联office component。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Open_Click(object sender, RoutedEventArgs e) { _host.Open(); } private void Protect_Click(object sender, RoutedEventArgs e) { _host.Protect(); } private void Print_Click(object sender, RoutedEventArgs e) { _host.Print(); } private void Close_Click(object sender, RoutedEventArgs e) { _host.Close(); } } } |
打开配置管理器。将Active Solution平台更改为x86选项。然后建立并运行。

public void Open() { //axEDOffice1.OpenFileDialog(); axEDOffice1.Open(sPath, “Word.Application”); axEDOffice1.Open(sPath, “Excel.Application”); axEDOffice1.Open(sPath, “PowerPoint.Application”); axEDOffice1.Open(sPath, “Visio.Application”); axEDOffice1.Open(sPath, “MSProject.Application”); } |
以上就是本次教程的全部内容,接下来会有更多相关教程,敬请关注!您也可以在评论者留下你的经验和建议。
试用、下载、了解更多产品信息请点击“咨询在线客服”
标签:WPFOfficeword
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!