Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Mappa Interattiva (VB.NET)

    Salve
    Devo fare una mappa interattiva.
    Sono riuscito a disegnare gli stati della mappa (con colore diverso),
    ma non so come fare a capire quando il mouse si trova su un determinato stato.
    In altre parole vorrei che scattasse un evento nel momento in cui il mouse si trova su uno stato.
    Come posso fare?
    Ecco un estratto del codice che utilizzo per disegnare gli stati:
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Drawing.Imaging
    Imports System.Drawing.Text
    ....
    Private Enum e_Colore
    Nero
    Verde
    Giallo
    Rosso
    Blu
    Viola
    End Enum
    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
    Dim gr As Graphics = e.Graphics

    DisegnaStato("Egitto", e_Colore.Viola, gr)
    gr.Dispose()
    End Sub

    Private Function DisegnaStato(ByVal Stato As String, ByVal Colore As e_Colore, ByRef gr As Graphics) As Bitmap
    Dim PathImmaginiStati As String = "C:\Immagini\"
    Dim R As Single, G As Single, B As Single

    Select Case Colore
    Case e_Colore.Viola
    R = 1
    G = 0.1
    B = 1
    Case e_Colore.Rosso
    R = 1
    B = 0.1
    G = 0.1
    Case e_Colore.Blu
    R = 0.1
    G = 0.1
    B = 1
    Case e_Colore.Verde
    R = 0.1
    G = 1
    B = 0.1
    Case e_Colore.Giallo
    R = 1
    G = 1
    B = 0.1
    Case e_Colore.Nero
    R = 0.25
    G = 0.25
    B = 0.25
    End Select

    Dim values()() As Single = {New Single() {R, 0, 0, 0, 0}, _
    New Single() {0, G, 0, 0, 0}, _
    New Single() {0, 0, B, 0, 0}, _
    New Single() {0, 0, 0, 1, 0}, _
    New Single() {0, 0, 0, 0, 1}}
    Dim colMatrix As New ColorMatrix(values)
    Dim imageAttr As New ImageAttributes()

    imageAttr.SetColorMatrix(colMatrix, ColorMatrixFlag.Default _
    , ColorAdjustType.Bitmap)
    Dim bmp As New Bitmap(PathImmaginiStati & Stato & ".gif")
    gr.DrawImage(bmp, New Rectangle(1, 1, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
    bmp.Dispose()

    End Function

  2. #2
    Utente di HTML.it L'avatar di yyzyyz
    Registrato dal
    Oct 2001
    Messaggi
    1,653
    :master: potresti mettere gli stati separati, cioe' , un'immagine x ogni picturebox / image, per poi controllare il focus di quel determinato oggetto(al cambio focus cambia text1.text=nomestato), oppure controllare un range di pixel , pero' mi sembra + complesso.

    ciao

  3. #3
    Grazie per la risposta
    Comunque mettere ogni stato in una picturebox non mi sembra la soluzione ottimale perchè la pb è rettangolare mentre gli stati non lo sono e non riuscirei a distinguere bene due stati confinanti.
    Ho provato ad utilizzare getpixel sull'oggetto bitmap e di confrontarlo con il colore trasparente.
    La cosa funziona anche se non sono sicurissimo che sia l'idea migliore. Inoltre ora sorge un altro problema:
    io disegno sul graphics che ottengo dall'evento Paint del Form, ma il mio obiettivo e'
    quello di disegnare uno stato con maggiore luminosità quando si trova sotto il mouse.
    Per far questo pensavo di utilizzare l'evento mousemove ottenendo cosi' le coordinate del mouse x,y da passare a getixel.
    Solo che quando scatta
    l'evento mousemove non ho un riferimento al graphics che mi serve per disegnare nuovamente lo stato.
    Mi rendo conto di essere stato un po' confuso nell'esposizione del problema, ma spero che abbiate capito comunque.

  4. #4
    Alla fine ho risolto, ma il codice risultante ò veramente lentissimo.
    eccolo:

    codice:
    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Drawing.Imaging
    Imports System.Drawing.Text
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private Enum e_Colore
            Nero
            Verde
            Giallo
            Rosso
            Blu
            Viola
        End Enum
    
        Private Terr As New Territori()
        Private X As Integer, Y As Integer
    
    
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            X = e.X
            Y = e.Y
        End Sub
    
        Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
            Terr.MouseOn(X, Y)
            Me.Refresh()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Terr.AggiungiTerritorio("Venezuela", e_Colore.Viola)
            Terr.AggiungiTerritorio("Brasile", e_Colore.Giallo)
            Terr.AggiungiTerritorio("Peru", e_Colore.Blu)
            Terr.AggiungiTerritorio("Argentina", e_Colore.Rosso)
            Terr.AggiungiTerritorio("AfricaDelNord", e_Colore.Verde)
            Terr.AggiungiTerritorio("Egitto", e_Colore.Viola)
            Terr.AggiungiTerritorio("Congo", e_Colore.Verde)
            Terr.AggiungiTerritorio("AfricaDelSud", e_Colore.Nero)
            Terr.AggiungiTerritorio("Madagascar", e_Colore.Rosso)
            Terr.AggiungiTerritorio("AfricaOrientale", e_Colore.Rosso)
            Terr.AggiungiTerritorio("EuropaMeridionale", e_Colore.Rosso)
            Terr.AggiungiTerritorio("MedioOriente", e_Colore.Giallo)
            Terr.AggiungiTerritorio("GranBretagna", e_Colore.Giallo)
            Terr.AggiungiTerritorio("EuropaSettentrionale", e_Colore.Blu)
            Terr.AggiungiTerritorio("EuropaOccidentale", e_Colore.Blu)
            Terr.AggiungiTerritorio("Scandinavia", e_Colore.Rosso)
            Terr.AggiungiTerritorio("Ucraina", e_Colore.Blu)
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
            Dim gr As Graphics = e.Graphics
            Dim bmp As New Bitmap("C:\Documents and Settings\x\Desktop\RISIKO\Immagini\Mare.gif")
    
            gr.SmoothingMode = SmoothingMode.AntiAlias
            gr.DrawImage(bmp, 1, 1)
            bmp.Dispose()
    
    
            Terr.gr = gr
            Terr.Refresh("Venezuela")
            Terr.Refresh("Brasile")
            Terr.Refresh("Peru")
            Terr.Refresh("Argentina")
            Terr.Refresh("AfricaDelNord")
            Terr.Refresh("Egitto")
            Terr.Refresh("Congo")
            Terr.Refresh("AfricaDelSud")
            Terr.Refresh("Madagascar")
            Terr.Refresh("AfricaOrientale")
            Terr.Refresh("EuropaMeridionale")
            Terr.Refresh("MedioOriente")
            Terr.Refresh("GranBretagna")
            Terr.Refresh("EuropaSettentrionale")
            Terr.Refresh("EuropaOccidentale")
            Terr.Refresh("Scandinavia")
            Terr.Refresh("Ucraina")
    
            gr.Dispose()
        End Sub
    
    
        Private Class Territori
            Private CollTerritori As New TerritoriCollection()
    
            Public Sub AggiungiTerritorio(ByVal Nome As String, ByVal Colore As e_Colore)
                CollTerritori.Add(Nome, New Territorio(Nome, Colore))
            End Sub
    
            Public Sub Refresh(ByVal Nome As String)
                CollTerritori(Nome).Refresh()
            End Sub
    
            Public WriteOnly Property gr() As Graphics
                Set(ByVal Value As Graphics)
                    Dim Item As DictionaryEntry
    
                    For Each Item In CollTerritori
                        CType(Item.Value, Territorio).gr = Value
                    Next Item
    
                End Set
            End Property
    
            Public Sub MouseOn(ByVal X As Integer, ByVal Y As Integer)
                Dim Item As DictionaryEntry
                Dim terr As Territorio
    
                For Each Item In CollTerritori
                    terr = CType(Item.Value, Territorio)
                    If terr.IsIn(X, Y) Then
                        terr.Seleziona()
                    Else
                        terr.Deseleziona()
                    End If
                Next Item
    
    
            End Sub
    
            Private Class TerritoriCollection
                Inherits System.Collections.DictionaryBase
    
                Default Property Item(ByVal Key As String) As Territorio
                    Get
                        Return CType(dictionary.Item(Key), Territorio)
                    End Get
                    Set(ByVal Value As Territorio)
                        dictionary.Item(Key) = Value
                    End Set
                End Property
    
                Public Sub Add(ByVal Key As String, ByVal Value As Territorio)
                    dictionary.Add(Key, Value)
                End Sub
    
            End Class
    
            Private Class Territorio
                Private bmp As Bitmap
                Private Nome As String, Colore As e_Colore
                Public gr As Graphics
                Private transparency As Single = 0.5
    
                Sub New(ByVal Nome As String, ByVal Colore As e_Colore)
                    Const PathImmaginiStati As String = "C:\Documents and Settings\x\Desktop\RISIKO\Immagini\"
    
                    Me.Nome = Nome
                    Me.Colore = Colore
                    bmp = Bitmap.FromFile(PathImmaginiStati & Nome & ".gif")
                End Sub
    
                Public Sub Refresh()
    
                    Dim R As Single, G As Single, B As Single
    
                    Select Case Colore
                        Case e_Colore.Viola
                            R = 1
                            G = 0.1
                            B = 1
                        Case e_Colore.Rosso
                            R = 1
                            B = 0.1
                            G = 0.1
                        Case e_Colore.Blu
                            R = 0.1
                            G = 0.1
                            B = 1
                        Case e_Colore.Verde
                            R = 0.1
                            G = 1
                            B = 0.1
                        Case e_Colore.Giallo
                            R = 1
                            G = 1
                            B = 0.1
                        Case e_Colore.Nero
                            R = 0.25
                            G = 0.25
                            B = 0.25
                    End Select
    
                    Dim values()() As Single = {New Single() {R, 0, 0, 0, 0}, _
                                                New Single() {0, G, 0, 0, 0}, _
                                                New Single() {0, 0, B, 0, 0}, _
                                                New Single() {0, 0, 0, transparency, 0}, _
                                                New Single() {0, 0, 0, 0, 2}}
                    Dim colMatrix As New ColorMatrix(values)
                    Dim imageAttr As New ImageAttributes()
    
                    imageAttr.SetColorMatrix(colMatrix, ColorMatrixFlag.Default _
                        , ColorAdjustType.Bitmap)
                    gr.DrawImage(bmp, New Rectangle(1, 1, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
                End Sub
    
                Public Function IsIn(ByVal X As Integer, ByVal Y As Integer) As Boolean
                    Try
                        IsIn = (bmp.GetPixel(X, Y).ToArgb <> Color.Transparent.ToArgb)
                    Catch
                        IsIn = False
                    End Try
                End Function
    
                Protected Overrides Sub Finalize()
                    Dim ex As Exception
                    Try
                        bmp.Dispose()
                    Catch ex
                    Finally
                        MyBase.Finalize()
                    End Try
                End Sub
    
                Public Sub Seleziona()
                    transparency = 1
                End Sub
                Public Sub Deseleziona()
                    transparency = 0.5
                End Sub
            End Class
        End Class
    
    
    End Class

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.