ARTICLE

Find and Close the window using WIN API

Posted by Mohan Kumar Articles | C# Language February 01, 2008
This article explains how to find and close the window using Win API.
Reader Level:

Find the Window

The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.

FindWindow(string lpClassName,string lpWindowName)

Finding ClassName and WindowName using Spy++

Spy++ (SPYXX.EXE) is a Win32-based utility that gives you a graphical view of the system's processes, threads, windows, and window messages.With the Window Finder Tool you can find the properties of a selected window.

Step 1 : Arrange your windows so that Spy++ and the subject window are visible.

Step 2: From the Spy menu, choose Find Window to open the Find Window dialog box.

Step 3:  Drag the Finder Tool to the desired window. As you drag the tool, window details display in the dialog box. (Handle,Caption(Window Name),Class Name)

Example for closing Notepad

//Declartion

[DllImport("user32.dll")]

public static extern int FindWindow(string lpClassName,string lpWindowName);

DllImport("user32.dll")]

Public static extern int SendMessage(int hWnd,uint Msg,int wParam,int lParam);

public const int WM_SYSCOMMAND = 0x0112;

public const int SC_CLOSE = 0xF060;

private void button1_Click(object sender, EventArgs e)

{

    // retrieve the handler of the window

    int iHandle = FindWindow("Notepad", "Untitled - Notepad");

    if (iHandle > 0)

    {

        // close the window using API

        SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);

    }
}

Closing google window

private void button2_Click(object sender, EventArgs e)

{

    //closing google window

    int googleHandle = FindWindow("IEFrame", "Google - Microsoft Internet Explorer provided by Cognizant");

    if (googleHandle > 0)

    {

        // close the window using API

        SendMessage(googleHandle, WM_SYSCOMMAND, SC_CLOSE, 0);

    }
}

Login to add your contents and source code to this article
post comment
     

Hello, I'm currently trying to figure out a way, to cancel the Windows Print Dialog Box after its spawned. My tool set at the moment is selenium and vs2010 (c#). In this test case my automation navigates to a page, finds an article and sends and clicks on a "Print Me" button that spawns the windows "Print" dialog box. However selenium cannot handle this event. All I'd like to do is click cancel on the Dialog box after its spawned. Any idea's would be very welcome. Thanks in advance, Jon

Posted by jon m Mar 07, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.