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
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....
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.
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
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!
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
Last edited by TT(n); June 26th, 2010 at 02:35 AM.
Reason: Option strict/explicit now
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.