Subscribe to Satan Eats Cheese Whiz        RSS Feed

Set the brightness of a Laptop through VB.NET

Icon 3 Comments
Ever needed to dim a screen down programmatically to save power while a lengthy operation completes its task? Ever needed to make the screen brighter because users were having difficulty viewing your application, or you just wanted to piss the user off fading the screen in and out of brightness to prompt the user for input?

Either way, all three of these options are possible using the WINAPI! Note that setting the brightness may not work on some graphics cards, but it should work on most of them.

First we need to make a declaration to the CreateFile method. This will return the pointer of the device we would like to communicate with, and we want to use it for communication with the graphics card.

Private Declare Auto Function CreateFile Lib "kernel32" _
	(ByVal lpFileName As String, ByVal dwDesiredAccess As Int32, _
	ByVal dwShareMode As Int32, ByVal lpSecurityAttributes As IntPtr, _
	ByVal dwCreationDisposition As Int32, _
	ByVal dwFlagsAndAttributes As Int32, ByVal hTemplateFile As IntPtr) As IntPtr

Now we need a couple of declarations to tell the function that we want to read the device (because we aren't doing any writing as of yet), and that it is already existing.

Const GENERIC_READ As Int32 = &H80000000%
Const SHARE_ALL As Int32 = &H7%
Const OPEN_EXISTING As Int32 = &H3%

Now that we have the CreateFile method setup, we are able to create some dependencies for the DeviceIoControl method we will be using to actually set the value.


Private Structure BRIGHTNESS
	Public ucDisplayPolicy As Byte
	Public ucACBrightness As Byte
	Public ucDCBrightness As Byte
End Structure

We can now declare the function like so:

Private Declare Auto Function DeviceIoControl Lib "kernel32" _
	(ByVal hDevice As IntPtr, ByVal dwIoControlCode As Int32, _
	ByRef lpInBuffer As BRIGHTNESS, ByVal nInBufferSize As Int32, _
	ByVal lpOutBuffer As IntPtr, ByVal nOutBufferSize As Int32, _
	ByRef lpBytesReturned As Int32, ByVal lpOverlapped As IntPtr) As Boolean

We will also need to declare the CloseHandle function, to close the handle of the CreateFile method we will be using.

Private Declare Auto Function CloseHandle Lib "kernel32" _
	(ByVal hObject As IntPtr) As Boolean

Now that we have all our declarations, we can now use the function to set the brightness.

''' <summary>
''' Set the brightness of the screen.
''' </summary>
''' <param name="Brightness">The Screen Brightness</param>
''' <remarks></remarks>
Private Sub SetBrightness(ByRef Brightness As BRIGHTNESS)
	' Get the display
	Dim pDisplay As IntPtr = _
	CreateFile("\\.\LCD", GENERIC_READ, SHARE_ALL, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)

	' Set the brightness.
	Brightness, Marshal.SizeOf(Brightness), IntPtr.Zero, 0, Nothing, IntPtr.Zero)
	' Close the handle.
End Sub

We can now call the function like so (put this code in a subroutine):

Dim brightness As BRIGHTNESS

' The brightness on DC Power
brightness.ucDCBrightness = 100
' The Brightness on AC Power
brightness.ucACBrightness = 100

' Call the function.

And now you should be able to set the screen brightness! Be aware that you won't be able to make the screen completely dark (well, in Vista anyway, I haven't tried it on any other machine), it will only go as low as it will in the Power Options settings. Experiment with the values a little bit to obtain different results.

Thanks for reading, and I hope you learnt something new about screen brightness! :)

3 Comments On This Entry

Page 1 of 1


28 March 2008 - 03:25 PM

I tried this on my laptop and nothing happens. The DeviceIoControl return false. I had written it myself in C# and tried your VB.NET version with the same result.

Any idea what might be going on?

Cheers, David


04 April 2008 - 06:38 PM
Sorry for the delayed response, I've been pretty busy this week. How old is your Laptop? This method seems to only work on some (not all) graphics cards unfortunately, so I have reason to believe this could possibly be the case.

Also, do you have access to another laptop, and when trying the code does it work on that laptop?

Thanks :).


02 February 2009 - 04:42 AM
I'm using this on a Vista Home Premium like a dream in my app.

I can set my brightness anywhere between 0 and 100%.

I'm stuck at one spot though, I want to read (and store) the current brightness for AC/DC before I change it. This will allow me to restore the brightness when the application ends.

MSDN suggests the WMIMonitorBrightness Class, but i have no clue how to use it or what imports I'll need. Apparently the IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS function is deprecated in Vista, so what options do I have?

I tried using QUERY_DISPLAY in the same manner as SET display, but got stuck at the formation of
  Brightness, Marshal.SizeOf(Brightness), IntPtr.Zero, 0, Nothing, IntPtr.Zero) line
and where do i get the &H number for

Please Help. Either show me how to pull data from IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS or WMIMonitorBrightness. A Vista-only application is fine for now...but I would like to expand it to XP eventually.

Page 1 of 1


Welcome to Satan Eats Cheese Whiz

Satan Eats Cheese Whiz is a programming blog, mostly directed at .NET Programming. Here you will find snippets, tutorials and other useful code that you can adapt or use with your projects. When using the code provided, please leave the comments intact, and link back to the page you got it from. Thanks for viewing!

Goto My Wiki
Syndicate this Blog (RSS)


0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)