PlanetSquires Forums

Support Forums => General Board => Topic started by: José Roca on April 12, 2011, 05:08:08 AM

Title: CWindow: Ajax Map
Post by: José Roca on April 12, 2011, 05:08:08 AM
 
Besides creating an instance of the WebBrowser control, the AddBrowser method of the CWindow class allows to display a web page on the fly.

This new template uses Ajax techniques to load the Virtual Earth map control and interactuate with it.


#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: AJAX Map", 0, 0, 500, 532, -1, -1, CODEPTR(WindowProc))
   ' // Center the window
   pWindow.CenterWindow

   ' // Add a WebBrowser control and display an interactive map using AJAX
   LOCAL hCtl AS DWORD
   LOCAL s AS WSTRING

   ' // Build the web page. Remember to always start it with "MSHTML:".
   s  = "MSHTML:<?xml version=""1.0""?>"
   s += "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" & $CRLF
   s += "<html>" & $CRLF
   s += "<head>" & $CRLF
   s += "<title>AJAX Map</title>" & $CRLF
   s += "" & $CRLF
   s += "<!-- Load the Virtual Earth map control. -->" & $CRLF
   s += "<script src=""http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6"" type=""text/javascript""></script>" & $CRLF
   s += "<script type=""text/javascript"">" & $CRLF
   s += "   var oMap = null;" & $CRLF
   s += "   var iZoomLevel = 0;" & $CRLF
   s += "" & $CRLF
   s += "   function GetMap()" & $CRLF
   s += "   {" & $CRLF
   s += "      oMap = new VEMap('myMap');" & $CRLF
   s += "      oMap.LoadMap();" & $CRLF
   s += "" & $CRLF
   s += "      oMap.AttachEvent(""onendzoom"", ZoomHandler);" & $CRLF
   s += "      iZoomLevel = oMap.GetZoomLevel();" & $CRLF
   s += "   }" & $CRLF
   s += "" & $CRLF
   s += "   function ZoomHandler(e)" & $CRLF
   s += "   {" & $CRLF
   s += "      iZoomLevel = oMap.GetZoomLevel();" & $CRLF
   s += "" & $CRLF
   s += "   }" & $CRLF
   s += "" & $CRLF
   s += "   function HashChangeHandler()" & $CRLF
   s += "   {" & $CRLF
   s += "      var hash = window.location.hash;" & $CRLF
   s += "      var iNewZoomLevel = hash.substr(1);" & $CRLF
   s += "" & $CRLF
   s += "      if (iNewZoomLevel != iZoomLevel)" & $CRLF
   s += "      {" & $CRLF
   s += "         iZoomLevel = iNewZoomLevel;" & $CRLF
   s += "         oMap.SetZoomLevel(iNewZoomLevel);" & $CRLF
   s += "      }" & $CRLF
   s += "   }" & $CRLF
   s += "</script>" & $CRLF
   s += "<!-- Attaching the event handler to a new onhashchange event allows" & $CRLF
   s += "     the page to detect when the hash has changed and an AJAX" & $CRLF
   s += "     navigation has occurred. -->" & $CRLF
   s += "<body scroll=""auto"" onhashchange=""HashChangeHandler();"" onload=""GetMap();""style=""height: 100%"">" & $CRLF
   s += "" & $CRLF
   s += "<div id=""myMap"" style=""position: relative; width: 450px;" & $CRLF
   s += "   height: 450px; vertical-align: middle"">" & $CRLF
   s += "</div>" & $CRLF
   s += "" & $CRLF
   s += "</body>" & $CRLF
   s += "" & $CRLF
   s += "</html>" & $CRLF

   ' // Create the control
   hCtl = pWindow.AddWebBrowserControl(pWindow.hwnd, %IDC_WEBBROWSER, s, 5, 5, 582, 356, -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
' ========================================================================================