CWindow: Google Maps

Started by José Roca, April 12, 2011, 02:52:23 PM

Previous topic - Next topic

José Roca

Sometimes, we will need to load a rel html page instead of creating it on the fly. This is the case of Google Maps, that uses the onload event to run the initialize script, and this event is only fired if you navigate to the page.


<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  function initialize() {
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

</script>
</head>
<body scroll="no" style="MARGIN: 10px 10px 10px 10px" onload="initialize()">
  <div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>


However, the solution is easy: instead of passing the html code in the AddWebBrowser method, pass the url of the page.


#COMPILE EXE
#DIM ALL
%UNICODE = 1

' // Include files for external files
%USEWEBBROWSER = 1            ' // Use the WebBrowser control
#INCLUDE ONCE "CWindow.inc"   ' // CWindow class
#INCLUDE ONCE "mshtml.inc"    ' // MSHTML

' // Identifier
%IDC_WEBBROWSER = 101

' ########################################################################################
' Main
' ########################################################################################
FUNCTION WinMain (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS WSTRINGZ PTR, BYVAL nCmdShow AS LONG) AS LONG

   ' // Create an instance of the class
   LOCAL pWindow AS IWindow
   pWindow = CLASS "CWindow"
   IF ISNOTHING(pWindow) THEN EXIT FUNCTION

   ' // Create the main window
   pWindow.CreateWindow(%NULL, "CWindow.AddWebBrowser Demo: Google Maps", 0, 0, 700, 500, -1, -1, CODEPTR(WindowProc))
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a WebBrowser control and display Goodle Maps
   LOCAL hCtl AS DWORD
   LOCAL bstrURL AS WSTRING
   bstrURL = EXE.Path$ & "GoogleMap_01.html"
   hCtl = pWindow.AddWebBrowserControl(pWindow.hwnd, %IDC_WEBBROWSER, bstrURL, 5, 5, pWindow.ClientWidth - 5, pWindow.ClientHeight - 5, -1, -1)

   ' // Default message pump (you can replace it with your own)
   pWindow.DoEvents(nCmdShow)

END FUNCTION
' ########################################################################################

' ========================================================================================
' Main callback function.
' ========================================================================================
FUNCTION WindowProc (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

   LOCAL rc AS RECT

   SELECT CASE uMsg

      CASE %WM_SYSCOMMAND
         ' // Capture this message and send a WM_CLOSE message
         ' // Note: Needed with some OCXs, that otherwise remain in memory
         IF (wParam AND &HFFF0) = %SC_CLOSE THEN
            SendMessage hwnd, %WM_CLOSE, 0, 0
            EXIT FUNCTION
         END IF

      CASE %WM_COMMAND
         SELECT CASE LO(WORD, wParam)
            CASE %IDCANCEL
               ' // If the Escape key has been pressed...
               IF HI(WORD, wParam) = %BN_CLICKED THEN
                  ' // ... close the application by sending a WM_CLOSE message
                  SendMessage hwnd, %WM_CLOSE, 0, 0
                  EXIT FUNCTION
               END IF
         END SELECT

      CASE %WM_SIZE
         IF wParam <> %SIZE_MINIMIZED THEN
            ' // Get the client area of the main window
            GetClientRect hwnd, rc
            ' // Resize the control
            MoveWindow GetDlgItem(hwnd, %IDC_WEBBROWSER), 5, 5, rc.nRight - rc.nLeft - 10, rc.nBottom - rc.nTop - 10, %TRUE
         END IF

      CASE %WM_DESTROY
         ' // End the application
         PostQuitMessage 0
         EXIT FUNCTION

   END SELECT

   ' // Pass unprocessed messages to Windows
   FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION
' ========================================================================================