Solved

hWnd

Posted on 2000-05-14
7
859 Views
Last Modified: 2013-11-20
I would like my programm to change the url of my browser. I know I have to use:

::ShellExecute(hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL);

but I don't know how to get  hWnd  for the browser window.
What I want is my programm to go onto the browser window and perform the change of the URL.

How can I do it?

Thanks in advance.
0
Comment
Question by:javiscript
7 Comments
 
LVL 2

Expert Comment

by:JunHaan
ID: 2808466
javiscript,
Here is a possible solution :

You could use ::FindWindow() to get the handle of your browser. Although so, you'd need to know the exact title of your browser window, which is always different because the window title is dependent on the website's title too.

Lets say you have a Netscape browser up, and am in this page(which in this case, you are) with the title "MFC Programming Question - Netscape"...

You could insert this in one of your application's event handlers:

//Start of code

HWND hWnd; //This will hold the handle to the window

hWnd = ::FindWindow( NULL, "MFC Programming Question - Netscape" );
::ShellExecute( hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL );

//End of code

This could only work if you know the exact title of the website.
I am looking forward to a better solution, of course...
Hope this helps, for the mean time! :)
0
 
LVL 2

Expert Comment

by:JunHaan
ID: 2808475
javiscript,
Here is a possible solution :

You could use ::FindWindow() to get the handle of your browser. Although so, you'd need to know the exact title of your browser window, which is always different because the window title is dependent on the website's title too.

Lets say you have a Netscape browser up, and am in this page(which in this case, you are) with the title "MFC Programming Question - Netscape"...

You could insert this in one of your application's event handlers:

//Start of code

HWND hWnd; //This will hold the handle to the window

hWnd = ::FindWindow( NULL, "MFC Programming Question - Netscape" );
::ShellExecute( hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL );

//End of code

This could only work if you know the exact title of the website.
I am looking forward to a better solution, of course...
Hope this helps, for the mean time! :)
0
 
LVL 3

Expert Comment

by:_mb_
ID: 2808609
A better way would be, to find the window handle based on the instance handle, that ShellExecute() returns. Then you don't need to know the application title.

Here's the code:

HINSTANCE hInst = ::ShellExecute(hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL);

HWND winHandle;

if( (winHandle = ::FindWindow(NULL, NULL) )
{
     do{
          if( hInst == ::GetWindowLong(winHandle, GWL_INSTANCE) )
               break;
     }while( (winHandle = ::GetWindow(winHandle, GW_HWNDNEXT) );
}
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.

 
LVL 23

Accepted Solution

by:
chensu earned 86 total points
ID: 2808647
"hwnd
Window handle to a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box."

The hWnd is not the window handle of the Web browser. You may use NULL or your window handle. In a CWnd derived class member function, use

hWnd = this->GetSafeHwnd();
0
 

Expert Comment

by:caiman23
ID: 2808695
I get an error about

GWL_INSTANCE: undeclared identifier.

What is GWL_INSTANCE?
How can I declare it?
0
 
LVL 3

Expert Comment

by:_mb_
ID: 2808817
Sorry,it must be GWL_HINSTANCE, it's an windows api constant.
By the way: I add a check, if we have the applications parent window.

if( (winHandle = ::FindWindow(NULL, NULL) )
{
     do{
          if( !GetParent(winHandle) )
          {
                if( hInst == ::GetWindowLong(winHandle, GWL_HINSTANCE) )
                     break;
          }
     }while( (winHandle = ::GetWindow(winHandle, GW_HWNDNEXT) );
}
0
 
LVL 2

Expert Comment

by:JunHaan
ID: 2809533
I would say chensu and mb is correct...
Here is a piece of code from mb and chensu's comment to try out... it works!

//Start code

HWND hWnd;
HWND winHandle;

hWnd = this->GetSafeHwnd();

HINSTANCE hInst = ::ShellExecute(hWnd, NULL, _T("http://www.experts-exchange.com"), NULL, NULL, SW_SHOWNORMAL);

if( winHandle = ::FindWindow(NULL, NULL) )
{
   do{
      if( !::GetParent(winHandle) )
               {
                    if( (long)hInst == ::GetWindowLong(winHandle,                                                                              GWL_HINSTANCE) )
                    break;
               }
      }while( winHandle = ::GetWindow(winHandle, GW_HWNDNEXT) );
}

//End of code

                                
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Login Script to Copy Folders 12 42
cat dog challenge 18 127
How to know only "File created" with EventLog 3 35
twoTwo  challenge 35 107
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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