none
Program is not minimized when brought front by other program RRS feed

  • Question

  • I made a sample program which is minimized to the System Tray.(MyProgB)

    Form1 has one button(bttnMinimize) which minimize program to the System Tray.

    Here is the sample code.

    //MyProgB

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Resize(object sender, EventArgs e) { notifyIcon1.BalloonTipTitle = "Minimize to Tray App"; notifyIcon1.BalloonTipText = "You have successfully minimized your form."; if (FormWindowState.Minimized == this.WindowState) { notifyIcon1.Visible = true; notifyIcon1.ShowBalloonTip(500); this.Hide(); } else if (FormWindowState.Normal == this.WindowState) { notifyIcon1.Visible = false; } } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; } private void bttnMinimize_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } }

    Problem occurs when MyProgB is activated from another program(MyProgA).

    If bring front MyProgB from other program(MyProgA), then, MyProgB is not minimized.

    If click bttnMyProgB in the MyProgA, then bring myPtogB to the front.

    Here is MyProgB.

    //MyProgA

    public partial class Form1 : Form { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr handle); [DllImport("User32.dll")] private static extern bool ShowWindow(IntPtr handle, int nCmdShow); [DllImport("User32.dll")] private static extern bool IsIconic(IntPtr handle); const int SW_RESTORE = 9; public Form1() { InitializeComponent(); } private void bttnMyProgB_Click(object sender, EventArgs e) { IntPtr handle = (IntPtr)FindWindow(null, "MyProgB"); if (IsIconic(handle)) { ShowWindow(handle, SW_RESTORE); } SetForegroundWindow(handle); } }

    MyProgB works well when run alone.

    1. Start MyProgB

    2. Press [Minimize] button and then it is minimized to the System Tray

    3. Doubleclick icon from the System Tray and window appear.

    4. Press [Minimize] button

    Works fine!

    However, it doesn't work correctly when run with MyProgA like following.

    1. Start MyProgB

    2. Start MyProgA

    3. Press [Minimize] button from MyProgB and then it is minimized to the System Tray

    4. Doubleclick icon from the System Tray and window appear.    

    5. Press [Minimize] button from MyProgB and then it is minimized to the System Tray => so far works well!

    6. Press [bttnMyProgB] button and then MyProgB sppear.

    7. Press [Minimize] button from MyProgB and then it is not minimized to the System Tray => problem occur here!!!

    Can anybody resolve this problem?

    Wednesday, July 3, 2019 2:12 AM

Answers

  • Try an alternative in MyProgB:

    [DllImport( "user32" )]
    public static extern IntPtr PostMessage( IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam );
    
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MINIMIZE = 0xF020;
    
    private void btnMinimize_Click( object sender, EventArgs e )
    {
        PostMessage( Handle, WM_SYSCOMMAND, new IntPtr( SC_MINIMIZE ), IntPtr.Zero );
    }
    

    • Marked as answer by Jeff0803 Friday, July 5, 2019 5:50 PM
    Wednesday, July 3, 2019 5:48 AM
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to solve the problem that it is not minimized to the System Tray when you press [Minimize] button from MyProgB.

    I find the solution, you could try the following code in bttnMinimize_Click event.

     private void bttnMinimize_Click(object sender, EventArgs e)
            {
                this.Show();
                this.WindowState = FormWindowState.Minimized;
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, July 3, 2019 6:39 AM
    Moderator

All replies

  • I made a sample program which is minimized to the System Tray.(MyProgB)

    Form1 has one button(bttnMinimize) which minimize program to the System Tray.

    Here is the sample code.

    //MyProgB

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Resize(object sender, EventArgs e) { notifyIcon1.BalloonTipTitle = "Minimize to Tray App"; notifyIcon1.BalloonTipText = "You have successfully minimized your form."; if (FormWindowState.Minimized == this.WindowState) { notifyIcon1.Visible = true; notifyIcon1.ShowBalloonTip(500); this.Hide(); } else if (FormWindowState.Normal == this.WindowState) { notifyIcon1.Visible = false; } } private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { this.Show(); this.WindowState = FormWindowState.Normal; } private void bttnMinimize_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Minimized; } }

    Problem occurs when MyProgB is activated from another program(MyProgA).

    If bring front MyProgB from other program(MyProgA), then, MyProgB is not minimized.

    If click bttnMyProgB in the MyProgA, then bring myPtogB to the front.

    Here is MyProgA.

    //MyProgA

    public partial class Form1 : Form { [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr handle); [DllImport("User32.dll")] private static extern bool ShowWindow(IntPtr handle, int nCmdShow); [DllImport("User32.dll")] private static extern bool IsIconic(IntPtr handle); const int SW_RESTORE = 9; public Form1() { InitializeComponent(); } private void bttnMyProgB_Click(object sender, EventArgs e) { IntPtr handle = (IntPtr)FindWindow(null, "MyProgB"); if (IsIconic(handle)) { ShowWindow(handle, SW_RESTORE); } SetForegroundWindow(handle); } }

    MyProgB works well when run alone.

    1. Start MyProgB

    2. Press [Minimize] button and then it is minimized to the System Tray

    3. Doubleclick icon from the System Tray and window appear.

    4. Press [Minimize] button

    Works fine!

    However, it doesn't work correctly when run with MyProgA like following.

    1. Start MyProgB

    2. Start MyProgA

    3. Press [Minimize] button from MyProgB and then it is minimized to the System Tray

    4. Doubleclick icon from the System Tray and window appear.    

    5. Press [Minimize] button from MyProgB and then it is minimized to the System Tray => so far works well!

    6. Press [bttnMyProgB] button and then MyProgB appear.

    7. Press [Minimize] button from MyProgB and then it is not minimized to the System Tray => problem occur here!!!

    Can anybody resolve this problem?



    Wednesday, July 3, 2019 3:38 AM
  • Try an alternative in MyProgB:

    [DllImport( "user32" )]
    public static extern IntPtr PostMessage( IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam );
    
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MINIMIZE = 0xF020;
    
    private void btnMinimize_Click( object sender, EventArgs e )
    {
        PostMessage( Handle, WM_SYSCOMMAND, new IntPtr( SC_MINIMIZE ), IntPtr.Zero );
    }
    

    • Marked as answer by Jeff0803 Friday, July 5, 2019 5:50 PM
    Wednesday, July 3, 2019 5:48 AM
  • Hi Jeff0803,

    Thank you for posting here.

    Based on your description, you want to solve the problem that it is not minimized to the System Tray when you press [Minimize] button from MyProgB.

    I find the solution, you could try the following code in bttnMinimize_Click event.

     private void bttnMinimize_Click(object sender, EventArgs e)
            {
                this.Show();
                this.WindowState = FormWindowState.Minimized;
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, July 3, 2019 6:39 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 5, 2019 8:08 AM
    Moderator