IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Web & réseau Delphi Discussion :

TWebBrowser n'affiche rien


Sujet :

Web & réseau Delphi

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Inscrit en
    Juin 2013
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 3
    Par défaut TWebBrowser n'affiche rien
    Bonjour,

    je débute en Delphi et je dois réaliser une appli qui comprend une carte Google Maps où je peux placer des marqueurs. J'ai donc copié le code de ce site qui répond parfaitement à mon besoin.

    Ce programme marche dans un projet à part, mais pas quand je le copie dans mon projet ! TWebBrowser n'affiche rien. Pour avoir un peu regardé, le programme ne rentre pas dans la procédure FormCreate, mais je ne vois pas pourquoi.

    J'ai pourtant recrée à l'identique les boutons, label, etc., le tout dans une page d'un PageControl où je souhaite afficher la carte.

    Si l'un de vous pouvait m'éclairer sur mes erreurs, je serais extrêmement reconnaissante.

    Je travaille sur la dernière version d'Embarcadero, avec Delphi XE4, sous Windows 7.

    Ci-dessous mon code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    unit Unit7;
     
    interface
     
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls, ComCtrls,MSHTML;
     
    type
      TForm7 = class(TForm)
        PageControl1: TPageControl;
        Situation: TTabSheet;
        Carte: TTabSheet;
        Tâches: TTabSheet;
        Equipe: TTabSheet;
        Chat: TTabSheet;
        Notes: TTabSheet;
        Indicateur: TShape;
        ColorBox1: TColorBox;
        Modifier: TButton;
        Panel1: TPanel;
        Panel2: TPanel;
        StaticText1: TStaticText;
        StaticText2: TStaticText;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        StaticText3: TStaticText;
        StaticText4: TStaticText;
        StaticText5: TStaticText;
        StaticText6: TStaticText;
        Panel3: TPanel;
        StaticText7: TStaticText;
        StaticText8: TStaticText;
        StaticText9: TStaticText;
        Edit3: TEdit;
        Edit4: TEdit;
        Edit5: TEdit;
        Edit6: TEdit;
        ComboBox1: TComboBox;
        RichEdit1: TRichEdit;
        Panel4: TPanel;
        RichEdit2: TRichEdit;
        Button5: TButton;
        Button6: TButton;
        WebBrowser1: TWebBrowser;
        ListView1: TListView;
        LabelLongitude: TLabel;
        Longitude: TEdit;
        LabelLatitude: TLabel;
        Latitude: TEdit;
        ButtonGotoLocation: TButton;
        ButtonClearMarkers: TButton;
        StaticText10: TStaticText;
        StaticText11: TStaticText;
        procedure clickModifier(Sender: TObject);
        procedure selectColor(Sender: TObject);
        procedure clickModifPopulation(Sender: TObject);
        procedure Button5Click(Sender: TObject);
        procedure Button6Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure ButtonClearMarkersClick(Sender: TObject);
        procedure WebBrowser1CommandStateChange(ASender: TObject; Command: Integer;  Enable: WordBool);
        procedure ButtonGotoLocationClick(Sender: TObject);
     
      private
          HTMLWindow2: IHTMLWindow2;
        procedure AddLatLngToList(const Lat,Lng:string);
        { Déclarations privées }
      public
        { Déclarations publiques }
      end;
     
    var
      Form7: TForm7;
     
    implementation
     
    uses Unit1, uMain, ActiveX;
    {$R *.dfm}
     
     
    procedure Search(const Adresse : string; const CP : string;
      const Ville : string);
    var URL : string;
    begin
      URL := 'http://maps.google.fr/maps?f=q&hl=fr&q=';
      URL := URL + Adresse + ' ,+ ' + CP + '+' + Ville;
     
    end;
     
    procedure TForm7.Button5Click(Sender: TObject);
    begin
       ShowMessage('Deconnexion réussie');
        form1.Show;
        form7.Visible:=false;
    end;
     
     
    procedure TForm7.Button6Click(Sender: TObject);
    begin
      FrmMain.Show;
    end;
     
    procedure TForm7.clickModifier(Sender: TObject);
    begin
       ColorBox1.Visible:=true;
       Edit2.Enabled:=true;
    end;
     
     
    procedure TForm7.clickModifPopulation(Sender: TObject);
    begin
       Edit1.Enabled:=true;
       Edit3.Enabled:=true;
       Edit4.Enabled:=true;
       Edit5.Enabled:=true;
       Edit6.Enabled:=true;
       ComboBox1.Enabled:=true;
    end;
     
    procedure TForm7.selectColor(Sender: TObject);
    begin
          Indicateur.Brush.Color :=ColorBox1.Selected;
    end;
     
    const
    HTMLStr: AnsiString =
    '<html> '+
    '<head> '+
    '<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> '+
    '<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&language=en"></script> '+
    //'<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> '+
    '<script type="text/javascript"> '+
    ''+
    ''+
    '  var geocoder; '+
    '  var map;  '+
    '  var markersArray = [];'+
    ''+
    ''+
    '  function initialize() { '+
    '    geocoder = new google.maps.Geocoder();'+
    '    var latlng = new google.maps.LatLng(40.714776,-74.019213); '+
    '    var myOptions = { '+
    '      zoom: 13, '+
    '      center: latlng, '+
    '      mapTypeId: google.maps.MapTypeId.ROADMAP '+
    '    }; '+
    '    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); '+
    '    map.set("streetViewControl", false);'+
    '    google.maps.event.addListener(map, "click", '+
    '         function(event) '+
    '                        {'+
    '                         document.getElementById("LatValue").value = event.latLng.lat(); '+
    '                         document.getElementById("LngValue").value = event.latLng.lng(); '+
    '                         PutMarker(document.getElementById("LatValue").value, document.getElementById("LngValue").value,"") '+
    '                        } '+
    '   ); '+
    ''+
    '  } '+
    ''+
    ''+
    '  function GotoLatLng(Lat, Lang) { '+
    '   var latlng = new google.maps.LatLng(Lat,Lang);'+
    '   map.setCenter(latlng);'+
    '  }'+
    ''+
    ''+
    'function ClearMarkers() {  '+
    '  if (markersArray) {        '+
    '    for (i in markersArray) {  '+
    '      markersArray[i].setMap(null); '+
    '    } '+
    '  } '+
    '}  '+
    ''+
    '  function PutMarker(Lat, Lang, Msg) { '+
    '   var latlng = new google.maps.LatLng(Lat,Lang);'+
    '   var marker = new google.maps.Marker({'+
    '      position: latlng, '+
    '      map: map,'+
    '      title: Msg+" ("+Lat+","+Lang+")"'+
    '  });'+
    '  markersArray.push(marker); '+
    '  index= (markersArray.length % 10);'+
    '  if (index==0) { index=10 } '+
    '  icon = "http://www.google.com/mapfiles/kml/paddle/"+index+"-lv.png"; '+
    '  marker.setIcon(icon); '+
    '  }'+
    ''+
    ''+
    ''+'</script> '+
    '</head> '+
    ''+
    '<body onload="initialize()"> '+
    '  <div id="map_canvas" style="width:100%; height:100%"></div> '+
    '  <div id="latlong"> '+
    '  <input type="hidden" id="LatValue" >'+
    '  <input type="hidden" id="LngValue" >'+
    '  </div>  '+
    ''+
    '</body> '+
    '</html> ';
     
     
    procedure TForm7.FormCreate(Sender: TObject);
    var
      aStream     : TMemoryStream;
    begin
       WebBrowser1.Navigate('about:blank');
        if Assigned(WebBrowser1.Document) then
        begin
          aStream := TMemoryStream.Create;
          try
             aStream.WriteBuffer(Pointer(HTMLStr)^, Length(HTMLStr));
             //aStream.Write(HTMLStr[1], Length(HTMLStr));
             aStream.Seek(0, soFromBeginning);
             (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(aStream));
          finally
             aStream.Free;
          end;
          HTMLWindow2 := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
        end;
    end;
     
     
    procedure TForm7.WebBrowser1CommandStateChange(ASender: TObject;  Command: Integer; Enable: WordBool);
    var
      ADocument : IHTMLDocument2;
      ABody     : IHTMLElement2;
      Lat : string;
      Lng : string;
     
          function GetIdValue(const Id : string):string;
          var
            Tag      : IHTMLElement;
            TagsList : IHTMLElementCollection;
            Index    : Integer;
          begin
            Result:='';
            TagsList := ABody.getElementsByTagName('input');
            for Index := 0 to TagsList.length-1 do
            begin
              Tag:=TagsList.item(Index, EmptyParam) As IHTMLElement;
              if CompareText(Tag.id,Id)=0 then
                Result := Tag.getAttribute('value', 0);
            end;
          end;
     
    begin
      if TOleEnum(Command) <> CSC_UPDATECOMMANDS then
        Exit;
     
      ADocument := WebBrowser1.Document as IHTMLDocument2;
      if not Assigned(ADocument) then
        Exit;
     
      if not Supports(ADocument.body, IHTMLElement2, ABody) then
        exit;
     
      Lat :=GetIdValue('LatValue');
      Lng :=GetIdValue('LngValue');
      if  (Lat<>'') and (Lng<>'') and ((Lat<>Latitude.Text) or (Lng<>Longitude.Text)) then
      begin
        Latitude.Text :=Lat;
        Longitude.Text:=Lng;
        AddLatLngToList(Lat, Lng);
      end;
    end;
     
    procedure TForm7.AddLatLngToList(const Lat, Lng: string);
    var
      Item  : TListItem;
    begin
       if (Lat<>'') and (Lng<>'') then
       begin
         Item:=ListView1.Items.Add;
         Item.Caption:=Lng;
         Item.SubItems.Add(Lat);
         Item.MakeVisible(False);
       end;
    end;
     
    procedure TForm7.ButtonClearMarkersClick(Sender: TObject);
    begin
      HTMLWindow2.execScript('ClearMarkers()', 'JavaScript');
      ListView1.Items.Clear;
    end;
     
    procedure TForm7.ButtonGotoLocationClick(Sender: TObject);
    begin
      if Assigned(ListView1.Selected) then
        HTMLWindow2.execScript(Format('GotoLatLng(%s,%s)',[ListView1.Selected.SubItems[0],ListView1.Selected.Caption]), 'JavaScript');
    end;
     
    end.

  2. #2
    Expert confirmé
    Avatar de Jipété
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    11 154
    Détails du profil
    Informations personnelles :
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 11 154
    Par défaut
    Yep !
    Citation Envoyé par tired_Panda Voir le message
    (...) J'ai donc copié le code (...)
    (...) Pour avoir un peu regardé, le programme ne rentre pas dans la procédure FormCreate, mais je ne vois pas pourquoi. (...)
    As-tu pensé à double-cliquer sur l'événement "FormCreate" dans l'inspecteur de propriétés, pour associer cet événement au code que tu as collé ?
    Et pareil pour les boutons.

    Si c'est pas ça, je donne ma langue au chat

  3. #3
    Candidat au Club
    Inscrit en
    Juin 2013
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 3
    Par défaut
    Euh, j'ai peur de ne pas comprendre de quoi vous voulez parler !

    Double-cliquer sur FormCreate, dans l'onglet "Structure" sur la gauche ? Mais ça ne fait rien ?

  4. #4
    Expert confirmé
    Avatar de Jipété
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    11 154
    Détails du profil
    Informations personnelles :
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 11 154
    Par défaut
    Et moi je n'ai qu'un vieux D7, ou un Lazarus assez récent, mais pas de XE4...

    Pour faire simple, y a qu'à double-cliquer sur une zone vide de la Form, et ça devrait ouvrir l'éditeur de code sur la procedure Form1.Create.
    De là 2 possibilités :
    • ou bien l'éditeur pointe sur la procédure qui a été copiée/collée, et je ne sais pas pourquoi ça ne fonctionne pas ;
    • ou bien l'éditeur a créé un squelette de procédure, vide, avec juste begin-end, et c'est ce que je suppose. Y aura plus qu'à supprimer cette entrée et recompiler, ça devrait aller car un lien manquant a été créé par le double-clic.

  5. #5
    Candidat au Club
    Inscrit en
    Juin 2013
    Messages
    3
    Détails du profil
    Informations forums :
    Inscription : Juin 2013
    Messages : 3
    Par défaut
    D'accord excusez moi !

    L'éditeur pointe bien sur la procédure qui a été collé, il n'y a pas de doublon vide.

    Edit : Je crois que le fait de double-cliquer a bien lié les objets, à présent ça fonctionne Ca n'était pas facile à voir comme soucis mais ça m'arrive avec d'autres objets que je renomme, il faut double-cliquer dessus pour que l'éditeur de code comprenne qu'il y a eu un changement...

    Merci beaucoup !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Boot mais n'affiche rien
    Par log2n dans le forum Assembleur
    Réponses: 28
    Dernier message: 03/11/2005, 21h16
  2. Mon programme n'affiche rien!!!!
    Par matthieu5978 dans le forum Langage
    Réponses: 1
    Dernier message: 03/11/2005, 11h28
  3. pourquoi mon quickreport n'affiche rien ?
    Par bertrand_declerck dans le forum Bases de données
    Réponses: 8
    Dernier message: 31/07/2005, 00h59
  4. [LG]Programme qui n'affiche rien
    Par ousunas dans le forum Langage
    Réponses: 4
    Dernier message: 17/02/2004, 19h38
  5. Opengl n'affiche rien.
    Par Fugus02 dans le forum OpenGL
    Réponses: 4
    Dernier message: 06/01/2004, 15h41

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo