Solved

Closing Window Instances

Posted on 1998-08-30
4
205 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

860 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