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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incorporate or insert the Italy map with the mouse move event in a form?

Status
Not open for further replies.
So ... you want the same functionality as the webpage - an exact copy of the map, and the ability to respond to clicks in any of the regions on said map?
 
So a few hints

It isn't a single map. It is 20 SVG polygons stitched together, and then each is treated as a single, clickable region.

VB doesn't do SVG. And it doesn't handle irregular polygon regions

So step 1 would be to extract the SVG drawing paths from the page for each of the regions
Step 2 would be to figure out how to draw those regions paths in VB
Step 3 would be figuring out how to reliably detect if the mouse in in a specific irregular region when moving or clicked

My code in might provide a hint on one way of achieving some of this ...
 
Actually, you have even more than just 20 polygons. Some regions (Toscana, Sardegna, Sicilia, etc.) have small islands, so those regions are represented by collection of polygons.

---- Andy

"Hmm...they have the internet on computers now"--Homer Simpson
 
Yep, that's why I said SVG polygons rather than just polygons. SVG has the concept of multiple polygonal regions making up a bigger polygon. Microsoft has a similar concept, called PolyPolygons (this is a clue ...)
 
tks strongm, but with my knoledgment, i finish this project arround the year 2024.

in other case tk alwsys for tips.
 
wow bro!!!!
is this code, for me?
if yes, in this case, i finish my project arround december 2023!
 
Not really, no, as

1) given my current understanding of your coding proficiency, it'll be likely too difficult for you to implement
2) It assumes you know how to extract the SVG paths from the web page and stick them in an XML file.
3) it is unsupportable ...

it was really just an intellectual exercise to prove to myself it could actually be done.

I can stick the code up if you really, really insist, but it is very much non-production code

Code:
[COLOR=blue][COLOR=green]' Requires that Microsoft Scripting Runtime library is added as Reference
' And Microsoft XML library
[/color]
Option Explicit


Private RegionDictionary As New Dictionary 'Requires that Microsoft Scripting Runtime library is added as Reference

