⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.vb

📁 一个界面很好的iRadio收音机 一个界面很好的iRadio收音机
💻 VB
📖 第 1 页 / 共 5 页
字号:
Option Explicit On
Option Strict On
Option Compare Text

Imports System.Xml
Imports System.Xml.XPath
Imports System.Runtime.InteropServices
Imports System.IO
Imports System.Text
Imports WMPLib

''' <summary>
''' The basis of this application is rather simple. It uses the
''' Microsoft Windows Media Player ActiveX control to play streaming
''' audio and video from the Internet. I refer you to the Windows Media
''' Player 10 SDK for details of the methods and events.
''' 
''' Where the application becomes more complex is in it's ability to
''' store and display lists of Internet media streams. These lists are
''' fully editable and extensible by the end user. The application uses
''' XML files to store and manage these lists.
''' 
''' It is also hoped that the application is not considered to look
''' ordinary. All forms and dialogs are borderless and have custom colors
''' applied as png gradients. All buttons are also customized with these
''' same gradients and also have custom mouse events.
''' </summary>
''' 
''' <copyright>Copyright ?2006 Herbert N Swearengen III</copyright>
''' 
''' <notice>
''' This application maybe freely distributed and modified as long
''' as the copyright notice and EULA are retained. This applies to
''' both the compiled application and it's source code.
''' </notice>
Public Class MainForm
    Inherits System.Windows.Forms.Form

