Tuesday, February 17, 2015

Firefox Inside VFP Form

Nothing new here, I used the same approach in the Chrome one but worked on FireFox itself this time.  This is to see if my suspicion that the reason for WinAPIs' inability to control the titlebar of Chrome is because it really has its own titlebar.

Also, this is based on advise of Bernard Bout to try this on FireFox.  This works on my end since I am using an older version of FF which is 12.  The titlebar is hidden properly.  I have not tried it though on newer version of FF as I don't download any being I prefer Chrome.  So it is up to you guys to test this on those newer version.


Anyway, here are the codes, with the same approach I used on Chrome and some changes on Windows Styles:



Public oForm
oForm = Createobject('form1')
oForm.Show()

Define Class form1 As Form
      Height = 600
      Width = 900
      Caption = "Firefox Inside VFP Form"
      Desktop = .T.
      AutoCenter = .T.

      Procedure Load
            Declare Integer FindWindow In user32;
                  STRING lpClassName, String lpWindowName

            Declare Integer GetActiveWindow  In user32

            Declare Integer GetWindow In user32 Integer HWnd, Integer wFlag

            Declare Integer GetWindowLong In User32 Integer HWnd, Integer nIndex

            Declare Integer GetWindowTextLength In user32 Integer HWnd

            Declare Integer IsWindow In user32 Integer HWnd

            Declare Integer IsWindowVisible In user32 Integer HWnd

            Declare Integer GetWindowText In user32;
                  INTEGER HWnd, String @lpString, Integer cch

            Declare Integer ShellExecute In shell32.Dll ;
                  INTEGER hndWin, ;
                  STRING cAction, ;
                  STRING cFileName, ;
                  STRING cParams, ;
                  STRING cDir, ;
                  INTEGER nShowWin

            Declare Integer SetWindowLong In user32 Integer HWnd,;
                  INTEGER nIndex, Integer dwNewLong

            Declare Integer SetWindowPos In user32;
                  INTEGER HWnd,;
                  INTEGER hWndInsertAfter,;
                  INTEGER x,;
                  INTEGER Y,;
                  INTEGER cx,;
                  INTEGER cy,;
                  INTEGER uFlags

            Declare Integer SetParent In user32;
                  INTEGER hWndChild,;
                  INTEGER hWndNewParent

            Declare Sleep In kernel32 Integer

            #Define GW_HWNDFIRST  0
            #Define GW_HWNDLAST   1
            #Define GW_HWNDNEXT   2
      Endproc

      Procedure KeyPress
            Lparameters nKeyCode, nCtrlShift
            If nKeyCode = 27
                  Thisform.Release
            Endif
      Endproc

      Procedure Init
            Local lcURL
            lcURL = 'http://www.clipartpanda.com/clipart_images/fox-clip-art-2605413'
            ShellExecute(0,'open','firefox.exe',m.lcURL,'',1)
            Activate Window (Thisform.Name)
            Sleep(1000)
            Thisform.SearchProcess()
      Endproc

      Procedure SearchProcess()
            With This
                  Local hWinActive, hWindow, lcWinText
                  hWinActive = GetActiveWindow()
                  hWindow = -1

                  Do While hWindow <> GetWindow(hWinActive, GW_HWNDLAST)
                        If hWindow = -1
                              hWindow = GetWindow(hWinActive, GW_HWNDFIRST)
                        Else
                              hWindow = GetWindow(hWindow, GW_HWNDNEXT)
                        Endif

                        If IsWindow(hWindow) <> 0 And IsWindowVisible(hWindow) <> 0;
                                    And GetWindowTextLength(hWindow) > 0
                              lcWinText = .GetWinText(hWindow)
                              nHwnd = FindWindow(Null, m.lcWinText)
                              If 'Mozilla Firefox' $ m.lcWinText
                                          Exit
                              Endif
                        Endif
                  Enddo
                  lnStyle = GetWindowLong(m.nHwnd, -6)
                  SetWindowLong(m.nHwnd, -16, Bitxor(lnStyle, 0x00400000))

                  With This
                        SetParent(m.nHwnd,.HWnd)
                        SetWindowPos(m.nHwnd, 1, 0, 0, .Width, .Height,0x0040)
                  Endwith
            Endwith

      Endproc

      Function  GetWinText(hWindow)
            Local lnBufsize, lcBuffer
            lnBufsize = 1024
            lcBuffer = Repli(Chr(0), lnBufsize)
            lnBufsize = GetWindowText(hWindow, @lcBuffer, lnBufsize)
            Return  Iif(lnBufsize=0, "", Left(lcBuffer,lnBufsize))
      Endfunc

Enddefine

The same approach can be used on any other browser or any other 3rd party app that cannot be easily reached via automation.  Though I would prefer automation if it is available as it gives us more control over the object.

No comments:

Post a Comment