加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

VB Google Translation

发布时间:2020-12-17 07:52:54 所属栏目:百科 来源:网络整理
导读:REM Form1.vbImports System.IOImports System.NetImports System.TextPublic Class Form1 Private spVoice As SpVoice Private Function GoogleTranslate(ByVal [Text] As String) As String Try Dim client As New WebClient client.Headers.Item("Referer
REM Form1.vb

Imports System.IO
Imports System.Net
Imports System.Text

Public Class Form1

    Private spVoice As SpVoice

    Private Function GoogleTranslate(ByVal [Text] As String) As String
        Try
            Dim client As New WebClient
            client.Headers.Item("Referer") = "http://translate.google.cn/"
            client.Headers.Item("User-Agent") = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 663; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)"
            Dim bytes As Byte() = client.DownloadData(("http://translate.google.cn/translate_a/t?client=t&text=" & Uri.EscapeDataString([Text]) & "&hl=zh-CN&multires=1&otf=1&pc=0&sc=1" + cype.AutoCompleteCustomSource(cype.SelectedIndex)))
            client.Dispose()
            Dim ret As String = Encoding.UTF8.GetString(bytes)
            Return ret
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Sub translate_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles translate.Click
        If cype.SelectedIndex > -1 Then
            Dim str = MyClass.GoogleTranslate(request.Text)
            If str <> Nothing Then
                result.Text = str.Substring((str.IndexOf("[[""") + 3),(str.IndexOf(""",""") - 4)).Replace("rn",vbNewLine) & vbNewLine
            End If
        Else
            result.Text = request.Text
        End If
    End Sub

    Private Sub deacon_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles deacon.Click
        If spVoice Is Nothing Then
            spVoice = New SpVoice()
            spVoice.Rate = -2
            spVoice.Volume = 100
            spVoice.SetVoices("Microsoft Sam")
        End If
        spVoice.Speak(result.Text,spVoice.Flags.SVSFlagsAsync)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load
        cype.SelectedIndex = 0
    End Sub
End Class

REM SpVoice.vb

Imports System.Reflection
Imports System.Collections.Generic
Imports System.Runtime.InteropServices

<Guid("ED5DCE10-F40B-4655-9E7B-903779C77AF2")> _
Public Class SpVoice
    ' Methods
    Public Sub New()
        Me.Init()
    End Sub

    Public Sub DisplayUI(ByVal hWndParent As Integer,ByVal Title As String,ByVal TypeOfUI As String,ByVal ExtraData As Object)
        Me._ComType.InvokeMember("DisplayUI",BindingFlags.InvokeMethod,Nothing,Me._ComObject,New Object() {hWndParent,Title,TypeOfUI,ExtraData})
    End Sub

    Public Function GetVoices() As List(Of String)
        Dim list As List(Of String) = Nothing
        Dim target As Object = Nothing
        Dim obj3 As Object = Me._ComType.InvokeMember("GetVoices",Nothing)
        Dim num As Integer = CInt(obj3.GetType.InvokeMember("Count",BindingFlags.GetProperty,obj3,Nothing))
        If (num <> 0) Then
            list = New List(Of String)
        End If
        Dim i As Integer
        For i = 0 To num - 1
            target = obj3.GetType.InvokeMember("Item",New Object() {i})
            If (Not target Is Nothing) Then
                list.Add(target.GetType.InvokeMember("GetDescription",target,Nothing).ToString)
            End If
        Next i
        Return list
    End Function

    Public Function Init() As Boolean
        Me._ComType = Type.GetTypeFromProgID("SAPI.SpVoice",True)
        If (Me._ComType Is Nothing) Then
            Throw New Exception("COM调用失败")
        End If
        Me._ComObject = Activator.CreateInstance(Me._ComType,True)
        Return (Not Me._ComObject Is Nothing)
    End Function

    Public Function IsUISupported(ByVal TypeOfUI As String,ByVal ExtraData As Object) As Boolean
        Return CBool(Me._ComType.InvokeMember("IsUISupported",New Object() {TypeOfUI,ExtraData}))
    End Function

    Public Sub Pause()
        Me._ComType.InvokeMember("Pause",Nothing)
    End Sub

    Public Function Release() As Integer
        Return Marshal.ReleaseComObject(Me._ComObject)
    End Function

    Public Sub [Resume]()
        Me._ComType.InvokeMember("Resume",Nothing)
    End Sub

    Public Function SetVoices(ByVal Name As String) As Boolean
        Dim target As Object = Nothing
        Dim obj3 As Object = Me._ComType.InvokeMember("GetVoices",Nothing))
        Dim i As Integer
        For i = 0 To num - 1
            target = obj3.GetType.InvokeMember("Item",New Object() {i})
            If ((Not target Is Nothing) AndAlso (target.GetType.InvokeMember("GetDescription",Nothing).ToString = Name)) Then
                Me._ComType.InvokeMember("Voice",BindingFlags.SetProperty,New Object() {target})
            End If
        Next i
        Return False
    End Function

    Public Function Skip(ByVal Type As Integer,ByVal NumItems As Integer) As Integer
        Return CInt(Me._ComType.InvokeMember("Skip",New Object() {Type,NumItems}))
    End Function

    Public Sub Speak(ByVal [Text] As String,ByVal Flags As Flags)
        Me._ComType.InvokeMember("Speak",New Object() {[Text],Flags})
    End Sub

    Public Function WaitUntilDone(ByVal msTimeout As Integer) As Boolean
        Return CBool(Me._ComType.InvokeMember("WaitUntilDone",New Object() {msTimeout}))
    End Function


    ' Properties
    Public Property [Property] As Priority
        Get
            Return DirectCast(Me._ComType.InvokeMember("Priority",Nothing),Priority)
        End Get
        Set(ByVal value As Priority)
            Me._ComType.InvokeMember("Priority",Me._ComType,New Object() {value})
        End Set
    End Property

    Public Property Rate As Integer
        Get
            Return CInt(Me._ComType.InvokeMember("Rate",Nothing))
        End Get
        Set(ByVal value As Integer)
            Me._ComType.InvokeMember("Rate",New Object() {value})
        End Set
    End Property

    Public Property Volume As Integer
        Get
            Return CInt(Me._ComType.InvokeMember("Volume",Nothing))
        End Get
        Set(ByVal value As Integer)
            Me._ComType.InvokeMember("Volume",New Object() {value})
        End Set
    End Property


    ' Fields
    Private _ComObject As Object = Nothing
    Private _ComType As Type = Nothing

    ' Nested Types
    Public Enum Flags
        ' Fields
        SVSFDefault = 0
        SVSFIsFilename = 4
        SVSFIsNotXML = &H10
        SVSFIsXML = 8
        SVSFlagsAsync = 1
        SVSFNLPMask = &H40
        SVSFNLPSpeakPunc = &H40
        SVSFParseAutodetect = 0
        SVSFParseMask = &H180
        SVSFParseSapi = &H80
        SVSFParseSsml = &H100
        SVSFPersistXML = &H20
        SVSFPurgeBeforeSpeak = 2
        SVSFUnusedFlags = -512
        SVSFVoiceMask = &H1FF
    End Enum

    Public Enum Priority
        ' Fields
        SVPAlert = 1
        SVPNormal = 0
        SVPOver = 2
    End Enum
