导航:  »没有这个级别以上的主题«

CWebBrowser Class

上一页返回章节概述下一页

使用OLE容器(COleCon类)托管Web应用程序的WebBrowser控件的实例的Wrapper类。

 

它还提供了连接和断开由WebBrowser控件触发的事件的方法,为DWebBrowser2和IDocHostUIHandler2接口设置事件处理程序(回调过程的指针),导航到URL的方法,以及获取对OLE的引用的功能 容器类和IWebBrowser2接口。

 

文件AfxExDisp.bi提供了使用抽象方法调用WebBrowser接口的方法的声明。

 

WebBrowser事件接收器类在文件CWebBrowserEventsImpl.inc中提供,DocHostUIHandler事件接收器类在文件CDocHostUIHandler2Impl.inc中提供。

 

示例

 

' ########################################################################################

' Microsoft Windows

' Contents: WebBrowser customization test

' Compiler: FreeBasic 32 & 64 bit

' Copyright (c) 2016 José Roca. Freeware. Use at your own risk.

' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER

' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF

' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

' ########################################################################################

 

#define UNICODE

#INCLUDE ONCE "Afx/CWindow.inc"

#INCLUDE ONCE "Afx/AfxCtl.inc"

#INCLUDE ONCE "Afx/CWebBrowser/CWebBrowser.inc"

USING Afx

 

CONST IDC_WEBBROWSER = 1001

CONST IDC_SATUSBAR = 1002

 

DECLARE FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                        BYVAL hPrevInstance AS HINSTANCE, _

                        BYVAL szCmdLine AS ZSTRING PTR, _

                        BYVAL nCmdShow AS LONG) AS LONG

 

 END WinMain(GetModuleHandleW(NULL), NULL, COMMAND(), SW_NORMAL)

 

DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

' // 远期声明

DECLARE SUB WebBrowser_StatusTextChangeProc (BYVAL hwndContainer AS HWND, BYVAL pwszText AS WSTRING PTR)

DECLARE SUB WebBrowser_DocumentCompleteProc (BYVAL hwndContainer AS HWND, BYVAL pdisp AS IDispatch PTR, BYVAL vUrl AS VARIANT PTR)

DECLARE FUNCTION WebBrowser_HtmlDocumentEventsProc (BYVAL hwndContainer AS HWND, BYVAL dispId AS LONG, BYVAL pEvtObj AS IHTMLEventObj PTR) AS BOOLEAN

DECLARE FUNCTION DocHostUI_ShowContextMenuProc (BYVAL hwndContainer AS HWND, BYVAL dwID AS DWORD, BYVAL ppt AS POINT PTR, BYVAL pcmdtReserved AS IUnknown PTR, BYVAL pdispReserved AS IDispatch PTR) AS HRESULT

DECLARE FUNCTION DocHostUI_GetHostInfo (BYVAL hwndContainer AS HWND, BYVAL pInfo AS DOCHOSTUIINFO PTR) AS HRESULT

DECLARE FUNCTION DocHostUI_TranslateAccelerator (BYVAL hwndContainer AS HWND, BYVAL lpMsg AS LPMSG, BYVAL pguidCmdGroup AS const GUID PTR, BYVAL nCmdID AS DWORD) AS HRESULT

 

' ========================================================================================

' Main

' ========================================================================================

