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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Namespace ScreenBrightness
Partial Public Class Form1
Inherits Form
Private iCount As Integer = 0
'global counter for hiding/closing form after certian period of inactivity
Private bLevels As Byte()
'array of valid level values
Private arguments As String()
Public Sub New(args As String())
arguments = args
InitializeComponent()
End Sub
'In case of an incompatible system, the form has to be shown in order to close the app...as far as I know ^^
Private Sub Form1_Shown(sender As Object, e As EventArgs)
bLevels = GetBrightnessLevels()
'get the level array for this system
If bLevels.Count() = 0 Then
'"WmiMonitorBrightness" is not supported by the system
Application.[Exit]()
Else
trackBar1.TickFrequency = bLevels.Count()
'adjust the trackbar ticks according the number of possible brightness levels
trackBar1.Maximum = bLevels.Count() - 1
trackBar1.Update()
trackBar1.Refresh()
check_brightness()
timer1.Enabled = True
'timer for closing form
'check the arguments
If Array.FindIndex(arguments, Function(item) item.Contains("%")) > -1 Then
startup_brightness()
End If
If arguments.Length = 0 OrElse Array.IndexOf(arguments, "hide") > -1 Then
'hide the trackbar initially if no arguments are passed
Me.Hide()
End If
End If
End Sub
Private Sub check_brightness()
Dim iBrightness As Integer = GetBrightness()
'get the actual value of brightness
Dim i As Integer = Array.IndexOf(bLevels, CByte(iBrightness))
If i < 0 Then
i = 1
End If
TrackBar1.Value = i
End Sub
Private Sub startup_brightness()
Dim sPercent As String = arguments(Array.FindIndex(arguments, Function(item) item.Contains("%")))
If sPercent.Length > 1 Then
Dim iPercent As Integer = Convert.ToInt16(sPercent.Split("%"c).ElementAt(0))
If iPercent >= 0 AndAlso iPercent <= bLevels(bLevels.Count() - 1) Then
Dim level As Byte = 100
For Each item As Byte In bLevels
If item >= iPercent Then
level = item
Exit For
End If
Next
SetBrightness(level)
check_brightness()
End If
End If
End Sub
Private Sub trackBar1_Scroll(sender As Object, e As EventArgs)
SetBrightness(bLevels(trackBar1.Value))
iCount = 0
'reset inactivity counter
End Sub
'get the actual percentage of brightness
Private Shared Function GetBrightness() As Integer
'define scope (namespace)
Dim s As New System.Management.ManagementScope("root\WMI")
'define query
Dim q As New System.Management.SelectQuery("WmiMonitorBrightness")
'output current brightness
Dim mos As New System.Management.ManagementObjectSearcher(s, q)
Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()
'store result
Dim curBrightness As Byte = 0
For Each o As System.Management.ManagementObject In moc
curBrightness = CByte(o.GetPropertyValue("CurrentBrightness"))
'only work on the first object
Exit For
Next
moc.Dispose()
mos.Dispose()
Return CInt(curBrightness)
End Function
'array of valid brightness values in percent
Private Shared Function GetBrightnessLevels() As Byte()
'define scope (namespace)
Dim s As New System.Management.ManagementScope("root\WMI")
'define query
Dim q As New System.Management.SelectQuery("WmiMonitorBrightness")
'output current brightness
Dim mos As New System.Management.ManagementObjectSearcher(s, q)
Dim BrightnessLevels As Byte() = New Byte(-1) {}
Try
Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()
'store result
For Each o As System.Management.ManagementObject In moc
BrightnessLevels = DirectCast(o.GetPropertyValue("Level"), Byte())
'only work on the first object
Exit For
Next
moc.Dispose()
mos.Dispose()
Catch generatedExceptionName As Exception
MessageBox.Show("Sorry, Your System does not support this brightness control...")
End Try
Return BrightnessLevels
End Function
Private Shared Sub SetBrightness(targetBrightness As Byte)
'define scope (namespace)
Dim s As New System.Management.ManagementScope("root\WMI")
'define query
Dim q As New System.Management.SelectQuery("WmiMonitorBrightnessMethods")
'output current brightness
Dim mos As New System.Management.ManagementObjectSearcher(s, q)
Dim moc As System.Management.ManagementObjectCollection = mos.[Get]()
For Each o As System.Management.ManagementObject In moc
o.InvokeMethod("WmiSetBrightness", New [Object]() {UInt32.MaxValue, targetBrightness})
'note the reversed order - won't work otherwise!
'only work on the first object
Exit For
Next
moc.Dispose()
mos.Dispose()
End Sub
'timer for hiding/closing form
Private Sub timer1_Tick(sender As Object, e As EventArgs)
iCount += 1
If iCount > 2 Then
If Array.IndexOf(arguments, "quit") > -1 Then
Me.Close()
Else
Me.Hide()
timer1.[Stop]()
iCount = 0
End If
End If
End Sub
Private Sub notifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Left Then
Dim p As New Point(MousePosition.X, MousePosition.Y)
Dim r As Rectangle = Screen.GetBounds(p)
'find the right position next to the icon
If p.X > r.Width / 2 Then
If p.X + 140 > r.Width Then
Me.Left = r.Width - 275
Else
Me.Left = p.X - 140
End If
Else
Me.Left = p.X
End If
If p.Y > r.Height / 2 Then
Me.Top = p.Y - 60
Else
Me.Top = p.Y
End If
check_brightness()
Me.Show()
Me.Activate()
timer1.Start()
End If
End Sub
Private Sub exitToolStripMenuItem_Click(sender As Object, e As EventArgs)
Me.Close()
End Sub
Friend WithEvents TrackBar1 As TrackBar
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.TrackBar1 = New System.Windows.Forms.TrackBar()
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'TrackBar1
'
Me.TrackBar1.Location = New System.Drawing.Point(12, 21)
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.Size = New System.Drawing.Size(260, 45)
Me.TrackBar1.TabIndex = 0
'
'Form1
'
Me.ClientSize = New System.Drawing.Size(284, 72)
Me.Controls.Add(Me.TrackBar1)
Me.Name = "Form1"
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Timer1 As Timer
Private components As IContainer
End Class
End Namespace |
Partager