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;
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));
if (button.InvokeRequired)
{
button.Invoke(method);
}
};
}
}
}
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.