Book a Demo

Author Topic: C# switch between forms hides EA main window  (Read 5145 times)

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1405
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
C# switch between forms hides EA main window
« on: April 27, 2018, 05:33:07 pm »
Hi,

To improve usability in a future release of my add-in, I sometimes close an add-in popup window (C# form) to open another one.
So I have a class that runs Close() and Dispose() on the initial form object before creating another form (typically I open form1 from EA, form1 opens settingsform, and when settingsform is closed, the add-in determines that form1 needs to be closed and replaced with form2).
It works well, except that EA main screen/window sometimes gets hidden behind another application e.g. my web browser.

I tried to run repository.ShowWindow(1); but it doesn't do anything (BTW EA help doesn't say what value needs to be provided in ShowWindow to display or hide... even though I assume it's 1 & 0).

 
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


EXploringEA

  • EA User
  • **
  • Posts: 172
  • Karma: +8/-0
    • View Profile
Re: C# switch between forms hides EA main window
« Reply #1 on: May 02, 2018, 12:36:36 am »
Hi Guillaume,

My guess is that EA is losing focus for some reason, but not entirely clear from your description. So a brute force way to try and bring EA to the the front after closing your forms is to call windows API to bring EA to foreground.

Something along the lines, in VB.Net but easy to convert to C# I'm sure:
1. declare functions and a constant
Code: [Select]
Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
Declare Function ShowWindow Lib "user32.dll" (ByRef hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
Private Const SW_MAXIMIZE As Integer = 3

2. Get the process handle (of EA) and then set as ForeGround
Code: [Select]
Dim proc As Process = Process.GetCurrentProcess()
Dim hWnd As IntPtr = proc.MainWindowHandle
If (hWnd <> IntPtr.Zero) Then
    SetForegroundWindow(hWnd)
    ShowWindow(hWnd, SW_MAXIMIZE)
End If

A quick test with this will at least identify/eliminate loss of focus as the issue.

BR
Adrian
EXploringEA - information, utilities and addins

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: C# switch between forms hides EA main window
« Reply #2 on: May 02, 2018, 01:18:44 am »
I tried to run repository.ShowWindow(1); but it doesn't do anything (BTW EA help doesn't say what value needs to be provided in ShowWindow to display or hide... even though I assume it's 1 & 0).
EAUI as usual. They don't have a hand to name things. Like in "Add Diagram" to also remove the composite or "Enable Security" to disable it. The same here. 0 will hide and 1 will show.  But basically this is a Windoze issue here and Adrians answer is probably the solution...

q.

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1405
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: C# switch between forms hides EA main window
« Reply #3 on: May 02, 2018, 06:54:08 pm »
Hi Adrian,

Thanks for your reply. I tried what you suggested in C# (using some infos from stackoverflow.com) and it's working :)
I need to consolidate it with all my forms, and should be able to provide a code sample if anyone comes across the same issue with C#.

cheers
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


Guillaume

  • EA Practitioner
  • ***
  • Posts: 1405
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: C# switch between forms hides EA main window
« Reply #4 on: May 02, 2018, 08:50:44 pm »
Here is a working solution in C#

1. create a Helper class:

Code: [Select]
using System.Runtime.InteropServices; //required for APIs
namespace myaddin....
{
    public static class WindowHelper
    {
        public static void BringProcessToFront()
        {
            Process proc = Process.GetCurrentProcess();
            IntPtr procHandle = proc.MainWindowHandle;
            if (IsIconic(procHandle))
            {
                ShowWindow(procHandle, SW_RESTORE);
            }
            SetForegroundWindow(procHandle);
        }

        const int SW_RESTORE = 9;

        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr handle);
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        private static extern bool IsIconic(IntPtr handle);
    }
}

2. when EA screen needs to be restored, call the following:

Code: [Select]
        WindowHelper.BringProcessToFront();
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com