Solved

FindWindow(NULL,"notepad");

Posted on 2007-11-27
7
873 Views
Last Modified: 2008-02-01
i know i said that i solved the problem, because I used a 3rd party software to look them up,


however

how do I find the handles of windows with the same title?


i other words, i have multiple notepads open
0
Comment
Question by:Troudeloup
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 20359393
You could use 'EnumWindows()', e.g.

#include <windows.h>
#include <iostream>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      cout << buf << endl;

      return true;
}

int main()
{
   EnumWindows(EnumWindowsProc,NULL);

   return 0;
}
0
 

Author Comment

by:Troudeloup
ID: 20359407
what does it do?


I need n handles returned for n number of windows of that name.
0
 
LVL 86

Expert Comment

by:jkr
ID: 20359414
Or

#include <windows.h>
#include <iostream>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      cout << buf << " HWND: " << hwnd << endl;

      return true;
}

int main()
{
   EnumWindows(EnumWindowsProc,NULL);

   return 0;
}

Alternatively, to filter for Notepad:

Or

#include <windows.h>
#include <iostream>
#include <string.h>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

      cout << buf << " HWND: " << hwnd << endl;

      return true;
}

int main()
{
   EnumWindows(EnumWindowsProc,(LPARAM)"Notepad");

   return 0;
}
0
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.

 

Author Comment

by:Troudeloup
ID: 20359443

#include <windows.h>
#include <iostream>
#include <string.h>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

      cout << buf << " HWND: " << hwnd << endl;

      return true;
}

int main()
{
   EnumWindows(EnumWindowsProc,(LPARAM)"Notepad");

   return 0;
}






I don't understand the code,  i was expecting an array to be returnd and then i can send the hwnd to setforground()

yet this returns a bool
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 20359490
If you need an array, you can use

#include <windows.h>
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;

vector<HWND> vw; // array for HWND

BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
      char buf [1024];
      GetWindowText(hwnd,buf,1024);

      if(!strstr(buf,(char*)lParam)) return true;

       vw.push_back(hwnd);

      return true;
}

int main()
{
   EnumWindows(EnumWindowsProc,(LPARAM)"Notepad");

   for (vector<HWND>::iterator i = vw.begin(); i != vw.end(); ++i)
    cout << "HWND: " << *i << endl;

   return 0;
}
0
 

Author Comment

by:Troudeloup
ID: 20360358
how do I find out how many items the vector has?
0
 
LVL 86

Expert Comment

by:jkr
ID: 20360370
Just use

size_t sz = vw.size();
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Handle Exceptions during instantiation 28 423
How to convert MFC::CString to UTF8 wchar_t* 10 287
Dialogbox API leak? 18 99
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 194
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…
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…
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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

830 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