943,682 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8468
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 12th, 2008
0

Return the window title

Expand 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?
Similar Threads
Reputation Points: -14
Solved Threads: 1
Junior Poster in Training
FTProtocol is offline Offline
99 posts
since May 2008
Aug 12th, 2008
0

Re: Return the window title

Click to Expand / Collapse  Quote originally posted by FTProtocol ...
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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Aug 12th, 2008
0

Re: Return the window title

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?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,949 posts
since Aug 2005
Aug 12th, 2008
0

Re: Return the window title

> 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:

C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 13th, 2008
0

Re: Return the window title

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Feb 4th, 2009
0

Re: Return the window title

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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fskreuz is offline Offline
14 posts
since Feb 2009
Feb 4th, 2009
0

Re: Return the window title

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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Feb 4th, 2009
1

Re: Return the window title

IsWindowVisible() might be useful.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Feb 4th, 2009
0

Re: Return the window title

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fskreuz is offline Offline
14 posts
since Feb 2009
Feb 10th, 2009
0

Re: Return the window title

Click to Expand / Collapse  Quote originally posted by Clockowl ...
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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fskreuz is offline Offline
14 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to use an array element in if-statement?
Next Thread in C++ Forum Timeline: HELP!! trying to write a quiz program part 1





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC