Gambas: Verschiedene kurze Beispielprogramme

Aus Wikibooks


Zurück zum Gambas-Inhaltsverzeichnis.


Konverter (Text zu HTML-Code)[Bearbeiten]

Dieses Programm verwandelt Text in Html-Zeichencode.

Sie Benötigen dazu:

  • 1 Form
  • 1 Button
  • 1 Textlabel
  • 2 Textboxen
Ergebnis AS String

PUBLIC SUB Button1_Click()
  i AS Integer
  Ergebnis = ""

  'Umwandlung vom "utf-8" Zeichensatz zum "ISO-8859-1" Zeichensatz
  Textbox2.Text = Conv$(Textbox1.Text, "utf-8", "ISO-8859-1")
  
  FOR i = 1 TO Len(Textbox2.Text)
  '"Len" gibt die Länge des übergebenen Strings zurück

    Ergebnis = Ergebnis & Chr$(38) & "#" & Asc( Textbox2.Text, i ) & ";"
    '"Asc" gibt den ASCII-Code des Zeichen an der angegebenen Stelle des Stings zurück
    'Chr$(38) ergibt das & Zeichen, hier nur verwendet um mögliche
    'Darstellungsprobleme im Browser zu vermeiden
  NEXT
  
  Textbox2.Text = Ergebnis
END

PUBLIC SUB TextBox1_Enter()
  Button1_Click
END

So sieht es dann aus:

Text zu HTML-Code

Youtube Downloader[Bearbeiten]

Ermöglicht das Runterladen von Youtubevideos anhand eines Youtubelinks. Die Komponenten gb.net,gb.net.curl,gb.desktop,gb.gui,gb.form sind notwendig.

Hier die Form

# Gambas Form File 2.0

{ Form Form
  MoveScaled(0,0,38.5714,8.5714)
  Text = ("YoutubeDownloader")
  Border = Window.Fixed
  { TextBox1 TextBox
    MoveScaled(1,1,21,3)
    ToolTip = ("Here goes the link")
    Text = ("")
  }
  { PictureBox1 PictureBox
    MoveScaled(23,1,14.1429,7.1429)
  }
  { Button1 Button
    MoveScaled(1,5,21,3)
    ToolTip = ("click here if you are ready to download")
    Text = ("Download")
  }
}

Hier der Quellcode

' Gambas class file
CONST PICTURENAME AS String = "youtube.jpg"
CONST YOUTUBE AS String = "http://www.youtube.com"

PUBLIC SUB Form_Open()
  DIM pic AS Picture
  
  pic = Picture.Load(Application.Path & "/" & PICTURENAME)
  pic.Resize(100, 50)
  PictureBox1.Picture = pic
  Application.MainWindow.Icon = pic
END

PUBLIC SUB Button1_Click()
  getYoutubeFile(TextBox1.Text)
END

PUBLIC SUB PictureBox1_MouseDown()
  Desktop.Open(YOUTUBE)
END

' Öffnet den Standardbrowser mit dem Video dessen 
' Youtubeurl als Parameter übergeben wird
PUBLIC SUB getYoutubeFile(url AS String)  
  DIM contents AS String
  
  url = parseYoutube(url)
  IF url = NULL THEN 
    Message.Error("Error: problems with parsing the page!")
    RETURN 
  END IF 

  Desktop.Open(url)
END

' parsed eine Youtubeseite um den tatsächlichen Lageort des Videos zu finden
PUBLIC FUNCTION parseYoutube(url AS String) AS String
  DIM contents AS String
  DIM tmp AS String
  DIM tmp2 AS String
  DIM tval AS String
  DIM id AS String 

  ' seite laden
  contents = getFile(url)
  IF contents = NULL THEN 
    RETURN NULL
  END IF 

  ' tval
  tmp = String.InStr(contents, "\"t\": \"", 0) + String.Len("\"t\": \"")
  tmp2 = String.InStr(contents, "\", ", tmp)
  tval = String.Mid(contents, tmp, tmp2 - tmp)
    
  ' id
  tmp = String.InStr(url, "v=", 0) + String.Len("v=")
  id = String.Mid(url, tmp)
    
  RETURN "http://www.youtube.com/get_video?video_id=" & id & "&t=" & tval 
END

' Lädt eine Seite runter und gibt diese als String zurück
PUBLIC FUNCTION getFile(url AS String) AS String
  DIM h AS HttpClient
  DIM buffer AS String
  
  buffer = NULL
  h = NEW HttpClient AS "h"
  h.URL = url
  h.Get() 
  
  DO WHILE h.Status <> 0
    WAIT 0.01
  LOOP

  IF h.Status >= 0 THEN
    IF Lof(h) THEN 
      READ #h, buffer, Lof(h)
    END IF 
  END IF

  RETURN buffer
END