Solved

Closing Window Instances

Posted on 1998-08-30
4
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

761 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