Close and/or move a window opened up with shellexecute RRS feed

  • Question

  • Can one control a window opened up with shellexecute?

    Specifically I'm looking to either close a pdf opened up, or make sure only one instance of the pdf viewer is open at a time (Including tabs within a window). Other options I'd like to explore: Move/size/minimize the window.

    Thanks for your input.

    Monday, July 13, 2020 1:46 PM

All replies

  • ShellExecute() will create a process off your hook. Main advantage: You can ShellExecute a PDF file and don't need to know or find out what PDF viewer is available or will start. The little control you have is in specifying which state the new window gets. But according to https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea it's up to the called program to decide whether it follows your wish or decides to act different.

    There would be ways to get at the window in coimplicated ways, like keeping track which windows are open before you Shellexecute and after. In really rare circumstances a new Window appears for other reasons. For example push notification windows coming from the web causing a message window. But you may identify which new window is caused by your ShellExecute by identifying the document file name you specified in the title bar or whatever other sign.

    Tabs? Forget it. Controls like VFP controls already are non standard for tools automating applications for different purposes like automatic tests of the user interface via emulated user, not by unit testing the components. So when VFP forms and controls evade such detection of the inner controls you can't expect to be able to address the inner workings of other applications. One of the few givens is how ActiveX always populate their own Windows with HWND handle, that's not only VFPs way to embed them into its forms. But as VFP controls have no HWND so do other applications 

    Is it hopeless? You get much more control than just the Hwnd, when you go for creating the new process yourself, key word is CreateProces: 


    It's not as easy to use, the first hurdle might be to know what executable to start for a certain document/file tye or extension, but there also are ways to find out by the registry. The main handle of your control now will be the process handle, not a Windows handle, though. And you may find the main Window of a process. Again you have the before/after overview of all Windows, when anything else fails. In the end not even the OS is master of all the inner workings of Windows. It sure can stop threads and processes misbehaving and violating memory access

    Also, you may decide for yourself what PDF viewr to use. Obviously regarding PDF you have several options of loading a PDF into a browser, autommate IE or even embed it in a webbrowser control in your application. And with webbrowser control you can arrange it as you like, ie in pages of a pageframe. That would be my solution to your very concrete PDF handling problem. You even don't have to rely on a PDF viewer having embedded a plugin for the IE browser, you find PDF viewer JS code or services from Google to use, so you'll not only set up the browser as a frame for whatever starts in there, you become the "owner" of the HTML div, iframe or other root element displaying the PDF, if you want, so you may even organise multiple PDFs embedded within one HTML document in onw Webbrowser conrtol.

    That way is very specific to PDF, though. The web world has brouhgt it to that state for sake of having PDFs under control inside their realm, too. For example look at https://github.com/mozilla/pdf.js/ or https://gist.github.com/tzmartin/1cf85dc3d975f94cfddc04bc0dd399be

    Bye, Olaf.

    Tuesday, July 14, 2020 5:30 AM
  • Hi

    what i understood is you want to control any opened window on your computer

    you can use Win32Api for checking a window with particular caption is opened or not

    DECLARE INTEGER FindWindow IN Win32API STRING @lpClassName, STRING @lpWindowName
    lnhWnd = FindWindow(0, @lcformcaption) 

    for example to find "Powerpoint" is open or not, you can use

    FindWindow(0, "Powerpoint")

    You can use "win32 api" functions to control any window on computer.

    Found one link for the code




    Thanks Mukesh Khandelwal

    • Edited by Mukesh Khandelwall Tuesday, July 14, 2020 6:36 AM found link regarding your question
    Tuesday, July 14, 2020 6:33 AM