Private Declare Function FrameRgn Lib "gdi32" (ByVal hdc As Long, ByVal hrgn As Long, ByVal hbrush As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
[COLOR=green]'Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
'Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long[/color]
Private Declare Function PtInRegion Lib "gdi32.dll" (ByVal hrgn As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function InvertRgn Lib "gdi32" (ByVal hdc As Long, ByVal hrgn As Long) As Long
Private Declare Function InvalidateRgn Lib "user32" (ByVal hwnd As Long, ByVal hrgn As Long, ByVal bErase As Long) As Long
Private Declare Function PaintRgn Lib "gdi32" (ByVal hdc As Long, ByVal hrgn As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function CreatePolyPolygonRgn Lib "gdi32" (lpPoint As POINTAPI, lpPolyCounts As Long, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hrgn As Long, ByVal hbrush As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long

Private Const WINDING = 2
Private Const ALTERNATE = 1

Private Type POINTAPI
        X As Long
        Y As Long
End Type

Private Sub Command1_Click()
    Dim mydoc As New DOMDocument
    Dim lp As Long
    Dim fred As IXMLDOMSelection
    Dim plotstring As String
    
    [COLOR=green]' This is all very bespoke[/color]
       
    mydoc.Load "f:\italymapsvg2.txt" [COLOR=green]' SVG paths in an xml file[/color]
    Set fred = mydoc.getElementsByTagName("a/path") [COLOR=green]' get all the paths[/color]

    [COLOR=green]' clean the path[/color]
    For lp = 0 To fred.length - 1
        plotstring = fred(lp).Attributes(1).Text
        plotstring = Left(plotstring, Len(plotstring) - 2)
        plotstring = Right(plotstring, Len(plotstring) - 2)
        polyplot plotstring [COLOR=green]' turn into a region, add to dictionary, and plot filled and outlined[/color]
    Next
End Sub

Private Sub Form_Load()
    Form1.ScaleMode = vbPixels
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim lp As Long
    Dim hbrush As Long
    Static prevhighlight As Long
    
    [COLOR=green]' loop through all regions to see if we get a hit[/color]
    For lp = 0 To RegionDictionary.Count - 1
        If PtInRegion(RegionDictionary.Items(lp), X, Y) Then
            paintaregion prevhighlight, RGB(127 + 42 * (lp Mod 3 + 1), 127, 127)
            paintaregion RegionDictionary.Items(lp), RGB(192, 64, 64)
            Text1.Text = RegionDictionary.Keys(lp)
            prevhighlight = RegionDictionary.Items(lp)
        End If
    Next
End Sub

[COLOR=green]' Cleanup at form unload[/color]
Private Sub Form_Unload(Cancel As Integer)
    Dim lp As Long

    [COLOR=green]' loop through all regions and delete GDI objects[/color]
    For lp = 0 To RegionDictionary.Count - 1
        DeleteObject RegionDictionary.Items(lp)
    Next
End Sub

Private Sub polyplot(strplotstring As String)
    Dim strLine As String
    Dim StrLineElements
    Dim lp As Long
    Dim lastx, lasty
    Dim polyPoints() As POINTAPI
    Dim numvertices() As Long
    Dim polycount As Long
    Dim vertices As Long
    Dim myrgn As Long
    
    Static regioncount As Long

    StrLineElements = Split(strplotstring, " ")
       
    For lp = 0 To UBound(StrLineElements) Step 2
        ReDim Preserve polyPoints(lp \ 2) As POINTAPI
        
        If StrLineElements(lp) = "Z" Then [COLOR=green]' new polygon to add to our polypolygon[/color]
            ReDim Preserve numvertices(polycount) As Long
            numvertices(polycount) = vertices + 1
            vertices = 0
            polycount = polycount + 1
        End If
        
        If StrLineElements(lp) <> "Z" Then
            polyPoints(lp \ 2).X = StrLineElements(lp)
            polyPoints(lp \ 2).Y = StrLineElements(lp + 1)
            vertices = vertices + 1
         End If
         
    Next
    
    ReDim Preserve numvertices(polycount) As Long
    
    numvertices(polycount) = vertices
    myrgn = CreatePolyPolygonRgn(polyPoints(0), numvertices(0), polycount + 1, 1)
    RegionDictionary.Add "Region " & regioncount, myrgn
    regioncount = regioncount + 1
    paintaregion myrgn, RGB(127 + 42 * (regioncount Mod 3 + 1), 127, 127)

End Sub

Public Sub paintaregion(hrgn As Variant, Optional fillcolour As Long = &H7F7FFF, Optional framecolour As Long = &HC8C8C8)
    Dim hbrush As Long
    
    hbrush = CreateSolidBrush(fillcolour)
    FillRgn Form1.hdc, hrgn, hbrush
    DeleteObject hbrush [COLOR=green]' make sure we clean up GDI objects we are no longer using[/color]
    
    hbrush = CreateSolidBrush(framecolour)
    FrameRgn Form1.hdc, hrgn, hbrush, 1, 1
    DeleteObject hbrush [COLOR=green]' make sure we clean up GDI objects we are no longer using[/color]
    
End Sub[/color]
 
It assumes you know how to extract the SVG paths from the web page and stick them in an XML file.

Dont know!
 
now with a crhoem estenction have this path:
file:///C:/Users/Admin/Dropbox/Il%20mio%20PC%20(DESKTOP-TQPVR2M)/Downloads/svgexport-1.svg
 
OK
i just have download the .svg image and saved in c:\test\svgexport-1.svg
but in wich part of the form i need to insert the svg imagemap?
 
Told you this code wasn't really suitable for you.

There is no image map to insert. The program draws the polygons onto the firm using the SVG data. I even commented the line that loads that data. Your form needs a command button and a text box
 
ok..

but in:
myrgn = CreatePolyPolygonRgn(polyPoints(0), numvertices(0), polycount + 1, 1)

takes a long time and crashes
 
As I said, it is NOT production code, it is simply a proof of concept, it works absolutely fine here, and I'm not going to support or debug it for you. Sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top