How to use the ShowWindow API to hide and show a form ?

Good night gentlemen,

I would like to see a code using the ShowWindow API with C# and .NET 2003.

I have an example here but it's not working. I think it's simple, but I'm facing troubles.

My code goes like this:

#region Constants

private const int SW_HIDE = 0;

private const int SW_SHOWNORMAL = 1;

private const int SW_SHOW = 5;

#endregion Constants

#region APIs

[System.Runtime.InteropServices.DllImport("coredll.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[System.Runtime.InteropServices.DllImport("coredll.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);

[System.Runtime.InteropServices.DllImport("coredll.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]

private static extern bool EnableWindow(IntPtr hwnd, bool enabled);

#endregion APIs

public static void ShowApp()

{

IntPtr h = FindWindow(null, "Form1");

ShowWindow(h, SW_SHOW);

EnableWindow(h, true);

}

public static void HideApp()

{

IntPtr h = FindWindow(null, "Form1");

ShowWindow(h, SW_HIDE);

EnableWindow(h, false);

}

The code works fine when I use the static method HideApp(), it definitly hides my "Form1" form. But when I use the ShowApp it didn't show my form. The FindWindow API returns a pointer to my form, it goes true the ShowWindows and EnableWindow lines without error, but my form is not shown.

What I'm doing wrong

Thanks in advance.




Answer this question

How to use the ShowWindow API to hide and show a form ?

  • olgaF

    Hi Alex, yes, the reason is because the form I'm trying to hide isn't inside my app, it's another app running on the device. I'm trying to do it because I'm building a kiosk app.

    Well I think I realize my error. There's nothing wrong with the code I posted.

    I did another test and it works.

    I think I misunderstood my tests.

    Thanks anyway.



  • slein

    Have you tried doing a form.update (or is it form.refresh ) after you call the ShowWindow

    I have a module of windows calls, it's in vb.net tho, which I know you c# fellows loath, but I'll post it if you want it


  • William Bartholomew

    Is there any reason why you don't want to use Form.Show and Form.Hide

    Also - to bring the application to foreground the most effective way it to launch it again (programmatically). The runtime will take care of it. Something like:
    Process.Start(typeof(Program).Assembly.GetModules()[0].FullyQualifiedName);



  • How to use the ShowWindow API to hide and show a form ?