Solved

Closing Window Instances

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

Suggested Solutions

Title # Comments Views Activity
FMX StringGrid1->Canvas->FillRect Problem 3 221
Header of docx file 17 145
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 96
Precision Problem in C++ 7 67
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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