FUNCTION WinMain (BYVAL hInstance AS HINSTANCE, _

                BYVAL hPrevInstance AS HINSTANCE, _

                BYVAL szCmdLine AS ZSTRING PTR, _

                BYVAL nCmdShow AS LONG) AS LONG

 

 ' // 设置处理DPI感知

 ' // The recommended way is to use a manifest file

 AfxSetProcessDPIAware

 

 ' // Creates the main window

 DIM pWindow AS CWindow

 ' -or- DIM pWindow AS CWindow = "MyClassName" (use the name that you wish)

 DIM hwndMain AS HWND = pWindow.Create(NULL, "Embedded WebBrowser control with events and customization", @WndProc)

 ' // Sizes it by setting the wanted width and height of its client area

 pWindow.SetClientSize(750, 450)

 ' // Centers the window

 pWindow.Center

 

 ' // Add a status bar

 DIM hStatusbar AS HWND = pWindow.AddControl("Statusbar", , IDC_SATUSBAR)

 

 ' // Add a WebBrowser control

 DIM pwb AS CWebBrowser = CWebBrowser(@pWindow, IDC_WEBBROWSER, 0, 0, pWindow.ClientWidth, pWindow.ClientHeight)

 ' // Connect events

 pwb.Advise

 ' // Set event callback procedures

 pwb.SetEventProc("StatusTextChange", @WebBrowser_StatusTextChangeProc)

 pwb.SetEventProc("DocumentComplete", @WebBrowser_DocumentCompleteProc)

 pwb.SetEventProc("HtmlDocumentEvents", @WebBrowser_HtmlDocumentEventsProc)

 ' // Set the IDocHostUIHandler interface

 pwb.SetUIHandler

 ' // Set event callback procedures

 pwb.SetUIEventProc("ShowContextMenu", @DocHostUI_ShowContextMenuProc)

 pwb.SetUIEventProc("GetHostInfo", @DocHostUI_GetHostInfo)

 pwb.SetUIEventProc("TranslateAccelerator", @DocHostUI_TranslateAccelerator)

 ' // Navigate to a URL

 pwb.Navigate("http://com.it-berater.org/")

'   pwb.Navigate("http://www.jose.it-berater.org/smfforum/index.php")

 

 ' // Display the window

 ShowWindow(hWndMain, nCmdShow)

 UpdateWindow(hWndMain)

 

 ' // Dispatch Windows messages

 DIM uMsg AS MSG

 WHILE (GetMessageW(@uMsg, NULL, 0, 0) <> FALSE)

    IF AfxForwardMessage(GetFocus, @uMsg) = FALSE THEN

       IF IsDialogMessageW(hWndMain, @uMsg) = 0 THEN

          TranslateMessage(@uMsg)

          DispatchMessageW(@uMsg)

       END IF

    END IF

 WEND

 FUNCTION = uMsg.wParam

 

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Main window procedure

' ========================================================================================

FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

 

 SELECT CASE uMsg

 

    CASE WM_COMMAND

       SELECT CASE LOWORD(wParam)

          CASE IDCANCEL

             ' // If ESC key pressed, close the application by sending an WM_CLOSE message

             IF HIWORD(wParam) = BN_CLICKED THEN

                SendMessageW hwnd, WM_CLOSE, 0, 0

                EXIT FUNCTION

             END IF

       END SELECT

 

    CASE WM_SIZE

       ' // Optional resizing code

       IF wParam <> SIZE_MINIMIZED THEN

          ' // Resize the status bar

          DIM hStatusBar AS HWND = GetDlgItem(hwnd, IDC_SATUSBAR)

          SendMessage hStatusBar, uMsg, wParam, lParam

          ' // Calculate the size of the status bar

          DIM StatusBarHeight AS DWORD, rc AS RECT

          GetWindowRect hStatusBar, @rc

          StatusBarHeight = rc.Bottom - rc.Top

          ' // Retrieve a pointer to the CWindow class

          DIM pWindow AS CWindow PTR = AfxCWindowPtr(hwnd)

          ' // Move the position of the control

          IF pWindow THEN pWindow->MoveWindow GetDlgItem(hwnd, IDC_WEBBROWSER), _

             0, 0, pWindow->ClientWidth, pWindow->ClientHeight - StatusBarHeight / pWindow->ryRatio, CTRUE

       END IF

 

         CASE WM_DESTROY

       ' // Ends the application by sending a WM_QUIT message

       PostQuitMessage(0)

       EXIT FUNCTION

 

 END SELECT

 

 ' // Default processing of Windows messages

 FUNCTION = DefWindowProcW(hwnd, uMsg, wParam, lParam)

 

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Process the WebBrowser StatusTextChange event.

' ========================================================================================

SUB WebBrowser_StatusTextChangeProc (BYVAL hwndContainer AS HWND, BYVAL pwszText AS WSTRING PTR)

 IF pwszText THEN StatusBar_SetText(GetDlgItem(GetParent(hwndContainer), IDC_SATUSBAR), 0, pwszText)

END SUB

' ========================================================================================

 

' ========================================================================================

' Process the WebBrowser DocumentComplete event.

' ========================================================================================

