You guys are AMAZING. It's why this forum is my goto for help. I feel bad that I'm not able to contribute much, but I do try.
Thanks again!
Printable View
You guys are AMAZING. It's why this forum is my goto for help. I feel bad that I'm not able to contribute much, but I do try.
Thanks again!
Well, I tried opening your solution, but it open as a blank solution in VS 2008. I've been able to get them open before. Any thoughts?
You're also amazing with all your effort you put in, if I had an employee such as you working for me, you'd have received a raise because of this porject :)
Helping others comes with time, heck, it took me 4 years to actually start trying to help other members here, I guess I was a bit too scared, or insecure. You'll be amazed what you can learn by helping others, and reading other people's questions. I have learnt a great deal with this thread, so I am happy and grateful to everyone that has contributed :)
Strange.... :confused:
I am attaching a new zip here again. And I'm adding the full code now with this post as well ( to cover both bases ) :)
Have a great night and weekend, and I'm looking forward to your next "complicated question" :D :)Code:Imports System.Runtime.InteropServices
Public Class Form1
<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, ByRef lpv As RAMP) As Boolean
End Function
Private WithEvents TrackBar1 As System.Windows.Forms.TrackBar
'Private Shared Ramp1(0 To 255, 0 To 2) As RAMP
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 RAMP)
'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), gamma)
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(MyRamp)
End Sub
Private Sub SetBrightness_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetDeviceGammaRamp(GetDC(IntPtr.Zero), MyRamp)
End Sub
End Class
PS: I'll pop in later tonight again to see if you have managed to open the zip :)
Hannes
The zips project still didn't work for me, but copying the code did. Looks like it works! Thank you a lot!
Cool :)
Just shout if you get any more issues :)
Hannes
I don't think the Boolean type matters.
I did mention that my declaration works, which was ByRef.
ByVal would be if you were using a memory API, or were omitting the optional functionality of the parameter.(this can make other parameters work sometimes)
In your example you pass the structure, which I should have caught and mentioned though.
One quick issue with the slider example.
Awesome work BTW!, I would have not figured out the InlineAssignHelper part. No, you're a genius, ... no you are, ... no YOU are.
BUG:
Trackbar can change before the load event is finished, thus locking in the wrong original ramp, which is nicely returned ofcourse, but to the wrong gamma.
Solution, put in a boolen.
In the load event switch it on.Code:Private IsLoaded As Boolean = False
In the trackbar scroll or valuechanged event put this:Quote:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetDeviceGammaRamp(GetDC(IntPtr.Zero), usrRamp)
IsLoaded = True
End Sub
Code:Private Sub TrackBar2_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar2.ValueChanged
If IsLoaded = False Then Exit Sub
SetGamma(TrackBar2.Value)
End Sub
EDIT: Oh yeah, I noticed that it doesn't matter what device context you pass.
Any works so far, although I prefer to use GetWindowDC(GetDeskTopWindow) which is at least supposed to be the whole desktop window.
{BATTERY LOW SOLUTION XP/Vista/7} {VB6}
Full slider dimmer solution shown later in this thread.
I brought in the structure in to my example, so that it would work on Vista/7. Great it works!
I did however notice a difference in the look between the two dimmings.
Here is an updated example, that toggle dims, without a slider.
It looks more like a laptop on battery power(the whites are dimmer!), but without the ability to adjust. {To come later}
Code:Private usrRamp As New RAMP
Private Structure RAMP
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Red As UShort()
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Green As UShort()
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Blue As UShort()
End Structure
Private Declare Function apiGetDeviceGammaRamp Lib "gdi32" Alias "GetDeviceGammaRamp" (ByVal hdc As Int32, ByRef lpv As RAMP) As Int32
Private Declare Function apiSetDeviceGammaRamp Lib "gdi32" Alias "SetDeviceGammaRamp" (ByVal hdc As Int32, ByRef lpv As RAMP) 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 sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
apiGetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), usrRamp)
End Sub
Private Sub Form1_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), usrRamp)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
HalfBright()
End Sub
Private Function HalfBright() As Int32
Dim rmp As New RAMP
apiGetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), rmp)
For iCtr As UShort = 0 To 255
rmp.Red(iCtr) = CUShort(rmp.Red(iCtr) / 2)
rmp.Green(iCtr) = CUShort(rmp.Green(iCtr) / 2)
rmp.Blue(iCtr) = CUShort(rmp.Blue(iCtr) / 2)
Next
Return apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), rmp)
End Function
Yeah, that till bothers me - it will probably keep on bothering me until I'm 60! Perhaps the whole problem was how I stored the retur value of GetDeviceGammaRamp. There is a change in one of the parameters' type as well, so that is more likely to have been the issue. I'm to scared to change it to any other datattype though! LOL! :blush: Maybe I should quickly have a look and see what effect it will have :)
Never realised that, guess I had tunnel vision :D
Good work! :thumb:
Aha! I think the real problem(s) were :
1) As TT(n) pointed out, the second parameter of GetDeviceGammaRamp was supposed to be ByRef and not ByVal
2) The data type of the second parameter shouldn't have been Int32 but RAMP instead. - How could I miss that???? :D
If I do this :
I modified my original code.Code:<DllImport("gdi32.dll")> _
Private Shared Function GetDeviceGammaRamp(ByVal hdc As Int32, ByRef lpv As RAMP) As Int32
End Function
It also works. TT(n) do you think my observations are correct? Do you agree?
Hannes
Absolutely. On xp, short works, but on Vista/7 you must pass the structure, which by default tends to be ByRef, especially if you expect information back out of it.
EDIT:
Moreover, that's probably why vb6 works on Vista/7 without the structure, because I don't believe it existed.
Sorry to keep this thread going on forever, but...
I've found that the slider on lowest does not dim the whites as much as the toggle HalfDim method.
I brought both examples into my current project, and can flip between them quickly, and they are clearly different.
I don't know how to put a slider on that yet, so that the whole picture dims a grey, blocking out the whites. Oh well, all in good time.
EDIT:
AH_HA, you can use both at the same time to get the desired effect of both, slider, and dimness of the whites.
.
Okay that's it, here is a final example with both methods discussed here, combined. The range is cut in half, so values 22-44 are still valid.
Code:Private Structure RAMP
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Red As UShort()
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Green As UShort()
<System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)> _
Public Blue As UShort()
End Structure
Private Declare Function apiGetDeviceGammaRamp Lib "gdi32" Alias "GetDeviceGammaRamp" (ByVal hdc As Int32, ByRef lpv As RAMP) As Int32
Private Declare Function apiSetDeviceGammaRamp Lib "gdi32" Alias "SetDeviceGammaRamp" (ByVal hdc As Int32, ByRef lpv As RAMP) 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 newRamp As New RAMP()
Private usrRamp As New RAMP()
Private IsLoaded As Boolean
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TrackBar1.Minimum = 1000 : TrackBar1.Maximum = 2000 'Set trackbar to valid range, since if will be half, the lower range is invalid
TrackBar2.Minimum = 25 : TrackBar2.Maximum = 44
apiGetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), usrRamp)
IsLoaded = True
End Sub
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), usrRamp)
End Sub
Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
If IsLoaded = False Then Exit Sub
DesktopBrightnessContrast(TrackBar1.Value, 44 - TrackBar2.Value + 3)
Label2.Text = "Dim 1/" & (TrackBar1.Value / 1000).ToString
End Sub
Private Sub TrackBar2_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar2.ValueChanged
If IsLoaded = False Then Exit Sub
DesktopBrightnessContrast(TrackBar1.Value, 44 - TrackBar2.Value + 3)
Label3.Text = "Contrast " & TrackBar2.Value.ToString
End Sub
Private Function DesktopBrightnessContrast(ByVal bLevel As Int32, ByVal gamma As Int32) As Int32
newRamp.Red = New UShort(255) {} : newRamp.Green = New UShort(255) {} : newRamp.Blue = New UShort(255) {}
For i As Int32 = 1 To 255 ' gamma is a value between 3 and 44
newRamp.Red(i) = InlineAssignHelper(newRamp.Green(i), InlineAssignHelper(newRamp.Blue(i), CUShort((Math.Min(65535, Math.Max(0, Math.Pow((i + 1) / 256.0R, gamma * 0.1) * 65535 + 0.5))))))
Next
For iCtr As UShort = 0 To 255
newRamp.Red(iCtr) = CUShort(newRamp.Red(iCtr) / CDbl(bLevel / 1000))
newRamp.Green(iCtr) = CUShort(newRamp.Green(iCtr) / CDbl(bLevel / 1000))
newRamp.Blue(iCtr) = CUShort(newRamp.Blue(iCtr) / CDbl(bLevel / 1000))
Next
Return apiSetDeviceGammaRamp(apiGetWindowDC(apiGetDesktopWindow), newRamp) ' Now set the value.
End Function
Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
target = value : InlineAssignHelper = value
End Function
This thread can keep going on, I do not mind :D
Good work dude! :thumb:
I did notice a great deal of flickering when changing the trackbar though. Did you get the same results?
Oh, and I decided to change this thread's title to Monitor brighness - if you don't mind stin. I'm just trying to help people with this same issue to find this excellent thread more easily :)
Hannes
And, I think it is now safe to mark the thread Resolved again..
:thumb:Nice, the thread name matches now!
I did get some flicker, and could not resolve it in the current state with two separate functions. I've been busy, but I might spend a little more time on it today.
You should be able to blend them directly in the SliderGamma, but I can't really understand what you've got going on, in the InlineAssignHelper nesting.
If you know a little more about it, then perhaps you can clear up the flicker.
Or better yet we get the half dim method to slide too if possible.
It really does drop the whites, while the slidergamma brightens or dims the darks. Do you agree HanneS? Stin, do you?
If this program is to be on a boat then a dimming slider for whites is exactly what is needed.
The good thing about SliderGamma is that it can brighten the darks, which can help keep the darks visible, when it's dimmed. So they are both great together, in the proper configuration.
EDIT:
So what I mean, is that having two independent adjustments, would avoid the flicker alltogether.
I'm really stoked about trying this out.
It was pretty simple to get rid of the flicker.
I'm gonna try and put a slider on the HalfBright function, and I'll post it if I get it.
I updated the code in this thread above.
EDIT: Slider working for dimmer
Project zip file on post #63
It was also simple to put a slider on the dimmer!
Hey guys, does this look smooth or what? Both dimmer and contrast now!
Full example Project zip file on post #63
EDIT: SMOOTHER dimmer now, set in thousands-th, is a double valid 1-2.
Perfect! That is awesome! Great work! :thumb: :thumb:
Hi all, I have never used VB .net before, but what I wanted to do was use some of this code to control monitor backlight with my C++ program. Does the code I see uploaded above in SetMonitorBrightness.zip output a .exe that allows me to set monitor brightness? If so, I could simply call that .exe in my C++ program and use it that way, correct?
Also, does this solution control the LCD backlight, or just image brightness on the screen?
Hmm, it's alive again...
Have you tried the sample, and see what it does? ;)
This thread is 1 year old, it is actually best to start a new thread and refer to this from your new thread....