如何在C#中检查MS字是否关闭 [英] How to check if an MS word is closed in C#

查看:89
本文介绍了如何在C#中检查MS字是否关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中做了一个应用程序,它打开像记事本,计算器这样的exe文件。

所以,我的目标是当我打开一个像Word这样的exe文件时,会自动创建一个按钮

然后当我关闭Word应用程序时,该按钮将被移除。

每个应用程序在启动时都会有自己的按钮。

我试过这样做但是我关闭应用程序时遇到了一些问题。

这就是我所做的:

I am doing an application in C# which open exe files like notepad, calculator.
So, my aim is when I open an exe file like Word, a button is created automatically
and then when I close the Word app, the button will be removed.
Each app will have its own button when it is launched.
I tried to do it but I have some issues with the closing of the app.
Here is what I did:

var applicationWord = new Microsoft.Office.Interop.Word.Application();


            applicationWord.Visible = true;
               applicationWord.DocumentBeforeClose += DocumentBeforeClose;



               if (applicationWord.Visible == true)
               {


                       button = new Button();
                       button.Image = Properties.Resources.word_80;
                       PIC_Barre.Controls.Add(button);
                       button.AutoSize = true;
                       //     button.Tag = proc.Id;
                       PIC_Barre.Controls.Add(button);


                   foreach (Process proc in Process.GetProcessesByName("WINWORD"))
                   {
                       if (proc.ProcessName.Contains("WINWORD"))
                       {
                           proc.WaitForInputIdle();
                           {
                               Thread.Sleep(500);
                               SetWindowPos(proc.MainWindowHandle.ToInt32(),
                                   (int)SetWinPos_ZOrderOpt.HWND_TOPMOST,
                                   0, 0, 0, 0,
                                   (int)(SetWinPosFlags.SWP_NOSIZE |
                                         SetWinPosFlags.SWP_NOMOVE));
                           }
                           button.Click += (s, e) => { ShowWindowAsync(proc.MainWindowHandle, (int)ShowWindowCommands.Normal); };
                           proc.Exited += (s, e) =>
                           {
                               var method = (Action)(() => PIC_Barre.Controls.Remove(button));
                               // button.Visible = false;
                               if (button.InvokeRequired)
                               {
                                   button.Invoke(method);
                               }


                           };

                       }

                   }
               }





什么我试过了:



我的问题是如何检查MS Word的窗口何时关闭,以便删除该按钮。



What I have tried:

My problem is how to check when a window of a MS Word is closed so that the button will be removed.

推荐答案

VisualBasic知道 GetObject [ ^ ]方法,返回 COM对象 null )。





没有C#等效方法,除非你添加对<的引用code> Microsoft.VisualBasic (不推荐)。



VisualBasic knows GetObject[^] method, which returns COM object or nothing (null).


There's no C# equivalent method, unless you add a reference to Microsoft.VisualBasic (not recommended).



有一个C#相当于 GetObject - Marshal.GetActiveObject [ ^ ]。



感谢Richard,感谢您的宝贵意见。



[/编辑]



请关注以下文章中的说明解决您的问题:

Visual Basic GetObject函数的AC#替代方法 [ ^ ]



注意:我自己没有测试过!


Thank you, Richard for that valuable comment.

[/EDIT]

Please, follow the instruction in below article to resolve your issue:
A C# alternative for the Visual Basic GetObject function[^]

Note: didn't tested by myself!


这将结束任何MS Word,也许这是一个开始:



this will end any MS Word, maybe this is a start :

using System.Diagnostics;



...


...

Process[] word_apps= Process.GetProcessesByName("winword");
foreach(var word_app in word_apps){
 word_app.Kill();
}


这篇关于如何在C#中检查MS字是否关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