SUB WebBrowser_DocumentCompleteProc (BYVAL hwndContainer AS HWND, BYVAL pdisp AS IDispatch PTR, BYVAL vUrl AS VARIANT PTR)

 ' // The vUrl parameter is a VT_BYREF OR VT_BSTR variant

 ' // It can be a VT_BSTR variant or a VT_ARRAY OR VT_UI1 with a pidl

 DIM varUrl AS VARIANT

 VariantCopyInd(@varUrl, vUrl)

 StatusBar_SetText(GetDlgItem(GetParent(hwndContainer), IDC_SATUSBAR), 0, "Document complete: " & AfxVarToStr(@varUrl))

 VariantClear(@varUrl)

END SUB

' ========================================================================================

 

' ========================================================================================

' Process the IDocHostUIHandler ShowContextMenu event.

' ========================================================================================

FUNCTION DocHostUI_ShowContextMenuProc (BYVAL hwndContainer AS HWND, BYVAL dwID AS DWORD, BYVAL ppt AS POINT PTR, BYVAL pcmdtReserved AS IUnknown PTR, BYVAL pdispReserved AS IDispatch PTR) AS HRESULT

 ' // This event notifies that the user has clicked the right mouse button to show the

 ' // context menu. We can anulate it returning %S_OK and show our context menu.

 ' // Do not allow to show the context menu

'   AfxMsg "Sorry! Context menu disabled"

'   RETURN S_OK

 ' // Host did not display its UI. MSHTML will display its UI.

 RETURN S_FALSE

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Process the IDocHostUIHandler GetHostInfo event.

' ========================================================================================

PRIVATE FUNCTION DocHostUI_GetHostInfo (BYVAL hwndContainer AS HWND, BYVAL pInfo AS DOCHOSTUIINFO PTR) AS HRESULT

 IF pInfo THEN

    pInfo->cbSize = SIZEOF(DOCHOSTUIINFO)

    pInfo->dwFlags = DOCHOSTUIFLAG_NO3DBORDER OR DOCHOSTUIFLAG_THEME OR DOCHOSTUIFLAG_DPI_AWARE

    pInfo->dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT

    pInfo->pchHostCss = NULL

    pInfo->pchHostNS = NULL

 END IF

 RETURN S_OK

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' Process the IDocHostUIHandler TranslateAccelerator event.

' ========================================================================================

PRIVATE FUNCTION DocHostUI_TranslateAccelerator (BYVAL hwndContainer AS HWND, BYVAL lpMsg AS LPMSG, BYVAL pguidCmdGroup AS const GUID PTR, BYVAL nCmdID AS DWORD) AS HRESULT

 ' // When you use accelerator keys such as TAB, you may need to override the

 ' // default host behavior. The example shows how to do this.

  IF lpMsg->message = WM_KEYDOWN AND lpMsg->wParam = VK_TAB THEN

     RETURN S_FALSE   ' S_OK to disable tab navigation

  END IF

 ' // return S_FALSE if you don't process the message

 RETURN S_FALSE

END FUNCTION

' ========================================================================================

 

' ========================================================================================

' For cancelable document events return TRUE to indicate that Internet Explorer should

' perform its own event processing or FALSE to cancel the event.

' ========================================================================================

PRIVATE FUNCTION WebBrowser_HtmlDocumentEventsProc (BYVAL hwndContainer AS HWND, BYVAL dispid AS LONG, BYVAL pEvtObj AS IHTMLEventObj PTR) AS BOOLEAN

 

 SELECT CASE dispid

 

    CASE DISPID_HTMLELEMENTEVENTS2_ONCLICK   ' // click event

       ' // Get a reference to the element that has fired the event

       DIM pElement AS IHTMLElement PTR

       IF pEvtObj THEN pEvtObj->lpvtbl->get_srcElement(pEvtObj, @pElement)

       IF pElement = NULL THEN EXIT FUNCTION

       DIM bstrHtml AS AFX_BSTR   ' // Outer html

       pElement->lpvtbl->get_outerHtml(pElement, @bstrHtml)

'         DIM bstrId AS AFX_BSTR   ' // identifier

'         pElement->lpvtbl->get_id(pElement, @bstrId)

       pElement->lpvtbl->Release(pElement)

       AfxMsg *bstrHtml

       SysFreeString bstrHtml

       RETURN TRUE

 

 END SELECT

 

 RETURN FALSE

 

END FUNCTION

' ========================================================================================