简体   繁体   English

如何使用 C# 关闭 OneNote 窗口?

[英]How to close OneNote window using C#?

How to close a OneNote window opened when creating a new Page using C#?如何关闭使用 C# 创建新页面时打开的 OneNote 窗口?

I downloaded Matthias' GetOCR C# project (from: link ).我下载了 Matthias 的 GetOCR C# 项目(来自:链接)。 However, when executing the GetOCR code the OneNote windows opens and is left open even once the method finishes executing.但是,在执行 GetOCR 代码时,OneNote 窗口会打开,并且即使方法完成执行也保持打开状态。

I know that probably one way to close this windows is sending a WM_CLOSE using the WindowHandle below:我知道关闭此窗口的一种方法可能是使用下面的 WindowHandle 发送 WM_CLOSE :

app.Windows.CurrentWindow

However, I would like to know if there is a "nicer" way (natively supported by OneNote API) of closing the OneNote window.但是,我想知道是否有一种“更好”的方式(OneNote API 原生支持)关闭 OneNote 窗口。 I can't seem to find any guidance on the OneNote API pages ( link ).我似乎在 OneNote API 页面( 链接)上找不到任何指导。

This should work unless you aren't trying to terminate the process.除非您不尝试终止该过程,否则这应该有效。

Process[] p = Process.GetProcessesByName("onenote");
p[0].Kill();

I'm not sure about the process name though.我不确定进程名称。

I know that this is an old question, but I recently needed the ability to close the OneNote Win32 application as part of an office automation.我知道这是一个老问题,但我最近需要能够关闭 OneNote Win32 应用程序作为办公自动化的一部分。

It is clearly possible to open a single section of a OneNote (one tab = a file with extension .one ) using OneNote interop:显然可以使用 OneNote 互操作打开 OneNote 的单个部分(一个选项卡 = 扩展名为.one的文件):

string sectionID = String.Empty;
var oneNoteApp = new Microsoft.Office.Interop.OneNote.Application();
oneNoteApp.OpenHierarchy(inputFile, null, out sectionID, CreateFileType.cftNone);
oneNoteApp.SyncHierarcy(sectionID);
oneNoteApp.NavigateTo(sectionID);
oneNoteApp.Publish(sectionID, outputFile, PublishFormat.pfPDF);

However, closing the section is not possible, even when obtaining the ID of the notebook.但是,即使获取笔记本的 ID,也无法关闭该部分。

Sending WM_CLOSE to the windows handle of the oneNoteApp (as @pushpraj suggested) will work the first few times, but after the 2nd or 3rd close, OneNote will complain that the app was not close properly.WM_CLOSE发送到oneNoteApp的 windows 句柄(如@pushpraj 建议)将在前几次工作,但在第 2 次或第 3 次关闭后,OneNote 会抱怨应用程序未正确关闭。 The "kill" approach (as @DarkDragon suggested) will also lead to the same issue. “杀死”方法(如@DarkDragon 建议的那样)也将导致相同的问题。

The following close method was the only workaround I was able to find.以下close方法是我能找到的唯一解决方法。 It solved my problem.它解决了我的问题。

var hWnd = oneNoteApp.Windows.CurrentWindows.WindowsHandle;
var phWnd = GetParent((IntPtr) hWnd);
var gphWnd = GetParent((IntPtr) phWnd);
PostMessage((IntPtr) gphWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

(Where GetParent and PostMessage both are available in user32.dll .) (其中GetParentPostMessage都在user32.dll中可用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM