Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get a third-party application window to appear inside VFP instead of in the Windows desktop?

API Functions

How do I get a third-party application window to appear inside VFP instead of in the Windows desktop?

by  wgcs  Posted    (Edited  )
The best way to work with other applications is through documented interfaces, such as through OLE, like you can with MS Word:
Code:
  word = createobject('word.application')
  word.visible = .t.

However, sometimes you really need to work with an application that doesn't provide an interface to do what you need. In these cases, you can resort to using Windows API calls to manipulate the other application. You can use SendMessage to simulate Clicks on buttons or typing.

If you want the other application to appear like it is a part of VFP (instead of having its own top-level window in the Windows desktop), here's how. Be warned, it probably won't cooperate with other VFP windows (since they aren't "true" Windows), it'll always be on top of the other VFP windows that aren't top-level windows, but it does work, without any other consequenses that I see:

Just issue:
TakeWindow( "Third party application window title here" )

(New optional second parameter:
TakeWindow("Third party app Title","VFP Child Window Title")
or
TakeWindow("VFP Child Window Title","VFP Child Window Title")
Use the second parameter to place the third party app or a vfp child window inside a child window that is already inside the vfp main window)


Code:
* Example use:
oWrd = createobject('word.application')
oWrd.height = _screen.height/2
oWrd.width  = _screen.width/2
oWrd.top    = _screen.height/8
oWrd.left   = _screen.width/8
res = takewindow(oWrd.caption)
oWrd.visible = .t.

* Or:
SET HELP TO MyHelp.chm
HELP My Help Topic
res=TakeWindow( "My Help File's Window Title" )


* Author.....: William GC Steinford
* Date.......: Nov 6, 2002
* Abstract...: Place Any window inside any VFP window (main VFP window or DEFINE'd Window)
PROCEDURE TakeWindow
LPARAMETERS pcWindowTitle, pcParentWindow
LOCAL lnWHND, lnVFP, lnRes, lnCNT, lnWinCnt

DECLARE INTEGER FindWindow in win32api as apiFindWindow ;
  INTEGER nClass, STRING cName
DECLARE INTEGER FindWindowEx in win32api as apiFindWindowEx ;
  INTEGER nParent, INTEGER nChildAfter, ;
  INTEGER nClass, STRING cName

* First get the VFP window handle
lnVFP = apiFindWindow( 0, _Screen.Caption )
if lnVFP<=0
  RETURN -1
ENDIF
lnWinCnt = lnVFP

lnWHND = apiFindWindow( 0, pcWindowTitle )

* Not outside of VFP... if we have a parent window, check inside VFP
if lnWHND<=0 and vartype(pcParentWindow)='C' and not empty(pcParentWindow)
  lnWHND=apiFindWindowEx( lnVFP, 0, 0, pcWindowTitle ) && Look in VFP window
  if lnWHND=0 && Not in Main VFP window... This is the case if this window
              && was created by VFP
    && Container windows in VFP have no title
    lnCNT  = apiFindWindowEx( lnVFP, 0, 0, ' ) && Find in container window
    do while lnWHND=0 and lnCNT<>0
      lnWHND = apiFindWindowEx( lnCNT, 0, 0, pcWindowTitle ) && Look in container
      lnCNT  = apiFindWindowEx( lnVFP, lnCNT, 0, ' ) && Find next container window
    enddo
  endif
endif

if lnWHND<0
  RETURN -1
endif

* Find the Parent Window
if vartype(pcParentWindow)='C' and not empty(pcParentWindow)
  lnWinCNT=apiFindWindowEx( lnVFP, 0, 0, pcParentWindow ) && Look in container
  
  if lnWinCnt=0 && Not in Main VFP window... This is the case if this window
              && was created by VFP
    && Container windows in VFP have no title
    lnCNT  = apiFindWindowEx( lnVFP, 0, 0, ' ) && Find in container window
    do while lnWinCnt=0 and lnCNT<>0
      lnWinCnt = apiFindWindowEx( lnCNT, 0, 0, pcParentWindow ) && Look in container
      lnCNT  = apiFindWindowEx( lnVFP, lnCNT, 0, ' ) && Find next container window
    enddo
    if lnWinCNT<=0
      RETURN -1  && Couldnt find new parent window
    endif
  endif
else
  if vartype(pcParentWindow)='N'
    if pcParentWindow>=0 
      * Use provide WHND
      lcWinCnt = pcParentWindow
    else
      * <0, indicates make child of main VFP window
      * do Nothing (leave lcWinCnt=0)
    endif
  else
    DEFINE WINDOW TakeWindowXoXoX TITLE "TakeWindowXoXoX" AT 0,0 Size 1,1
    SHOW WINDOW TakeWindowXoXoX
    * Find the empty-caption window inside VFP that contains "TakeWindowXoXoX"
    lnWinCNT =0
    lnCNT  = apiFindWindowEx( lnVFP, 0, 0, ' ) && Find first container window
    DO WHILE lnCnt<>0 and lnWinCnt=0
      lnWinCNT = apiFindWindowEx( lnCNT, 0, 0, 'TakeWindowXoXoX' ) && Find Marker Window
      if lnWinCnt=0
        lnCNT  = apiFindWindowEx( lnVFP, lnCNT, 0, ' ) && Find next container window
      endif
    ENDDO
    if lnWinCnt<>0
      lnWinCnt = lnCnt
    endif
    RELEASE WINDOW TakeWindowXoXoX
  endif
endif


DECLARE INTEGER SetParent in Win32API as apiSetParent ;
  INTEGER hWndChild, INTEGER hWndNewParent
     
if lnWinCnt<>0 && Use proper container window
  lnRes = apiSetParent( lnWHND, lnWinCnt )
else
  lnRes = apiSetParent( lnWHND, lnVFP )
endif
RETURN lnRes

If you are done with the window (or if you want to make a standard VFP window into a Top Level window), you can use the corresponding FreeWindow function:

Code:
* Author.....: William GC Steinford
* Date.......: Nov 6, 2002
* Abstract...: Make any VFP child/grandchild window
*              instead a child of the desktop (Top level)
PROCEDURE FreeWindow
LPARAMETERS pcWindowTitle
LOCAL lnWHND, lnVFP, lnRes, lnCNT

DECLARE INTEGER FindWindow in win32api as apiFindWindow ;
  INTEGER nClass, STRING cName
  
lnVFP = apiFindWindow( 0, _Screen.Caption )
if lnVFP<0
  RETURN -1
ENDIF

DECLARE INTEGER FindWindowEx in win32api as apiFindWindowEx ;
  INTEGER nParent, INTEGER nChildAfter, ;
  INTEGER nClass, STRING cName
  
lnWHND = apiFindWindowEx( lnVFP, 0, 0, pcWindowTitle ) && Look in container

if lnWHND=0 && Not in Main VFP window... This is the case if this window
            && was created by VFP
  lnCNT  = 0 && Container windows in VFP have no title
  lnCNT  = apiFindWindowEx( lnVFP, 0, 0, ' ) && Find in container window
  do while lnWHND=0 and lnCNT<>0
    lnWHND = apiFindWindowEx( lnCNT, 0, 0, pcWindowTitle ) && Look in container
    lnCNT  = apiFindWindowEx( lnVFP, lnCNT, 0, ' ) && Find next container window
  enddo
  if lnWHND<0
    RETURN -1
  endif
endif

DECLARE INTEGER SetParent in Win32API as apiSetParent ;
  INTEGER hWndChild, INTEGER hWndNewParent   

lnRes = apiSetParent( lnWHND, 0 )
RETURN lnRes
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top