End Class


REM Form1.Designer.vb

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form 重写 Dispose,以清理组件列表。
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改它。
    '不要使用代码编辑器修改它。
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.request = New System.Windows.Forms.TextBox()
        Me.result = New System.Windows.Forms.TextBox()
        Me.translate = New System.Windows.Forms.Button()
        Me.cype = New System.Windows.Forms.ComboBox()
        Me.deacon = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'request
        '
        Me.request.Location = New System.Drawing.Point(12,28)
        Me.request.Multiline = True
        Me.request.Name = "request"
        Me.request.Size = New System.Drawing.Size(221,176)
        Me.request.TabIndex = 0
        '
        'result
        '
        Me.result.Location = New System.Drawing.Point(239,28)
        Me.result.Multiline = True
        Me.result.Name = "result"
        Me.result.Size = New System.Drawing.Size(248,176)
        Me.result.TabIndex = 1
        '
        'translate
        '
        Me.translate.Location = New System.Drawing.Point(331,223)
        Me.translate.Name = "translate"
        Me.translate.Size = New System.Drawing.Size(75,22)
        Me.translate.TabIndex = 2
        Me.translate.Text = "翻译(&T)"
        Me.translate.UseVisualStyleBackColor = True
        '
        'cype
        '
        Me.cype.AutoCompleteCustomSource.AddRange(New String() {"&sl=auto&tl=zh-CN","&sl=en&tl=zh-CN","&sl=zh-CN&tl=en","&sl=zh-CN&tl=ja","&sl=ja&tl=zh-CN"})
        Me.cype.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
        Me.cype.FormattingEnabled = True
        Me.cype.Items.AddRange(New Object() {"自动","英中","中英","中日","日中"})
        Me.cype.Location = New System.Drawing.Point(173,224)
        Me.cype.Name = "cype"
        Me.cype.Size = New System.Drawing.Size(152,20)
        Me.cype.TabIndex = 3
        '
        'deacon
        '
        Me.deacon.Location = New System.Drawing.Point(413,223)
        Me.deacon.Name = "deacon"
        Me.deacon.Size = New System.Drawing.Size(75,22)
        Me.deacon.TabIndex = 4
        Me.deacon.Text = "朗读(&D)"
        Me.deacon.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!,12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(499,261)
        Me.Controls.Add(Me.deacon)
        Me.Controls.Add(Me.cype)
        Me.Controls.Add(Me.translate)
        Me.Controls.Add(Me.result)
        Me.Controls.Add(Me.request)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Translation"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents request As System.Windows.Forms.TextBox
    Friend WithEvents result As System.Windows.Forms.TextBox
    Friend WithEvents translate As System.Windows.Forms.Button
    Friend WithEvents cype As System.Windows.Forms.ComboBox
    Friend WithEvents deacon As System.Windows.Forms.Button

End Class

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读