#Region " New "

    Public Sub New()

        Try
            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            ' Read settings.
            My.Settings.Reload()

            ' Set settings.
            Settings.AutoPlay = My.Settings.AutoPlay
            Settings.Color = My.Settings.Color
            Settings.DisplayCoverArt = My.Settings.DisplayCoverArt
            Settings.DisplayVisualizations = My.Settings.DisplayVisualizations
            Settings.DisplaySplashScreen = My.Settings.DisplaySplashScreen
            Settings.KeepOnTop = My.Settings.KeepOnTop
            Settings.LastButtonSelected = My.Settings.LastButtonSelected
            Settings.RotateStationInfo = My.Settings.RotateStationInfo
            Settings.SelectedFolder = My.Settings.SelectedFolder
            Settings.StartAsIcon = My.Settings.StartAsIcon
            Settings.StartWithWindows = My.Settings.StartWithWindows
            Settings.StationList = My.Settings.StationList
            Settings.ToolTips = My.Settings.ToolTips
            Settings.Volume = My.Settings.Volume
            Settings.XPosition = My.Settings.XPosition
            Settings.YPosition = My.Settings.YPosition

        Catch ex As ApplicationException
            ' If error occurs, cancel topmost.
            If Me.TopMost = True Then Me.TopMost = False

            MessageBox.Show("An unhandled error has occurred in " & My.Application.Info.Title & vbCrLf & _
                            "The system returned the following information:" & vbCrLf & ex.Message, _
                            My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch exc As Configuration.SettingsPropertyWrongTypeException
            ' If error occurs, cancel topmost.
            If Me.TopMost = True Then Me.TopMost = False

            MessageBox.Show("An unhandled error has occurred in " & My.Application.Info.Title & vbCrLf & _
                            "The system returned the following information:" & vbCrLf & exc.Message, _
                            My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch excep As AccessViolationException
            ' If error occurs, cancel topmost.
            If Me.TopMost = True Then Me.TopMost = False

            MessageBox.Show("An unhandled error has occurred in " & My.Application.Info.Title & vbCrLf & _
                            "The system returned the following information:" & vbCrLf & excep.Message, _
                            My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

#End Region

#Region " Form-Level Variables "

    ''' <summary>
    ''' These variables hold information for the selected station.
    ''' </summary>
    Private mSelectedName As String
    Private mSelectedLocation As String
    Private mSelectedFormat As String
    Private mSelectedWebsite As String
    Private mSelectedUri As String

    ''' <summary>
    ''' Used by Player_MediaError to allow up to 5 retries for common network errors
    ''' before displaying an error message.
    ''' </summary>
    Private mConnectionAttempts As Integer

    ''' <summary>
    ''' The current station button that the mouse is over.
    ''' Used by ClearStation and set by the MouseEnter
    ''' events of the station radiobuttons and
    ''' cleared by ClearStation.
    ''' </summary>
    Private mCurrentStationButton As String

    ''' <summary>
    ''' Indicates if media player is loaded so errors do not occur when closing or disposing the object.
    ''' </summary>
    Private mPlayerLoaded As Boolean

    ''' <summary>
    ''' Used to toggle ListView sort direction.
    ''' </summary>
    Private mAscending As Boolean

    ''' <summary>
    ''' Used to toggle display of station information and visualizations or covor art.
    ''' </summary>
    Private mDisplayStationInfo As Boolean

    ''' <summary>
    ''' Uri provided by media player error event.
    ''' Used to access codec availability information.
    ''' </summary>
    Private mWebHelp As String

    ''' <summary>
    ''' Used when opening a url to flag errors so that
    ''' message to delete entry is not displayed.
    ''' </summary>
    Private mOpenUrl As Boolean

#End Region

#Region " Form-Level Constants "

    Private Const cPlay As String = "Play"
    Private Const cStop As String = "Stop"

#End Region

#Region " Form-Level Enums "

    Private Enum NotifyIcons
        DefaultIcon = 0
        CbsBlackIcon = 1
        CbsWhiteIcon = 2
    End Enum

#End Region

#Region " Form-Level Structures "

    Private Structure NetworkInfoTemplate
        Dim Url As String
        Dim SourceUrl As String
        Dim BitRate As String
        Dim MaximumBitRate As String
        Dim Bandwidth As String
        Dim MaximumBandwidth As String
        Dim ReceivedPackets As String
        Dim LostPackets As String
        Dim RecoveredPackets As String
        Dim ReceptionQuality As String
        Dim SourceProtocol As String
    End Structure

    Private Structure StationInfoTemplate
        Dim Name As String
        Dim CurrentPlaylistName As String
        Dim Status As String
        Dim CurrentPosition As String
        Dim MoreInfoRef As String
        Dim AdUrl As String
        Dim Author As String
        Dim Copyright As String
        Dim CoverArt As String
        Dim SourceUrl As String
        Dim Title As String
    End Structure

    Private NetworkInfo As New NetworkInfoTemplate
    Private StationInfo As New StationInfoTemplate

#End Region

#Region " Form Events "

    ''' <summary>
    ''' Event occurs when form is moved.
    ''' Record the values of the X and Y coordinates.
    ''' </summary>
    Private Sub MainForm_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Settings.XPosition = Me.Location.X
        Settings.YPosition = Me.Location.Y
    End Sub

    ''' <summary>
    ''' If form is shown, hide NotifyIcon1.
    ''' If StartInTray is true, hide MainForm and show NotifyIcon1.
    ''' </summary>
    Private Sub MainForm_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        Try
            ' Load form at saved screen position unless it is 0,0 (default).
            ' In that case, calculate center bounds and locate form there.
            If Settings.XPosition = 0 And Settings.YPosition = 0 Then
                My.Settings.XPosition = GetCenterBounds(Me).X
                My.Settings.YPosition = GetCenterBounds(Me).Y
                Me.Location = GetCenterBounds(Me)
                My.Settings.Save()

            Else
                ' Validate screen X position. If invalid set to center bounds.
                If Settings.XPosition < 0 Or Settings.XPosition > My.Computer.Screen.Bounds.Width - Me.Width Then
                    Settings.XPosition = GetCenterBounds(Me).X
                    My.Settings.XPosition = Settings.XPosition
                    Settings.YPosition = GetCenterBounds(Me).Y
                    My.Settings.YPosition = Settings.YPosition
                    My.Settings.Save()
                End If

                ' Validate screen Y position. If invalid set to center bounds.
                If Settings.YPosition < 0 Or Settings.YPosition > My.Computer.Screen.Bounds.Height - Me.Height Then
                    Settings.XPosition = GetCenterBounds(Me).X
                    My.Settings.XPosition = Settings.XPosition
                    Settings.YPosition = GetCenterBounds(Me).Y
                    My.Settings.YPosition = Settings.YPosition
                    My.Settings.Save()
                End If

                Me.Location = New System.Drawing.Point(Settings.XPosition, Settings.YPosition)
            End If

            ' Hide MainForm if configured.
            If Settings.StartAsIcon = True Then
                Me.WindowState = FormWindowState.Minimized
                Me.ShowInTaskbar = False
                Me.Opacity = 0
                NotifyIcon1.Visible = True
                NotifyIcon1.ContextMenuStrip = Me.NotifyIconContextMenu

                ' If topmost is set, disable it now because icon is shown.
                If Settings.KeepOnTop = True Then
                    Me.TopMost = True
                End If

            Else
                ' Display main form.
                Me.WindowState = FormWindowState.Normal
                Me.ShowInTaskbar = True
                Application.DoEvents()  ' todo - debug.
                Me.Opacity = 1
                NotifyIcon1.Visible = False
                NotifyIcon1.ContextMenuStrip = Nothing
            End If

        Catch ex As AccessViolationException
            MessageBox.Show("An error occurred in ""MainForm_Shown."" The system returned the following information:" & _
                vbCrLf & ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    ''' <summary>
    ''' Hide MainForm when Escape is pressed.
    ''' Display easter egg.
    ''' </summary>
    Private Sub MainForm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then

            ' Switch to system tray.
            Me.WindowState = FormWindowState.Minimized
            Me.ShowInTaskbar = False
            NotifyIcon1.Visible = True
            NotifyIcon1.ContextMenuStrip = Me.NotifyIconContextMenu

            ' If player is in full screen mode, disable it.
            If Player.fullScreen = True Then
                Player.fullScreen = False
            End If

            ' If topmost is set, disable it now because icon is shown.
            If Settings.KeepOnTop = True Then
                Me.TopMost = False
            End If

        ElseIf e.Alt And e.Control And e.Shift And e.KeyCode = Keys.H Then
            ' Disconnect stream.
            Disconnect()

            ' Display easter egg.
            pnlStationInfo.BackgroundImage = My.Resources.EasterEgg
            pnlStationInfo.BackgroundImageLayout = ImageLayout.Stretch
        End If

    End Sub

    ''' <summary>
    ''' Set Title. Read XML file and populate ListView. Read Settings.
    ''' </summary>
    Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            ' Check if Media Player 9 or greater is installed.
            Try
                If CInt(Player.versionInfo.Substring(0, Player.versionInfo.IndexOf("."))) < 9 Then  ' Check major version. 
                    MessageBox.Show("This program requires Windows?Media Player version 9 or higher." & vbCrLf & _
                                    "Please download and install the latest version of Windows Media Player from Microsoft?" & vbCrLf & vbCrLf & _
                                    My.Application.Info.Title & " will now close.", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    ' Exit application
                    NotifyIcon1.Dispose()
                    AppExit()
                End If
            Catch ex As COMException
                ' If error occurs, cancel topmost.
                If Me.TopMost = True Then Me.TopMost = False

                MessageBox.Show("This program requires Windows?Media Player version 9 or higher." & vbCrLf & _

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -