Windows Calc hwnds 
Author Message
 Windows Calc hwnds

I am writing an app which amongst other things has a toggle button for
opening & closing  calc.exe
all works well with one exception:
If I change the calc from standard to scientific or vice-versa a new hwnd
seems to be generated so I am unable to keep track of the processID and hwnd
etc, is there a way that i can detect that the calc has been changed?

Thanks in advance
Howard



Mon, 18 Mar 2002 03:00:00 GMT  
 Windows Calc hwnds
In changing from normal to scientific mode calc destroys its main window and
recreates a new one. This renders any saved hwnd useless.

To test if a window handle is still valid, you can call IsWindow (hwnd) ..
if it's a valid window, the return value is non-zero. If its 0, the hwnd is
useless BUT, with a Shelled app, the process ID originally returned from
shell is **not** invalid, so its a simple matter for calling a method to
return the new hwnd from the new window with the old process ID ...

ie ... (use the API viewer for the declares, or visit the link below for the
demo)

Private hwndApp as Long
Private hProcID   as Long

Private Sub Command1_Click()

   hWndApp = StartShell("calc.exe")

End Sub

Private Function StartShell(sApplication As String) As Long

   Dim hProcessID As Long
   Dim hWndApp As Long

  'start using shell, and get the process ID
   hProcessID = Shell(sApplication, vbNormalFocus)

  'used for this demo ...
   hProcID  = hProcessID

  'get the hwnd to the shelled process
   hWndApp = GethWndFromProcessID(hProcessID)

   StartShell = hWndApp

End Function

At this point you have a valid handle to the new calc window.  Now change it
to scientific (or back to standard).  hWnd is now invalid, so use something
like ...

  'is handle good ?
   If IsWindow(hWndApp) = 0 Then

     'nope, so get the handle using the hProcID once again
      hWndApp = GethWndFromProcessID(hProcID)

     'do this to show it really worked!
      Call SetWindowText(hWndApp, "Got it " & CStr(hWndApp))
      Call BringWindowToTop(hWndApp)

   End If

I have a demo on my site that does 80% of this work
http://www.mvps.org/vbnet/code/system/shellhwnd.htm ... with minor mods (the
above code) it will do what you need.

Here is the workhorse ...

Public Function GethWndFromProcessID(hProcessIDToFind As Long) As Long

    Dim hWndDesktop  As Long
    Dim hWndChild  As Long
    Dim hWndChildProcessID  As Long

    On Error GoTo GethWndFromProcessID_Error

   'get the handle to the desktop
    hWndDesktop = GetDesktopWindow()

   'get the first child under the desktop
    hWndChild = GetWindow(hWndDesktop, GW_CHILD)

   'hwndchild will = 0 when no more child windows are found
    Do While hWndChild <> 0

       'get the ThreadProcessID of the window
        Call GetWindowThreadProcessId(hWndChild, hWndChildProcessID)

       'if it matches the target, exit returning that value
        If hWndChildProcessID = hProcessIDToFind Then
            GethWndFromProcessID = hWndChild
            Exit Do
        End If

       'not found, so get the next hwnd
        hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)

    Loop

Exit Function

GethWndFromProcessID_Error:
    GethWndFromProcessID = 0
    Exit Function

End Function

--

Randy Birch, MVP Visual Basic

http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/

Please correspond only using the newsgroups so all can benefit.


| I am writing an app which amongst other things has a toggle button for
| opening & closing  calc.exe
| all works well with one exception:
| If I change the calc from standard to scientific or vice-versa a new hwnd
| seems to be generated so I am unable to keep track of the processID and
hwnd
| etc, is there a way that i can detect that the calc has been changed?
|
| Thanks in advance
| Howard
|
|



Mon, 18 Mar 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Complete Windows HWnds List

2. Complete Windows Hwnds List

3. Windows CALC.EXE

4. Getting result of windows calc.exe

5. shell cannot hide calc.exe in windows 2000

6. Persistant HWNDs

7. How to find All Existing hWnds

8. ShellExecute and hWnds

9. Sending text to a textbox using hWnds and SendMessage

10. Get a list of all running executables and their hWnds

11. ShellExecute and hWnds

12. calc lenght of string in form as you type

 

 
Powered by phpBB® Forum Software