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);
- }
- };
- }
- }
- }

Esakki MuthuRank #5,293 · 304 reputationPosted Sep 14, 2016, 2:40 AM EDT
Jim Tat52 reputationPosted Sep 14, 2016, 2:44 AM EDT
Esakki MuthuRank #5,293 · 304 reputationPosted Sep 14, 2016, 2:56 AM EDT
Jim Tat52 reputationPosted Sep 14, 2016, 5:00 AM EDT
Esakki MuthuRank #5,293 · 304 reputationPosted Sep 19, 2016, 10:39 PM EDT