Hiding other windows in C#

Discussion in 'C#' started by shabbir, Jul 5, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    To start with include namespaces

    Code:
    using System.Diagnostics;               // For prcesss related information
    using System.Runtime.InteropServices;   // For DLL importing
    
    Now declare these variables

    Code:
    private const int SW_HIDE = 0; 
    Now declare win32 function ShowWindow

    Code:
    [DllImport("User32")] 
    private static extern int ShowWindow(int hwnd, int nCmdShow);
    The above function accepts 2 parameters hWnd is handle of a window whose state we needs to be modified and nCmdShow contains integer value which denotes state. Here are the list of available states
    Code:
    SW_HIDE             0
    SW_SHOWNORMAL       1
    SW_NORMAL           1
    SW_SHOWMINIMIZED    2
    SW_SHOWMAXIMIZED    3
    SW_MAXIMIZE         3
    SW_SHOWNOACTIVATE   4
    SW_SHOW             5
    SW_MINIMIZE         6
    SW_SHOWMINNOACTIVE  7
    SW_SHOWNA           8
    SW_RESTORE          9
    SW_SHOWDEFAULT      10
    SW_FORCEMINIMIZE    11
    SW_MAX              11
    
    Now the main problem is how to get handle of a particular window. Its simple

    Process[] processRunning = Process.GetProcesses();

    This will return array of all the processes. After this you can use foreach loop to iterate through each process in the array.

    Code:
    int hWnd;
    Process[] processRunning = Process.GetProcesses();
    foreach (Process pr in processRunning)
    {
        if (pr.ProcessName == "notepad")
        {
            hWnd = pr.MainWindowHandle.ToInt32();
            ShowWindow(hWnd, SW_HIDE);
        }
    }
    Note Remember that it will just Hide the notepad process and not kill it. You need to be killing all the notepad.exe's running in the background through task manager. If you even wish to kill the process use
    Code:
    pr.Kill();
    inside the if block.
     
  2. cselvaraju

    cselvaraju New Member

    Joined:
    Nov 22, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Works just great
     
  3. eye_v_eye

    eye_v_eye New Member

    Joined:
    Apr 4, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Okie, I have success hide the application into the process list. Any idea how to bring it back from the process list. In another word restore/show back the application.

    I have used the SW_RESTORE, it doesn't work.
     
  4. booyeeka

    booyeeka New Member

    Joined:
    Jun 3, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have two errors related to this:
    1. The name 'ShowWindow' does not exist in the current context
    2. The name 'SW_HIDE' does not exist in the current context

    Can you help me how to solve this, and what I did wrong?

    Thank you in advance!
     
  5. Imrul Kayser

    Imrul Kayser New Member

    Joined:
    Nov 13, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I want able/ Disable local area connections (may be dial up or broadband) using C#
    How can I do that?

    I will be grateful if anyone help me
     
  6. oasisfleeting

    oasisfleeting New Member

    Joined:
    Nov 27, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    What about never letting another applications window pop up?
    I have a magic jack and when a call is being made, (outgoing or incoming) the interface window pops up and interrupts my video games or movies. I want to permanently force this window to be hidden.
     
  7. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    can we have a downloadable source code plz, if possible
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Its just one API Call and so I did not had it.
     
  9. sameer_havakajoka

    sameer_havakajoka New Member

    Joined:
    Sep 14, 2009
    Messages:
    271
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Sleeping
    Location:
    Hava Ke Paro Me
    k f9, bt it ll be much useful for extreme beginners
     
  10. santumk

    santumk New Member

    Joined:
    May 7, 2016
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Iam controlling UNITY and UBI soft games like minimizing and maximizing but its not its not working properly .. Ill import your code also but it is also not working .. plz can u help me
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice