Solved

Closing Window Instances

Posted on 1998-08-30
4
198 Views
Last Modified: 2010-04-02
Does any one know how to close all instances of a window, like Internet Exlporer or Netscape Navigator for example. I would prefer an example of C++ code. I know it has something to do with findWindow() or findWindowEx().
Can any one help?

Thanks
0
Comment
Question by:bsugden
  • 3
4 Comments
 

Author Comment

by:bsugden
ID: 1171578
Edited text of question
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
ID: 1171579
You have two options.

You can use FindWindow() in a loop.  FindWindow() returns a top-level window that matches the requested title and/or class.  The problem is, the title of NS or IE usually changes to reflect the page it currently views and the class can depend on the version.  The loop will look like this:
    HWND hwnd;
    while ((hwnd = FindWindow(title, class) != NULL)
        DestroyWindow(hwnd);

A more flexible approach is to use EnumWindows().  EnumWindows() loops over all top-level windows and passes their handles to a user-specified function.  That function can get the title of the window, the class, the position or any other property.  It can compare substrings and do whatever it likes.

    EnumWindows(EnumWindowsProc, NULL);


    BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
        /* Do whatever you want with the handle */
        return TRUE;
    }

Much more flexible but a bit less efficient.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1171580
An example:

        #define SUBSTRING "Notepad"

        #define STRICT
        #include <windows.h>

        BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

        int main()
        {
            EnumWindows(EnumWindowsProc, NULL);
            return 0;
        }

        BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
        {
            char buffer[512];
            GetWindowText(hwnd, buffer, sizeof buffer);
            if (strstr(buffer, SUBSTRING))
                SendMessage(hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0);
                // or: ShowWindow(hwnd, SW_MINIMIZE);
            return TRUE;
        }

Modify as needed.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1171581
And the autograder hits again!
Thank you so much for your consideration.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question