Solved

Closing Window Instances

Posted on 1998-08-30
4
178 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
Comment Utility
Edited text of question
0
 
LVL 11

Accepted Solution

by:
alexo earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
And the autograder hits again!
Thank you so much for your consideration.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now