Re: Current page focus.

Discussions related to mouse, keyboard, fonts and Graphical User Interface
Hated Moron

Re: Current page focus.

Post by Hated Moron »

On 16/03/2023 15:28, The Famous Cash via groups.io wrote (cross-posted from the Discussion Group):
Some time ago I tried to get help to close a web page from a program once it had been opened with SYS "ShellExecute", @hwnd%, "open",URL$, 0, 0, 1. Unfortunately, I was unable to achieve this although I have worked on it for some time.
When you say "I have worked on it for some time" I take it that means you have tried sending the WM_CLOSE message to the window, since this is the most straightforward approach, and it usually works.

I appreciate, of course, that the difficulty with sending WM_CLOSE is that you need to know the window handle, and that isn't straightforward when it's a window opened with SYS "ShellExecute". However it's not impossible, Google finds this StackOverflow article explaining how you can do it. First you need the Process ID, there is code for that at the BB4W wiki: pass the sei.hProcess% value to GetProcessId.

Having got the process ID you can then use SYS "EnumWindows" to find the top-level window which belongs to that process (again, there's BB4W code for that at the wiki). I hasten to add that I've not tried it myself, but if StackOverflow can be believed it's fairly straightworward. You could usefully write a BBC BASIC function which behaves like ShellExecute but returns a window handle.

I should add that there are easier ways of finding the window handle if you can be confident that the window in which you are interested is the only one belonging to a specific class or having a specific name. Then it's just a case of calling FindWindow. But that approach will fail if there are two or more windows with the same class/name.
Please can someone advise how the program could automatically accept input without the user requiring to left-click on the screen?
Again, if you already know the window handle (in which case you should be able to close the window directly) you can also artificially send a mouse click to that window with the WM_LBUTTONDOWN message. One problem with that approach is that the top-level window of which you have the handle may not be the same window that is expecting mouse input.

So have you really exhausted the methods that are readily found with a web search? With this sort of problem Google is definitely your friend!
User avatar
JeremyNicoll
Posts: 72
Joined: Sun 26 Jul 2020, 22:22
Location: Edinburgh

Re: Current page focus.

Post by JeremyNicoll »

If the command Bob uses to display a URL opens a brand new instance of his default browser every time, then presumably (apart from identifying which instance is which) asking that instance to close should be quite easy ... but isn't there a possibility/probability that once the browser's been started, the command just opens the requested URL in another tab of the most-recently started instance?

Then the issue is in IDing the tab and sending the browser the command that would close that tab? Or sneding a simulated click to a tabs close icon (if it has one - I know Firefox tabs do).
Hated Moron

Re: Current page focus.

Post by Hated Moron »

On 17/03/2023 14:44, Bob Cash wrote (cross-posted from the Discussion Group):
As I don’t understand @hwnd% or how a “handle” is used, I’m stuck but thanks anyway.
Have you already tried the method I described (which I found at StackOverflow)? Since that involves hardly anything more than cutting-and-pasting code from the BB4W Wiki you don't need to "understand" it, just to use it.

Of course if it doesn't work a limited degree of understanding would be helpful in debugging it, but we're not there yet (unless you've already tried it and not said). Nine times out of ten, in my experience, the reason for a failure will be a trivial error and no specialised knowledge of the Windows API will be needed to fix it, just clear and logical thought.
Hated Moron

Re: Current page focus.

Post by Hated Moron »

On 17/03/2023 19:55, The Famous Cash via groups.io wrote (cross-posted from the Discussion Group):
When I use your link to WM_CLOSE I see C++ code which I do not understand.
If I go to the BB4W Wiki and search there for WM_CLOSE, it finds this BBC BASIC code for closing a window that you know the Class Name of (in the specific case of the code at the Wiki it was ""ActiveMovie Window" but of course yours will be something different):

Code: Select all

      WM_CLOSE = &10
      SYS "FindWindow", 0, "ActiveMovie Window" TO hamw%
      IF hamw% SYS "SendMessage", hamw%, WM_CLOSE, 0, 0
So there you have it, a potential solution to your problem which you can copy-and-paste into your own program. Crucially, it's a solution that you could have found yourself in seconds simply by searching the Wiki for WM_CLOSE. :?

Of course you're not entirely out of the woods, because you do need to know the Class Name of the window you want to close, and it's important that there is only one of them otherwise you might close the wrong one! But it sounds like it's more progress than you have been able to make.