Page 1 sur 1

Dialog XML en DataSection

Publié : jeu. 19/juil./2018 11:18
par falsam
J'ai lu quelques part sur ce forum qu'il n'était pas facile de mettre un fichier de dialogue XML dans la DataSection.

J'ai fait un petit test avec ce fichier que j'ai nommé ui0.xml

Code : Tout sélectionner

<?xml version="1.0" encoding="UTF-8"?>

<dialogs>
  <window name="window_0" flags="#PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered" width="800" height="600" text="My application" id="#mf"/>
</dialogs>
Ce fichier est saisie avec NotePad ++ au format UTF-8

Il ouvre une fenêtre de taille 800x600 centrée. Je n'ai pas ajouté d'autre gadget pour ne pas surcharger ce test.

■ Mise en DataSection.

Code : Tout sélectionner

DataSection
  XMLDialog:
  IncludeBinary "ui0.xml"
  XMLDialog_EOF:  
EndDataSection
■ Lecture du fichier dans une variable string.

Code : Tout sélectionner

XML.s = PeekS(?XMLDialog, ?XMLDialog_EOF - ?XMLDialog, #PB_UTF8)
La suite du code est classique est fait référence à la bibliothéque Dialog
:arrow: https://www.purebasic.com/french/docume ... index.html

■ Le code exemple.

Code : Tout sélectionner

#XML = 0
#Dialog = 0

Runtime Enumeration Window
  #mf  
EndEnumeration


;Plan de l'application
Declare Start()
Declare Exit()

Start()

Procedure Start()
  ;Lecture du fichier XML en DataSection. 
  ;Pour cette démo mon fichier XML (ui0.xml) est au format UTF-8
  Protected XML.s = PeekS(?XMLDialog, ?XMLDialog_EOF - ?XMLDialog, #PB_UTF8) ;ou #PB_ASCII si fichier au format ASCII 
  
  ;Chargement de l'interface de dialogue
  If CatchXML(#Xml, @XML, StringByteLength(XML), 0, #PB_UTF8) And XMLStatus(#Xml) = #PB_XML_Success
    
    ;Créer l'interface utilisateur
    CreateDialog(#Dialog)
    
    ;Ouvrir la fenetre nommée : "window_0" 
    If OpenXMLDialog(#Dialog, #Xml, "window_0")    
      
      ;Déclencheurs evenementiels
      BindEvent(#PB_Event_CloseWindow, @Exit())
      
      Repeat : WaitWindowEvent() : ForEver
    Else
      Debug "Dialog creation erreur: " + DialogError(#Dialog)
    EndIf
    
  Else
    Debug "Erreur lié à l'encodage du fichier UTF-8 avec BOM ou ASCII"
    Debug "XML erreur à la ligne" + XMLErrorLine(#Xml) + ": " + XMLError(#Xml)
  EndIf  
EndProcedure

Procedure Exit()  
  End
EndProcedure

DataSection
  XMLDialog:
  IncludeBinary "ui0.xml"
  XMLDialog_EOF:  
EndDataSection

Re: Dialog XML en DataSection

Publié : jeu. 19/juil./2018 12:58
par Kwai chang caine
Ca marche nickel merci 8)

Re: Dialog XML en DataSection

Publié : ven. 20/juil./2018 9:58
par microdevweb
Merci falsam pour cet exemple

Re: Dialog XML en DataSection

Publié : ven. 20/juil./2018 13:26
par Micoute
Merci falsam pour cet exemple, je l'ai modifié en y mettant mes propres constantes et ça fonctionne très bien.