Return the window title

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 99
Reputation: FTProtocol has a little shameless behaviour in the past 
Solved Threads: 1
FTProtocol FTProtocol is offline Offline
Junior Poster in Training

Return the window title

 
0
  #1
Aug 12th, 2008
Just wondering if theres a function to get the window title of your browser.

Like if you were to visit www.myspace.com your window title would be: www.myspace.com - Mozilla Firefox

or w/e browser you happen to use.

Is there a way to get the title of a window?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Return the window title

 
0
  #2
Aug 12th, 2008
Originally Posted by FTProtocol View Post
Just wondering if theres a function to get the window title of your browser.

Like if you were to visit www.myspace.com your window title would be: www.myspace.com - Mozilla Firefox

or w/e browser you happen to use.

Is there a way to get the title of a window?
Use GetWindowText() once you have the handle to the correct window.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Return the window title

 
0
  #3
Aug 12th, 2008
Call EnumWindows() to get a list of all the top-level windows so that you can get the handle to the browser window. The problem is -- how will you know which one is the browser?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,428
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 116
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Return the window title

 
0
  #4
Aug 12th, 2008
> The problem is -- how will you know which one is the browser?
Simple You can use Spy++ to find out the browsers class name and then you can narrow down which windows are browsers. I found out these:

  1. Firefox's Class: MozillaUIWindowClass
  2. IExplorer Class: IEFrame
  3.  

Now using AD's suggestion, using EnumWindows you can narrow it down only to the internet explorers.

Another helpful function you could try, FindWindow.

If you have firefox, test out this code and the title of the browser should change, if not simply change to class name to work with IExplorer.
  1. #include<windows.h>
  2.  
  3. int main() {
  4. HWND firefox = FindWindow("MozillaUIWindowClass", NULL);
  5. SetWindowText(firefox, "Found it!!");
  6. return 0;
  7. }
but this only works with one browser, so your probably better to stick with AD's technique.

Hope this helps.
I need pageviews! most fun profile ever :)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Return the window title

 
0
  #5
Aug 13th, 2008
Hey, in the past I've messed with those functions a bit, here're the results. Might help you a bit.

Finds a window given a caption:
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char *argv[]){
  5. HWND hWnd;
  6. char windowName[512];
  7.  
  8. printf("Window to look for: ");
  9. fgets(windowName, 512, stdin);
  10. if (hWnd = FindWindow(NULL, windowName)){
  11. printf("hWnd : %Xh with caption:\n\t",hWnd );
  12. GetWindowText(hWnd, windowName, 512);
  13. }
  14. else printf("No window found with that caption!");
  15.  
  16. return 0;
  17. }

Enumerates all windows and displays some information about them. Then allows to hide a window.
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. char windowName[63];
  5.  
  6. int DisplayWindows(HWND hWnd, LPARAM lParam);
  7.  
  8. int main(int argc, char *argv[]){
  9. EnumWindows((WNDENUMPROC) DisplayWindows,(LPARAM) NULL);
  10.  
  11. printf("What window do you want to hide? (give the hWnd)\nhWnd: ");
  12. int hWnd;
  13. scanf("%d", &hWnd);
  14.  
  15. if (!IsWindow((HWND)hWnd)){
  16. printf("Window doesn't exist!");
  17. return 0;
  18. }
  19.  
  20. if(!ShowWindow((HWND) hWnd, SW_HIDE)){
  21. printf("Window was already hidden...");
  22. }
  23.  
  24. return 0;
  25. }
  26.  
  27. int DisplayWindows(HWND hWnd, LPARAM lParam){
  28. GetWindowText(hWnd, windowName, 63);
  29. if (windowName[0] == '\0') return 1;
  30.  
  31. printf("hWnd: %d\nClass: ", hWnd);
  32. GetClassName(hWnd, windowName, 63);
  33. puts(windowName);
  34. printf("Caption:");
  35. GetWindowText(hWnd, windowName, 63);
  36. puts(windowName);
  37. printf("\n");
  38. return 1;
  39. }
Last edited by Clockowl; Aug 13th, 2008 at 11:00 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: fskreuz is an unknown quantity at this point 
Solved Threads: 1
fskreuz fskreuz is offline Offline
Newbie Poster

Re: Return the window title

 
0
  #6
Feb 4th, 2009
Your code works! But it includes those windows which are hidden or those programs without interface. In short, how to get the captions of windows that can be seen?

Firefox has a visible window, I would like to include it but my firewall, by your method, can be seen but has no visible window so I would like to exclude it.

