Click here to Skip to main content
Click here to Skip to main content

Tagged as

Find and Close the Window using Win API

, 19 Dec 2007
Rate this:
Please Sign up or sign in to vote.
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.

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)

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)

Share

About the Author

Mohan Kumar
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! Pinmembercchangkhongayngo9-Jul-12 21:07 
GeneralFinding a window PinmemberRyan Stivenson23-Apr-11 22:49 
GeneralAny perspectives? ;) PinmemberGeni20-Dec-07 4:12 
GeneralПрикольная статья PinmemberFerlySky200819-Dec-07 1:21 
GeneralRe: Прикольная статья PinmemberJoergen Sigvardsson19-Dec-07 2:02 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 19 Dec 2007
Article Copyright 2007 by Mohan Kumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid