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.
Last edited by TT(n); June 26th, 2010 at 02:31 AM.
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.
.
Last edited by TT(n); June 26th, 2010 at 06:24 PM.
Reason: Dim slider
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
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
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.
Last edited by TT(n); June 30th, 2010 at 01:02 AM.
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?
* 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.