基于word API 创建的可以打开word的自定义控件
代碼基本框架來源: CSDN 博主:zjlovety的專欄?????? 地址:https://blog.csdn.net/zjlovety/article/details/24463117
?public partial class WinWordViewer : UserControl
??? {
??????? public WinWordViewer()
??????? {
??????????? InitializeComponent();
??????????? this.ParentChanged += new EventHandler(WinWordViewer_ParentChanged);
??????????? this.Disposed += new EventHandler(WinWordViewer_Disposed);
??????????? this.Click += new EventHandler(WinWordViewer_Click); ?
??????? }
??????? #region 窗體事件
??????? void WinWordViewer_Click(object sender, EventArgs e)
??????? {
??????????? if (wd != null)
??????????? {
??????????????? wd.ActiveWindow.SetFocus();? //嵌入的窗體焦點是獨立與父窗體,此功能為能生效。
??????????? }
??????? }
??????? void WinWordViewer_Disposed(object sender, EventArgs e)
??????? {
??????????? this.CloseControl();? //父窗體資源釋放時,主動釋放打開的創(chuàng)建的word應(yīng)用、文檔和進程。
??????? }
??????? void WinWordViewer_ParentChanged(object sender, EventArgs e)
??????? {
??????????? if (wd != null)
??????????? {
??????????????? if (this.Parent == null)
??????????????? {
??????????????????? wd.Visible = false; ?
??????????????? }
??????????????? else
??????????????? {
??????????????????? wd.Visible = true; // 當WinWordViewer加入到其他控件中時才展示。
??????????????? }
??????????? }
??????? }
??????? #endregion
??????? public Microsoft.Office.Interop.Word.Document document;? //文檔的引用
??????? public Microsoft.Office.Interop.Word.ApplicationClass wd = null;? //應(yīng)用的引用
??????? public int wordWnd = 0;???? //word引用的句柄
??????? public string filename = null;? //打開的文件名稱
??????? private bool deactivateevents = false; //窗體激活標識
??????? public void PreActivate()
??????? {
??????????? if (wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();
??????? }
??????? /// <summary>
??????? /// 關(guān)閉引用
??????? /// </summary>
??????? public void CloseControl()
??????? {
??????????? try
??????????? {
??????????????? deactivateevents = true;
??????????????? object dummy = null;
??????????????? object dummy2 = (object)false;
??????????????? if (wd != null)
??????????????? {
??????????????????? wd.Visible = false;
??????????????? }
??????????????? if (document != null)
??????????????? {
??????????????????? document.Close(ref dummy, ref dummy, ref dummy);
??????????????? }
??????????????? // Change the line below.
??????????????? if (wd != null)
??????????????? {
??????????????????? wd.Quit(ref dummy2, ref dummy, ref dummy);
??????????????????? wd = null;
??????????????? }
??????????????? deactivateevents = false;
??????????????? foreach (Process pro in Process.GetProcesses()) //這里是找到那些沒有界面的Word進程
??????????????? {
??????????????????? if (pro.ProcessName == "WINWORD")
??????????????????? {
??????????????????????? IntPtr ip = pro.MainWindowHandle;
??????????????????????? string str = pro.MainWindowTitle; //發(fā)現(xiàn)程序中打開跟用戶自己打開的區(qū)別就在這個屬性
??????????????????????? //用戶打開的str 是文件的名稱,程序中打開的就是空字符串
??????????????????????? if (str == "")
??????????????????????? {
??????????????????????????? pro.Kill();
??????????????????????? }
??????????????????? }
??????????????? }
??????????????? //以下2種方法都無法獲取進程。
??????????????? //int id;
??????????????? //int td;
??????????????? //td = User32.GetWindowThreadProcessId(new IntPtr(wordWnd), out id);
??????????????? //if (id == 0)
??????????????? //{
??????????????? //???? Process[] processes = Process.GetProcesses();
??????????????? //??? foreach(Process process in processes)
??????????????? //??? {
??????????????? //??????? if (process.ProcessName == "WINWORD")
??????????????? //??????? {
??????????????? //??????????? if(process.Handle == new IntPtr(wordWnd))
??????????????? //??????????? {
??????????????? //??????????????? MessageBox.Show(process.Id.ToString());
??????????????? //??????????? }
??????????????? //??????? }
??????????????? //??? }
??????????????? //}
??????????????? //if (id != 0)
??????????????? //{
??????????????? //??? var p = Process.GetProcessById(id);
??????????????? //??? p.Kill();
??????????????? //}
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? String strErr = ex.Message;
??????????? }
??????? }
??????? /// <summary>
??????? /// catches Word's close event
??????? /// starts a Thread that send a ESC to the word window ;)
??????? /// </summary>
??????? /// <param name="doc"></param>
??????? /// <param name="test"></param>
??????? private void OnClose(Microsoft.Office.Interop.Word.Document doc, ref bool cancel)
??????? {
??????????? if (!deactivateevents)
??????????? {
??????????????? cancel = true;
??????????? }
??????? }
??????? /// <summary>
??????? /// catches Word's open event
??????? /// just close
??????? /// </summary>
??????? /// <param name="doc"></param>
??????? private void OnOpenDoc(Microsoft.Office.Interop.Word.Document doc)
??????? {
??????????? OnNewDoc(doc);
??????? }
??????? /// <summary>
??????? /// catches Word's newdocument event
??????? /// just close
??????? /// </summary>
??????? /// <param name="doc"></param>
??????? private void OnNewDoc(Microsoft.Office.Interop.Word.Document doc)
??????? {
??????????? if (!deactivateevents)
??????????? {
??????????????? deactivateevents = true;
??????????????? object dummy = null;
??????????????? doc.Close(ref dummy, ref dummy, ref dummy);
??????????????? deactivateevents = false;
??????????? }
??????? }
??????? /// <summary>
??????? /// 加載文件
??????? /// </summary>
??????? /// <param name="t_filename"></param>
??????? public void LoadDocument(string t_filename)
??????? {
??????????? deactivateevents = true;
??????????? filename = t_filename;
??????????? if (wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();
??????????? try
??????????? {
??????????????? wd.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);
??????????????? wd.ApplicationEvents2_Event_NewDocument += new Microsoft.Office.Interop.Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
??????????????? wd.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(OnOpenDoc);
??????????? }
??????????? catch
??????????? {
??????????? }
??????????? if (document != null)
??????????? {
??????????????? try
??????????????? {
??????????????????? object dummy = null;
??????????????????? wd.Documents.Close(ref dummy, ref dummy, ref dummy);
??????????????? }
??????????????? catch
??????????????? {
??????????????? }
??????????? }
??????????? if (wordWnd == 0) wordWnd = User32.FindWindow("Opusapp", null);
??????????? if (wordWnd != 0)
??????????? {
??????????????? User32.SetParent(wordWnd, this.Handle.ToInt32());
??????????????? object fileName = filename;
??????????????? object newTemplate = false;
??????????????? object docType = 0;
??????????????? object readOnly = true;
??????????????? object isVisible = true;
??????????????? object missing = System.Reflection.Missing.Value;
??????????????? try
??????????????? {
??????????????????? if (wd == null)
??????????????????? {
??????????????????????? throw new WordInstanceException();
??????????????????? }
??????????????????? if (wd.Documents == null)
??????????????????? {
??????????????????????? throw new DocumentInstanceException();
??????????????????? }
??????????????????? if (wd != null && wd.Documents != null)
??????????????????? {
??????????????????????? document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
??????????????????? }
??????????????????? if (document == null)
??????????????????? {
??????????????????????? throw new ValidDocumentException();
??????????????????? }
??????????????? }
??????????????? catch
??????????????? {
??????????????? }
??????????????? try
??????????????? {
??????????????????? //int wordWndN = 0;
??????????????????? 取消所有邊框
??????????????????? //User32.SetWindowLong(wordWnd, GWL_STYLE, User32.GetWindowLong(wordWnd, (int)WinWordViewer.GWL_STYLE) & ~(int)Enum.WindowStyles.WS_CAPTION & ~(int)Enum.WindowStyles.WS_THICKFRAME);
??????????????????? //User32.SetWindowPos(wordWnd, wordWndN, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
??????????????????? //long style = User32.GetWindowLong(wordWnd, (int)WinWordViewer.GWL_STYLE);
??????????????????? style &= ~(int)Enum.WindowStyles.WS_SIZEBOX; //去掉可變大小
??????????????????? //style &= ~(int)Enum.WindowStyles.WS_BORDER; //去掉邊框
??????????????????? //style |= (int)Enum.WindowStyles.WS_MAXIMIZE; //最大化
??????????????????? 設(shè)置風格
??????????????????? //User32.SetWindowLong(wordWnd, (int)WinWordViewer.GWL_STYLE, style);
???????????????????? //int counter = wd.ActiveWindow.Application.CommandBars.Count;
???????????????????? //for (int i = 1; i <= counter; i++)
???????????????????? //{
???????????????????? //??? wd.ActiveWindow.Application.CommandBars[i].Visible = false;
???????????????????? //??? wd.ActiveWindow.View.DisplayBackgrounds = true;
???????????????????? //??? wd.ActiveWindow.View.DisplayPageBoundaries = false;
???????????????????? //}
??????????????? }
??????????????? catch
??????????????? {
?
??????????????? }
??????????? }
??????????? deactivateevents = false;
??????? }
??????? //static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
??????? //static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
??????? //static readonly IntPtr HWND_TOP = new IntPtr(0);
??????? //const UInt32 SWP_NOSIZE = 0x0001;
??????? //const UInt32 SWP_NOMOVE = 0x0002;
??????? //const UInt32 SWP_NOZORDER = 0x0004;
??????? //const UInt32 SWP_NOREDRAW = 0x0008;
??????? //const UInt32 SWP_NOACTIVATE = 0x0010;
??????? //const UInt32 SWP_FRAMECHANGED = 0x0020;
??????? //const UInt32 SWP_SHOWWINDOW = 0x0040;
??????? //const UInt32 SWP_HIDEWINDOW = 0x0080;
??????? //const UInt32 SWP_NOCOPYBITS = 0x0100;
??????? //const UInt32 SWP_NOOWNERZORDER = 0x0200;
??????? //const UInt32 SWP_NOSENDCHANGING = 0x0400;
??????? //const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
??? }
?
public class User32
??? {
??????? //獲取句柄
??????? [DllImport("User32.dll", EntryPoint = "FindWindow")]
??????? internal static extern int FindWindow(
??????? string lpClassName,
??????? string lpWindowName);
??????? //設(shè)置父控件(將句柄轉(zhuǎn)至父控件)
??????? [DllImport("User32.dll", EntryPoint = "SetParent")]
??????? internal static extern int SetParent(
??????? int hWndChild,
??????? int hWndNewParent);
??????? //[DllImport("User32.dll", EntryPoint = "SetWindowLong")]
??????? //internal static extern int SetWindowLong(
??????? //int hWnd,
??????? //int nIndex,
??????? //long dwNewLong);
??????? [DllImport("User32.dll", EntryPoint = "SetWindowLong")]
??????? internal static extern long SetWindowLong(
??????? int hWnd,
??????? int nIndex,
??????? long dwNewLong);
??????? [DllImport("User32.dll", EntryPoint = "GetWindowLong")]
??????? internal static extern long GetWindowLong(
??????? int hWnd,
??????? int nIndex);
??????? [DllImport("user32.dll", EntryPoint = "MoveWindow")]
??????? internal static extern int MoveWindow(
??????? int hWnd,
??????? int x,
??????? int y,
??????? int cx,
??????? int cy,
??????? bool repaint);
??????? [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
??????? internal static extern bool SetWindowPos(
??????????? int hWnd,?????????????? // handle to window
?????????? int hWndInsertAfter,??? // placement-order handle
??????????? int X,????????????????? // horizontal position
??????????? int Y,????????????????? // vertical position
??????????? int cx,???????????????? // width
??????????? int cy,???????????????? // height
??????????? uint uFlags???????????? // window-positioning options
??????????? );
??????? [DllImport("user32.dll", EntryPoint = "DrawMenuBar")]
??????? internal static extern Int32 DrawMenuBar(
??????????? Int32 hWnd
??????????? );
??????? [DllImport("user32.dll", EntryPoint = "GetMenuItemCount")]
??????? internal static extern Int32 GetMenuItemCount(
??????????? Int32 hMenu
??????????? );
??????? [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
??????? internal static extern Int32 GetSystemMenu(
??????????? Int32 hWnd,
??????????? bool Revert
??????????? );
??????? [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
??????? internal static extern Int32 RemoveMenu(
??????????? Int32 hMenu,
??????????? Int32 nPosition,
??????????? Int32 wFlags
??????????? );
??????? [DllImport("User32.dll", CharSet = CharSet.Auto)]
??????? public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int ID);
??? }
--------------------------------------------------------------------------------------------
由于個人水平不高,對原代碼的修改主要還是基于對自定義控件本身邏輯的調(diào)整。
對 word API 的使用還是比較簡單。
無法成功的對調(diào)用的word引用的焦點和樣式進行調(diào)整(找了其他的資料,但是無法成功運用),故很多代碼選擇刪除或者。
轉(zhuǎn)載于:https://www.cnblogs.com/DarkWill-BlackGod/p/9230291.html
總結(jié)
以上是生活随笔為你收集整理的基于word API 创建的可以打开word的自定义控件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 唐宇迪机器学习课程笔记:随机森林
- 下一篇: mysql 常用命令与备份恢复 整理