[RESOLVED] Monitor Brightness
I'm looking for a way to adjust the monitor's temperature. There's a program called f.lux that does this, but it doesn't get dark enough. The program I'm writing will be used on a boat and needs to be dark enough so that it's not blinding while driving at night.
Any thoughts?
Re: [RESOLVED] Monitor Temperature
I know this is marked resolved but I was wondering if there is a similar function to get the current gamma settings? I tried getDeviceGammaRamp, but from what I could tell that returns whether or not the devices is able to change the gamma.
Re: [RESOLVED] Monitor Temperature
Hi Stin :)
Hmm, not sure that there is something, offhand :)
Could you perhaps explain a bit more what you'd like to achieve then we can hopefully, possibly identify a solution
Hannes
Re: [RESOLVED] Monitor Temperature
The program will allow the user to change their gamma settings and then reset them when they leave. So really I just would like to capture what it's at when they start the program. The one you gave me starts at 14 or 18 which seams to be an arbitrary number.
Re: [RESOLVED] Monitor Temperature
I see :)
We will have to use GetDeviceGammaRamp at Form load, then with the Form_Closing event set it to what was retrieved at Form load. That will reset it to the value it was before the program was run.
Should I try to incorporate this into my previous example for you?
Sorry, this was very sloppy programming on my side, I should have done that initially :)
Re: [RESOLVED] Monitor Temperature
If it's not too much trouble that would fantastic. Using APIs isn't my forte, and as I mentioned, I tried using the getDeviceGammaRamp, but couldn't get it to work.
Thank you so much for your help.
Re: [RESOLVED] Monitor Temperature
It's no problem at all :)
I'll have a look at it over the weekend
Hannes
Re: [RESOLVED] Monitor Temperature
Hi Stin :)
I am looking for external help on this issue. I know what to use and what should be done, but, for the love of me, I can't manage to get it working. perhaps I'm too stupid :p
Hang in there...
Hannes
Re: [RESOLVED] Monitor Temperature
I really appreciate your help. It's not urgent, and I could get by with a hard coded number.
Re: [RESOLVED] Monitor Temperature
This might steer someone in the right direction. Seems that it's been solved before. Looks like C++ though.
http://www.autohotkey.com/forum/topic32034.html
Re: [RESOLVED] Monitor Temperature
That is not C code.. As far as I can tell it is a script for AutoHotKey
Re: [RESOLVED] Monitor Temperature
OK, I haven't received any response yt on any of the forums I have posted this question to :(
I did try this :
Code:
Imports System
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Class SetBrightness
Inherits Form
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Private Structure RAMP
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> _
Public Red As UInt16()
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> _
Public Green As UInt16()
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=256)> _
Public Blue As UInt16()
End Structure
<DllImport("gdi32.dll")> _
Private Shared Function SetDeviceGammaRamp(ByVal hDC As IntPtr, ByRef lpRamp As RAMP) As Boolean
End Function
<DllImport("user32.dll")> _
Private Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
End Function
<DllImport("gdi32.dll")> _
Private Shared Function GetDeviceGammaRamp(ByVal hdc As Int32, ByVal lpv As Int32) As Int32
End Function
Private WithEvents TrackBar1 As System.Windows.Forms.TrackBar
Private Shared Ramp1(0 To 255, 0 To 2) As Integer
Private Shared s_ramp As New RAMP()
Private Shared MyRamp As New RAMP()
Private Shared Sub SetGamma(ByVal gamma As Integer)
s_ramp.Red = New UShort(255) {}
s_ramp.Green = New UShort(255) {}
s_ramp.Blue = New UShort(255) {}
For i As Integer = 1 To 255
' gamma is a value between 3 and 44
s_ramp.Red(i) = InlineAssignHelper(s_ramp.Green(i), InlineAssignHelper(s_ramp.Blue(i), CUShort((Math.Min(65535, Math.Max(0, Math.Pow((i + 1) / 256.0R, gamma * 0.1) * 65535 + 0.5))))))
Next
' Now set the value.
SetDeviceGammaRamp(GetDC(IntPtr.Zero), s_ramp)
End Sub
Private Shared Sub RestoreGamma(ByVal gamma As Integer)
MyRamp.Red = New UShort(255) {}
MyRamp.Green = New UShort(255) {}
MyRamp.Blue = New UShort(255) {}
For x = 0 To 255
MyRamp.Red(x) = Ramp1(x, 0)
MyRamp.Green(x) = Ramp1(x, 1)
MyRamp.Blue(x) = Ramp1(x, 2)
Next
SetDeviceGammaRamp(GetDC(IntPtr.Zero), MyRamp)
End Sub
Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value
Return value
End Function
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
SetGamma(TrackBar1.Value)
End Sub
Private Sub SetBrightness_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
RestoreGamma(Ramp1(0, 0))
End Sub
Private Sub SetBrightness_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetDeviceGammaRamp(GetDC(IntPtr.Zero), Ramp1(0, 0))
End Sub
End Class
But it doesn't restore the screen back. either the settings doesn't get stored into my array ( which I think is the most likely explanation ), or the SetDeviceGammaRamp doesn't want to get the right values to apply. I'm feeling out of my league here.... :(
Re: [RESOLVED] Monitor Temperature
Yikes. If you're out of your league...
Re: [RESOLVED] Monitor Temperature
I didn't originally realize how popular this question was.
I mean the question of brightness, instead of temp.
Since this is pretty interesting, and possibly useful to me as well, I'll take a look into it.
I've seen alot online about it, and several programs that apparently do it.
Re: [RESOLVED] Monitor Temperature
Another set of eyes is always welcome.
Re: [RESOLVED] Monitor Temperature
got it!
I found the vb6 version kickin around in the API-Guide.
Made it work for the desktop with the right Device context.
It will take a few moments to convert this to .NET, so I'll post back.
For giggles, here is the vb6 example.
Code:
Option Explicit
Private Ramp1(0 To 255, 0 To 2) As Integer
Private Ramp2(0 To 255, 0 To 2) As Integer
Private Declare Function apiGetDeviceGammaRamp Lib "gdi32" Alias "GetDeviceGammaRamp" (ByVal hdc As Long, ByRef lpv As Any) As Long
Private Declare Function apiSetDeviceGammaRamp Lib "gdi32" Alias "SetDeviceGammaRamp" (ByVal hdc As Long, ByRef lpv As Any) As Long
Private Declare Function apiCopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long
Private Declare Function apiGetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Long) As Long
Private Declare Function apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
Private Sub Form_Load()
Dim iCtr As Integer
Dim lVal As Long
Call apiGetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp1(0, 0))
For iCtr = 0 To 255
lVal = Int2Lng(Ramp1(iCtr, 0))
Ramp2(iCtr, 0) = Lng2Int(Int2Lng(Ramp1(iCtr, 0)) / 2)
Ramp2(iCtr, 1) = Lng2Int(Int2Lng(Ramp1(iCtr, 1)) / 2)
Ramp2(iCtr, 2) = Lng2Int(Int2Lng(Ramp1(iCtr, 2)) / 2)
Next iCtr
Call apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp2(0, 0))
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp1(0, 0))
End Sub
Public Function Int2Lng(IntVal As Integer) As Long
Call apiCopyMemory(Int2Lng, IntVal, 2)
End Function
Public Function Lng2Int(Value As Long) As Integer
Call apiCopyMemory(Lng2Int, Value, 2)
End Function
Re: [RESOLVED] Monitor Temperature
Just when I was starting too lose all hope, the Honourable mr TT(n) pops in! :D
Let us hope for the best :)
Hannes
Re: [RESOLVED] Monitor Temperature
I just found a vb6 project too at CodeProject.com that I was starting to look at!
1 Attachment(s)
Re: [RESOLVED] Monitor Temperature
Sad to say, but VB 6 wins .NET hands down here!! Amzingly surprisingly easy in VB 6 - unbelievable!!
Look at this. Add 2 buttons on a VB 6 form, and a checkbox. If the Checkbox is checked and command1 is clicked it dims. And when the second commandbutton is clicked, it restores everything!
I am flabbergasted! This is one more reason why I'll probably never leave VB 6!
Hannes
Re: [RESOLVED] Monitor Temperature
Here is a basic .NET example, which works to start from. {XP only, Vista/7 solution shown later in this thread}
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Ramp1(255, 2) As Short
Private Ramp2(255, 2) As Short
Private Declare Function apiGetDeviceGammaRamp Lib "gdi32" Alias "GetDeviceGammaRamp" (ByVal hdc As Int32, ByRef lpv As Short) As Int32
Private Declare Function apiSetDeviceGammaRamp Lib "gdi32" Alias "SetDeviceGammaRamp" (ByVal hdc As Int32, ByRef lpv As Short) As Int32
Private Declare Function apiCopyMemoryIntShrt Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Int32, ByRef Source As Short, ByVal Length As Int32) As Int32
Private Declare Function apiCopyMemoryShrtInt Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Short, ByRef Source As Int32, ByVal Length As Int32) As Int32
Private Declare Function apiGetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Int32) As Int32
Private Declare Function apiGetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Int32
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim iCtr As Short
Dim lVal As Int32
apiGetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp1(0, 0))
For iCtr = 0 To 255
lVal = Shr2Int(Ramp1(iCtr, 0))
Ramp2(iCtr, 0) = Int2Shr(CInt(Shr2Int(Ramp1(iCtr, 0)) / 2))
Ramp2(iCtr, 1) = Int2Shr(CInt(Shr2Int(Ramp1(iCtr, 1)) / 2))
Ramp2(iCtr, 2) = Int2Shr(CInt(Shr2Int(Ramp1(iCtr, 2)) / 2))
Next
apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp2(0, 0))
End Sub
Private Sub Form1_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp1(0, 0))
End Sub
Public Function Shr2Int(ByRef shrVal As Short) As Int32
apiCopyMemoryIntShrt(Shr2Int, shrVal, 2)
End Function
Public Function Int2Shr(ByRef Value As Int32) As Short
apiCopyMemoryShrtInt(Int2Shr, Value, 2)
End Function
End Class
Re: [RESOLVED] Monitor Temperature
Great work TT(n)!
I do get a red squiggly at this line though :
Code:
apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), Ramp1(0, 0))
And it tells me that it cannot convert Short to Type RAMP
Coincidentally, That is the error I have been battling with the whole time, before I got to the code I had in post # 21