Hiding Background with OSD - A C# Sample to Hide Window

In this post the basics for finding and hiding a window will be shown in Visual Studio 2008 (C#). As previously mentioned in Snazzy OSD status with BGInfo, hiding the Windows 7 Setup window will be demonstrated.

Creating and Compiling

First, create a simple Console Application under the C# tree.

image

Next, copy and paste the code sample below.

 using System;
 using System.Collections.Generic;
 using System.Text;
 using System.Runtime.InteropServices;   // Namespace for Import DLL
  
  
 namespace HideSetupWindow
 {
  
     class Program
     {
  
         static void Main(string[] args)
         {
             // ####################
             // | Find Window
             // ####################
             int hwnd = WinAPI.FindWindow(null, "FirstUXWnd");
             Console.WriteLine("FindWindow API Result: " + hwnd.ToString());
  
             // ####################
             // | Hide Window : 0 = Hide, 1 = Show
             // ####################
             if (hwnd != 0) WinAPI.ShowWindow(hwnd, 0);
  
         }
  
         internal static class WinAPI
         {
             // ----------------------------------------------------------
             // FindWindow : https://msdn.microsoft.com/en-us/library/ms633499.aspx
             // ----------------------------------------------------------
             [DllImport("user32.dll", SetLastError = true)]
             internal static extern int FindWindow(string lpClassName, string lpWindowName);
  
             // ----------------------------------------------------------
             // Show Window : https://msdn.microsoft.com/en-us/library/ms633548.aspx
             // ----------------------------------------------------------
             [DllImport("user32.dll", SetLastError = true)]
             internal static extern int ShowWindow(int hwnd, int nCmdShow);
         }
  
         // ####################
         // NOTE: Any of the following will work...
         // ####################
         // ----------------------------------------------------------------
         //int hwnd = WinAPI.FindWindow("FirstUXWndClass", null);
         //int hwnd = WinAPI.FindWindow("FirstUXWndClass", "FirstUXWnd");
         //int hwnd = WinAPI.FindWindow(null, "FirstUXWnd");
         // ----------------------------------------------------------------
  
     }
 }

Finally, compile the application.

 

Tips & Tricks

Use the code sample below to hide an instance of Notepad. Two points worth mentioning:

  1. The Class Name for Notepad windows is “Notepad”; all Notepad windows will share this common class name.
  2. The Text or Window Name for Notepad windows may vary for each window; for example, a new instance of Notepad will usually contain the window text “Untitled – Notepad”

Parameter 1 is the Class Name. Parameter 2 is the Window Name. Searching on either (or both) parameter is possible:

 /* Sample 1 */ int hwnd = WinAPI.FindWindow("Notepad", null);
 /* Sample 2 */ int hwnd = WinAPI.FindWindow(null, "Untitled - Notepad");
 /* Sample 3 */ int hwnd = WinAPI.FindWindow("Notepad", "Untitled - Notepad

In parting, a very accommodating utility for learning and working with this type of window manipulation is Winspector.

This blog post was originally created by Michael Schmidt.