我在Visual Basic中创建浏览器,但在保存设置时出现问题。 在高级选项中,在隐私选项卡下,我希望我的浏览器在关闭窗口后保留设置。如果我单击"从不记住密码",然后关闭窗口,则不会存储设置。 此外,当我输入URL时,搜索后URL未正确更新。
有没有人对如何使我的项目的这些部分工作有任何想法? 我对编码有些新意,所以添加到浏览器的任何提示或额外的想法都会受到欢迎并且非常感激,因为我希望看到这个功能完全正常。
谢谢! :d
表格1
Imports System.Net
Imports System.IO
Public Class Magycka
Private Declare Sub keybd_event Lib "user32" (ByVal volumeUpOrDown As Byte, ByVal v1 As Byte, ByVal v2 As Integer, ByVal v3 As Integer)
Private Sub Magycka_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
File.Delete("C:\Magycka\history.txt")
For Each link As String In History.lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
For Each favoriteName As String In lstName.Items
File.AppendAllText("C:\Magycka\favoriteName.txt", favoriteName & vbNewLine)
Next
For Each favoriteURL As String In lstURL.Items
File.AppendAllText("C:\Magycka\favoriteURL.txt", favoriteURL & vbNewLine)
Next
End Sub
Private Sub tmrDate_Tick(sender As Object, e As EventArgs) Handles tmrDate.Tick
lblTime.Text = DateTime.Now.ToString
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbSearchEngine.Items.Add("Google")
cmbSearchEngine.Items.Add("Google Images")
cmbSearchEngine.Items.Add("YouTube")
cmbSearchEngine.SelectedIndex = 0
addTab(TabControl1)
Try
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
If My.Settings.homePageorBlankPage = 0 Then
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
Else
End If
Try
History.Visible = False
For Each link As String In File.ReadAllLines("C:\Magycka\history.txt")
History.lstHistory.Items.Add(link.ToString)
Next
Catch ex As Exception
End Try
Try
For Each url As String In File.ReadAllLines("C:\Magycka\favoriteURL.txt")
ListBox3.Items.Add(url)
Next
Catch ex As Exception
End Try
Try
ListBox3.SelectedIndex = 0
Catch ex As Exception
End Try
Try
For Each Name As String In File.ReadAllLines("C:\Magycka\favoriteName.txt")
Dim newBookmark As New ToolStripButton
newBookmark.Text = Name
newBookmark.Tag = ListBox3.SelectedItem.ToString
tsBookmarks.Items.Add(newBookmark)
ListBox3.SelectedIndex = ListBox3.SelectedIndex + 1
Next
Catch ex As Exception
End Try
createdBookClick()
End Sub
Private Sub txtURLsearchEngines_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURLsearchEngines.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
End If
Case 1
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" + txtURL.Text)
End If
Case 2
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End If
End Select
End Sub
Private Sub txtURL_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURL.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
If e.KeyCode = Keys.Enter Then
browser.Navigate(txtURL.Text)
End If
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoBack()
End Sub
Private Sub btnForward_Click(sender As Object, e As EventArgs) Handles btnForward.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoForward()
End Sub
Private Sub brnRefresh_Click(sender As Object, e As EventArgs) Handles brnRefresh.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Refresh()
End Sub
Private Sub btnURL_Click(sender As Object, e As EventArgs) Handles btnURL.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub btnVolumeUp_Click(sender As Object, e As EventArgs) Handles btnVolumeUp.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeUp, 0, 0, 0)
End Sub
Private Sub btnVolumeDown_Click(sender As Object, e As EventArgs) Handles btnVolumeDown.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeDown, 0, 0, 0)
End Sub
Private Sub btnMute_Click(sender As Object, e As EventArgs) Handles btnMute.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeMute, 0, 0, 0)
End Sub
Private Sub OptionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OptionsToolStripMenuItem.Click
AdvancedOptions.Show()
End Sub
Private Sub ViewHistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewHistoryToolStripMenuItem.Click
History.Show()
End Sub
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
Case 1
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" = txtURLsearchEngines.Text)
Case 2
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End Select
End Sub
Private Sub NewTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewTabToolStripMenuItem.Click
addTab(TabControl1)
End Sub
Private Sub CloseTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseTabToolStripMenuItem.Click
removeTab()
End Sub
Public Sub addTab(ByRef TabControl As TabControl)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(My.Settings.homePage)
TabControl1.SelectedTab = tab
End Sub
Public Sub addTabfromHistory(ByRef TabControl As TabControl, ByRef link As String)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(link)
TabControl1.SelectedTab = tab
End Sub
Public Sub removeTab()
If TabControl1.TabPages.Count <> 0 Then
TabControl1.TabPages.Remove(TabControl1.SelectedTab)
End If
End Sub
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Try
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
End Sub
Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
Dim newWindow As New Magycka
newWindow.Show()
End Sub
Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
txtURL.Undo()
End Sub
Private Sub RedoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RedoToolStripMenuItem.Click
txtURL.ClearUndo()
End Sub
Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click
txtURL.Cut()
End Sub
Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
txtURL.Copy()
End Sub
Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasteToolStripMenuItem.Click
txtURL.Paste()
End Sub
Private Sub SelectAllToolStripMenuItem_Click(sender As Object, e As EventArgs)
txtURL.SelectAll()
End Sub
Private Sub SaveSourceCodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveSourceCodeToolStripMenuItem.Click
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim fileName As String = InputBox("Enter FileName:", "Save HTML", ".txt")
Dim path As String = "C:\Magycka\" & fileName
Try
If File.Exists(path) Then
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
Else
File.Create(path).Dispose()
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
End If
Catch ex As Exception
End Try
End Sub
Private Sub addBookmarks()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim newBookmark As New ToolStripButton
newBookmark.Text = TabControl1.SelectedTab.Text
newBookmark.Tag = browser.Url
tsBookmarks.Items.Add(newBookmark)
lstName.Items.Add(TabControl1.SelectedTab.Text)
lstURL.Items.Add(browser.Url.ToString)
AddHandler newBookmark.Click, AddressOf newBookmarkClick
End Sub
Private Sub newBookmarkClick(sender As Object, e As EventArgs)
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
If TypeOf sender Is ToolStripButton Then
browser.Navigate(CType(sender, ToolStripButton).Tag)
End If
End Sub
Private Sub createdBookClick()
For Each item As ToolStripButton In tsBookmarks.Items
AddHandler item.Click, AddressOf bookClick
Next
End Sub
Private Sub bookClick(sender As Object, e As EventArgs)
txtURL.Text = CType(sender, ToolStripButton).Tag
bookNav()
End Sub
Private Sub bookNav()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub BookmarkPageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BookmarkPageToolStripMenuItem.Click
addBookmarks()
End Sub
End Class
高级选项
Imports System.Net
Imports System.IO
Public Class Magycka
Private Declare Sub keybd_event Lib "user32" (ByVal volumeUpOrDown As Byte, ByVal v1 As Byte, ByVal v2 As Integer, ByVal v3 As Integer)
Private Sub Magycka_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
File.Delete("C:\Magycka\history.txt")
For Each link As String In History.lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
For Each favoriteName As String In lstName.Items
File.AppendAllText("C:\Magycka\favoriteName.txt", favoriteName & vbNewLine)
Next
For Each favoriteURL As String In lstURL.Items
File.AppendAllText("C:\Magycka\favoriteURL.txt", favoriteURL & vbNewLine)
Next
End Sub
Private Sub tmrDate_Tick(sender As Object, e As EventArgs) Handles tmrDate.Tick
lblTime.Text = DateTime.Now.ToString
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbSearchEngine.Items.Add("Google")
cmbSearchEngine.Items.Add("Google Images")
cmbSearchEngine.Items.Add("YouTube")
cmbSearchEngine.SelectedIndex = 0
addTab(TabControl1)
Try
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
If My.Settings.homePageorBlankPage = 0 Then
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
Else
End If
Try
History.Visible = False
For Each link As String In File.ReadAllLines("C:\Magycka\history.txt")
History.lstHistory.Items.Add(link.ToString)
Next
Catch ex As Exception
End Try
Try
For Each url As String In File.ReadAllLines("C:\Magycka\favoriteURL.txt")
ListBox3.Items.Add(url)
Next
Catch ex As Exception
End Try
Try
ListBox3.SelectedIndex = 0
Catch ex As Exception
End Try
Try
For Each Name As String In File.ReadAllLines("C:\Magycka\favoriteName.txt")
Dim newBookmark As New ToolStripButton
newBookmark.Text = Name
newBookmark.Tag = ListBox3.SelectedItem.ToString
tsBookmarks.Items.Add(newBookmark)
ListBox3.SelectedIndex = ListBox3.SelectedIndex + 1
Next
Catch ex As Exception
End Try
createdBookClick()
End Sub
Private Sub txtURLsearchEngines_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURLsearchEngines.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
End If
Case 1
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" + txtURL.Text)
End If
Case 2
If e.KeyCode = Keys.Enter Then
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End If
End Select
End Sub
Private Sub txtURL_KeyUp(sender As Object, e As KeyEventArgs) Handles txtURL.KeyUp
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
If e.KeyCode = Keys.Enter Then
browser.Navigate(txtURL.Text)
End If
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoBack()
End Sub
Private Sub btnForward_Click(sender As Object, e As EventArgs) Handles btnForward.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.GoForward()
End Sub
Private Sub brnRefresh_Click(sender As Object, e As EventArgs) Handles brnRefresh.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Refresh()
End Sub
Private Sub btnURL_Click(sender As Object, e As EventArgs) Handles btnURL.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub btnVolumeUp_Click(sender As Object, e As EventArgs) Handles btnVolumeUp.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeUp, 0, 0, 0)
End Sub
Private Sub btnVolumeDown_Click(sender As Object, e As EventArgs) Handles btnVolumeDown.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeDown, 0, 0, 0)
End Sub
Private Sub btnMute_Click(sender As Object, e As EventArgs) Handles btnMute.Click
Call keybd_event(System.Windows.Forms.Keys.VolumeMute, 0, 0, 0)
End Sub
Private Sub OptionsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OptionsToolStripMenuItem.Click
AdvancedOptions.Show()
End Sub
Private Sub ViewHistoryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewHistoryToolStripMenuItem.Click
History.Show()
End Sub
Private Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
browser.Navigate(My.Settings.homePage)
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim browser As webBrowserfunction = Me.TabControl1.SelectedTab.Tag
Select Case (cmbSearchEngine.SelectedIndex)
Case 0
browser.Navigate("https://www.google.com/#q=" + txtURLsearchEngines.Text)
Case 1
browser.Navigate("https://www.google.com/search?site=imghp&tbm=isch&source=hp&biw=1366&bih=661&q=" = txtURLsearchEngines.Text)
Case 2
browser.Navigate("https://www.youtube.com/results?search_query=" + txtURLsearchEngines.Text)
End Select
End Sub
Private Sub NewTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewTabToolStripMenuItem.Click
addTab(TabControl1)
End Sub
Private Sub CloseTabToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseTabToolStripMenuItem.Click
removeTab()
End Sub
Public Sub addTab(ByRef TabControl As TabControl)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(My.Settings.homePage)
TabControl1.SelectedTab = tab
End Sub
Public Sub addTabfromHistory(ByRef TabControl As TabControl, ByRef link As String)
Dim browser As New webBrowserfunction
Dim tab As New TabPage
browser.Tag = tab
tab.Tag = browser
TabControl1.TabPages.Add(tab)
tab.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(link)
TabControl1.SelectedTab = tab
End Sub
Public Sub removeTab()
If TabControl1.TabPages.Count <> 0 Then
TabControl1.TabPages.Remove(TabControl1.SelectedTab)
End If
End Sub
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Try
txtURL.Text = browser.Url.ToString
Catch ex As Exception
End Try
End Sub
Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
Dim newWindow As New Magycka
newWindow.Show()
End Sub
Private Sub UndoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UndoToolStripMenuItem.Click
txtURL.Undo()
End Sub
Private Sub RedoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RedoToolStripMenuItem.Click
txtURL.ClearUndo()
End Sub
Private Sub CutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CutToolStripMenuItem.Click
txtURL.Cut()
End Sub
Private Sub CopyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CopyToolStripMenuItem.Click
txtURL.Copy()
End Sub
Private Sub PasteToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles PasteToolStripMenuItem.Click
txtURL.Paste()
End Sub
Private Sub SelectAllToolStripMenuItem_Click(sender As Object, e As EventArgs)
txtURL.SelectAll()
End Sub
Private Sub SaveSourceCodeToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveSourceCodeToolStripMenuItem.Click
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim fileName As String = InputBox("Enter FileName:", "Save HTML", ".txt")
Dim path As String = "C:\Magycka\" & fileName
Try
If File.Exists(path) Then
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
Else
File.Create(path).Dispose()
Dim allText As String
allText = browser.DocumentText
File.WriteAllText(path, allText)
End If
Catch ex As Exception
End Try
End Sub
Private Sub addBookmarks()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
Dim newBookmark As New ToolStripButton
newBookmark.Text = TabControl1.SelectedTab.Text
newBookmark.Tag = browser.Url
tsBookmarks.Items.Add(newBookmark)
lstName.Items.Add(TabControl1.SelectedTab.Text)
lstURL.Items.Add(browser.Url.ToString)
AddHandler newBookmark.Click, AddressOf newBookmarkClick
End Sub
Private Sub newBookmarkClick(sender As Object, e As EventArgs)
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
If TypeOf sender Is ToolStripButton Then
browser.Navigate(CType(sender, ToolStripButton).Tag)
End If
End Sub
Private Sub createdBookClick()
For Each item As ToolStripButton In tsBookmarks.Items
AddHandler item.Click, AddressOf bookClick
Next
End Sub
Private Sub bookClick(sender As Object, e As EventArgs)
txtURL.Text = CType(sender, ToolStripButton).Tag
bookNav()
End Sub
Private Sub bookNav()
Dim browser As webBrowserfunction = TabControl1.SelectedTab.Tag
browser.Navigate(txtURL.Text)
End Sub
Private Sub BookmarkPageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BookmarkPageToolStripMenuItem.Click
addBookmarks()
End Sub
End Class
历史
Imports System.IO
Public Class History
Private Sub History_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
Me.Visible = False
For Each link As String In lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
End Sub
Private Sub btnClearHis_Click(sender As Object, e As EventArgs) Handles btnClearHis.Click
lstHistory.Items.Clear()
System.IO.File.Delete("C:\Magycka\History.txt")
End Sub
Private Sub btnCloseHis_Click(sender As Object, e As EventArgs) Handles btnCloseHis.Click
For Each link As String In lstHistory.Items
File.AppendAllText("C:\Magycka\History.txt", link & vbNewLine)
Next
Close()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
lstHistory.Items.Remove(lstHistory.SelectedItem)
End Sub
Private Sub lstHistory_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstHistory.MouseDoubleClick
Magycka.addTabfromHistory(Magycka.TabControl1, lstHistory.SelectedItem.ToString)
End Sub
End Class