Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Find and Close the Window using Win API

Rate me:
Please Sign up or sign in to vote.
3.19/5 (14 votes)
19 Dec 2007CPOL 135.1K   20   5
Find and Close the window using Win API

Introduction

This article explains how to find and close the window using Win API .

Find and Close the Window

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.

C#
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)

C#
using Microsoft.Win32;

[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 closeWindow()
        {
            // 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);
            }  
        }	

History

  • 19th December, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Cognizant Technology Solution
India India
I’m Mohan Kumar from Chennai and hold a Masters Degree in Information Technology. I have worked with Opcion Technologies and Atlas systems in Various Technologies.Currently i'm associated with congnizant Technology Solutions.I am a very good fan of Microsoft Technologies. I love Programming, and I am eager to learn New things Curious about Microsoft Products, Interested in Knowledge sharing.I am glad that I have received couple of awards from community Credit and Dotnetspider for submitting .Net related articles and answers. My famous slogan "Awake! Arise! Stop not till the goal is reached.

Comments and Discussions

 
Generalplease help me! Pin
cchangkhongayngo9-Jul-12 21:07
cchangkhongayngo9-Jul-12 21:07 
GeneralFinding a window Pin
Ryan Stivenson23-Apr-11 22:49
Ryan Stivenson23-Apr-11 22:49 
Hello,

I'm trying to find a window by using FindWindow() Win Api.
I'm using c# V.S 10 .net 4.0. on Windows 7.
When I type:
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName,string lpWindowName);

I'm getting this error:
Error 1 Expected class, delegate, enum, interface, or struct
Error 2 The modifier 'extern' is not valid for this item

Any Idea ?

Thanks
Ryan
GeneralAny perspectives? ;) Pin
Geni20-Dec-07 4:12
Geni20-Dec-07 4:12 
GeneralПрикольная статья Pin
FerlySky200819-Dec-07 1:21
FerlySky200819-Dec-07 1:21 
GeneralRe: Прикольная статья Pin
Jörgen Sigvardsson19-Dec-07 2:02
Jörgen Sigvardsson19-Dec-07 2:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.