-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.vb
151 lines (122 loc) · 4.91 KB
/
Main.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Imports System.ComponentModel
Imports Microsoft.Web.WebView2.Core
Public Class Main
Dim AllowClose As Boolean = False
Private Sub WV_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WV.NavigationCompleted
Try
If Text <> WV.CoreWebView2.DocumentTitle Then
Text = WV.CoreWebView2.DocumentTitle
End If
Catch ex As Exception
' Handle exception (optional)
End Try
End Sub
Private Sub Main_Invalidated(sender As Object, e As InvalidateEventArgs) Handles Me.Invalidated
'Register hotkey
Hotkey.registerHotkey(Me, "i", Hotkey.KeyModifier.Control + Hotkey.KeyModifier.Alt)
End Sub
Private Sub WV_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles WV.CoreWebView2InitializationCompleted
AddHandler WV.CoreWebView2.NewWindowRequested, AddressOf CoreWebView2_NewWindowRequested
End Sub
Private Sub CoreWebView2_NewWindowRequested(sender As Object, e As CoreWebView2NewWindowRequestedEventArgs)
e.Handled = True
Try
Dim p As New Process
p.StartInfo.UseShellExecute = True
p.StartInfo.FileName = e.Uri
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal
p.Start()
Catch ex As Exception
'Handle exception
End Try
End Sub
Private Sub SystemTrayIcon_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles SystemTrayIcon.MouseDoubleClick
DoShow()
End Sub
Private Sub Main_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
If AllowClose = False Then
e.Cancel = True
Hide()
End If
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = Hotkey.WM_HOTKEY Then
Hotkey.handleHotKeyEvent(m.WParam)
End If
MyBase.WndProc(m)
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ContextMenu_Exit.Click
AllowClose = True
Hotkey.unregisterHotkeys(Me)
Close()
End Sub
Private Sub ShowToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ContextMenu_Show.Click
DoShow()
End Sub
Private Sub Startup_Tick(sender As Object, e As EventArgs) Handles Startup.Tick
Startup.Enabled = False
Dim s() As String = Environment.GetCommandLineArgs()
For i = 1 To s.Length - 1
Select Case LCase(s(i))
Case "-startup"
Close()
End Select
Next
Opacity = 100
End Sub
Private Sub ContextMenu_Restart_Click(sender As Object, e As EventArgs) Handles ContextMenu_Restart.Click
Try
Dim p As New Process
p.StartInfo.FileName = "cmd"
p.StartInfo.Arguments = $"/C timeout /t 3 && start """" ""{Application.ExecutablePath}"""
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.Start()
Application.Exit()
Catch ex As Exception
'Handle exception
End Try
End Sub
Public Shared Sub DoShow()
If Main.WindowState = FormWindowState.Minimized Then
Main.WindowState = FormWindowState.Maximized
End If
Main.Show()
Main.Activate()
End Sub
End Class
Public Class Hotkey
#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
''' <summary>
''' Declaration of winAPI function wrappers. The winAPI functions are used to register / unregister a hotkey
''' </summary>
Private Declare Function RegisterHotKey Lib "user32" _
(hwnd As IntPtr, id As Integer, fsModifiers As Integer, vk As Integer) As Integer
Private Declare Function UnregisterHotKey Lib "user32" (hwnd As IntPtr, id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum _
'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc.
#End Region
#Region "Hotkey registration, unregistration and handling"
Public Shared Sub registerHotkey(ByRef sourceForm As Form, triggerKey As String, modifier As KeyModifier)
Dim val As Integer
If triggerKey = "ESC" Then
val = Keys.Escape
Else
val = Asc(triggerKey.ToUpper)
End If
RegisterHotKey(sourceForm.Handle, 1, modifier, val)
End Sub
Public Shared Sub unregisterHotkeys(ByRef sourceForm As Form)
UnregisterHotKey(sourceForm.Handle, 1) 'Remember to call unregisterHotkeys() when closing your application.
End Sub
Public Shared Sub handleHotKeyEvent(hotkeyID As IntPtr)
Main.DoShow()
End Sub
#End Region
End Class