Can anyone shed some light to this?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Return the window title

 
0
  #7
Feb 4th, 2009
You can probably get the state of a window by using an API, have you tried looking for it? I don't know any from the top of my head. If there isn't any, you could try and hide the window, and if the function ShowWindow() returns 0, it was already in the state you tried to set it in. I think. Better check on that though.
Last edited by Clockowl; Feb 4th, 2009 at 1:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Return the window title

 
1
  #8
Feb 4th, 2009
IsWindowVisible() might be useful.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: fskreuz is an unknown quantity at this point 
Solved Threads: 1
fskreuz fskreuz is offline Offline
Newbie Poster

Re: Return the window title

 
0
  #9
Feb 4th, 2009
thanks mitrmkar, that was really a help. I'm also glad that this thread is alive. I got my solution though. I used key strokes and mouse clicks to get the windows that are being used, although your method is useful too. I'll weigh in if which one will be more useful to my operation.

but another problem is here. i have this snippet in my code.

  1. //from the top part of the code
  2. char lastwindow[MAX_PATH];
  3. char currentwindow[MAX_PATH];
  4.  
  5.  
  6. /* this one is from the bottom, where it runs when a key or mouse button is pressed (a very common activity when using the pc) and eliminates the chance of getting hidden windows. FStrem is a text file to where it records rather than using the printf() */
  7.  
  8. HWND mainwindow;
  9. mainwindow = GetForegroundWindow();
  10. GetWindowText(mainwindow,currentwindow,sizeof(currentwindow));
  11. if(lastwindow==currentwindow)
  12. {}
  13. else
  14. if(lastwindow!=currentwindow)
  15. {
  16. strcpy (lastwindow,currentwindow);
  17. FStream << "\n\n[" << currentwindow << "]\n\n";
  18. }

summary is, when the active window changes, the currentwindow variable should change value, record the current active window title to a file and copy the value to the lastwindow variable. if the active window has not changed, it will do nothing and wait until mouseclicks or keypresses are done in another window for recording. But it seems that everytime i activate this part of the script, even though i did not change window, it still types in the window title, which it should not.

the values that are being recorded are window captions. is something wrong with the stored values? they seem to be the same when i try to printf them but come out different using this script.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: fskreuz is an unknown quantity at this point 
Solved Threads: 1
fskreuz fskreuz is offline Offline
Newbie Poster

Re: Return the window title

 
0
  #10
Feb 10th, 2009
Originally Posted by Clockowl View Post
Hey, in the past I've messed with those functions a bit, here're the results. Might help you a bit.

Finds a window given a caption:
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. int main(int argc, char *argv[]){
  5. HWND hWnd;
  6. char windowName[512];
  7.  
  8. printf("Window to look for: ");
  9. fgets(windowName, 512, stdin);
  10. if (hWnd = FindWindow(NULL, windowName)){
  11. printf("hWnd : %Xh with caption:\n\t",hWnd );
  12. GetWindowText(hWnd, windowName, 512);
  13. }
  14. else printf("No window found with that caption!");
  15.  
  16. return 0;
  17. }

Enumerates all windows and displays some information about them. Then allows to hide a window.
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. char windowName[63];
  5.  
  6. int DisplayWindows(HWND hWnd, LPARAM lParam);
  7.  
  8. int main(int argc, char *argv[]){
  9. EnumWindows((WNDENUMPROC) DisplayWindows,(LPARAM) NULL);
  10.  
  11. printf("What window do you want to hide? (give the hWnd)\nhWnd: ");
  12. int hWnd;
  13. scanf("%d", &hWnd);
  14.  
  15. if (!IsWindow((HWND)hWnd)){
  16. printf("Window doesn't exist!");
  17. return 0;
  18. }
  19.  
  20. if(!ShowWindow((HWND) hWnd, SW_HIDE)){
  21. printf("Window was already hidden...");
  22. }
  23.  
  24. return 0;
  25. }
  26.  
  27. int DisplayWindows(HWND hWnd, LPARAM lParam){
  28. GetWindowText(hWnd, windowName, 63);
  29. if (windowName[0] == '\0') return 1;
  30.  
  31. printf("hWnd: %d\nClass: ", hWnd);
  32. GetClassName(hWnd, windowName, 63);
  33. puts(windowName);
  34. printf("Caption:");
  35. GetWindowText(hWnd, windowName, 63);
  36. puts(windowName);
  37. printf("\n");
  38. return 1;
  39. }
what does the %d value mean? what are those numbers?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC