-->>I hope all is well,I'm developing an application that stimulates the Computer monitor and so I have lots of buttons more likly as on my Monitor here.
-->>I got stuck when I added a Slider on my Vitual Monitor,this is for adjusting Brightness on the Monitor whether high or low...
-->>Most of the buttons like shutdown,sleep,hibernate I've used the batch files and so I just execute them when the buttons are clicked by using a common dialog control...
-->>If any one knows how to code this please help as I dont even have a clue where to start...
-->>Thanks in advance.
Bile 10 Newbie Poster
AndreRet 526 Senior Poster
Being a virtual monitor I presume that you are using a picture box as your monitor....
You can use the following code to dim the brightness...
Add a picture box (Picture1, a command button (Command1) and a textbox to the form. Set the picture box ScaleMode to 3-Pixels, and AutoRedraw to True. Add the following code to the form:
Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function SetPixelV Lib "gdi32" _
(ByVal hDC As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal crColor As Long) As Byte
Private Declare Function GetPixel Lib "gdi32" _
(ByVal hDC As Long, _
ByVal x As Long, _
ByVal y As Long) As Long
Private Sub Command1_Click()
'variables for brightness, colour calculation, positioning
Dim Brightness As Single
Dim NewColour As Long
Dim pixHdc As Long
Dim x As Long, y As Long
Dim r As Integer, g As Integer, b As Integer
'change the brightness to a percent
Brightness = CSng(Val(Text1.Text) / 100)
pixHdc = Picture1.hDC
'run a loop through the picture to change every pixel
For x = 0 To Picture1.ScaleWidth
For y = 0 To Picture1.ScaleHeight
'get the current colour value
NewColour = GetPixel(pixHdc , x, y)
'extract the R,G,B values from the long returned by GetPixel
r = (NewColour Mod 256)
b = (Int(NewColour \ 65536))
g = ((NewColour - (b * 65536) - r) \ 256)
'change the RGB settings to their appropriate brightness
r = r * Brightness
b = b * Brightness
g = g * Brightness
'make sure the new variables aren't too high or too low
If r > 255 Then r = 255
If r < 0 Then r = 0
If b > 255 Then b = 255
If b < 0 Then b = 0
If g > 255 Then g = 255
If g < 0 Then g = 0
'set the new pixel
SetPixelV pixHdc, x, y, RGB(r, g, b)
'continue through the loop
Next y
'refresh the picture box
'(a nice visual progress effect)
Picture1.Refresh
Next x
'final picture refresh
Picture1.Refresh
End Sub
If you're using a slower machine, you may want to reduce the number of times the picture box updates by using this line instead of the Refresh line above:
If x MOD 10 Then Picture1.Refresh
Bile 10 Newbie Poster
-->>Thank you Andre for your reply I tested the code and sure it was pretty good but the thing is may be I was not so clear enough...
-->>What I mean is that I want the Actusl Monitor to be dim and not the control on my form as to the fact I did'nt use any control on my form I just used the bare form and the slider to do the adjustments of the brightness...
-->>At first place I used the same technique of making the form color to be high or low with the following codes
ON THE MODULE LEVEL I HAVE THIS CODE:
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bDefaut As Byte, ByVal dwFlags As Long) As Long
Private Const GWL_EXSTYLE As Long = (-20)
Private Const LWA_COLORKEY As Long = &H1
Private Const LWA_Defaut As Long = &H2
Private Const WS_EX_LAYERED As Long = &H80000
Public Function Transparency(ByVal hWnd As Long, Optional ByVal Col As Long = vbBlack, Optional ByVal PcTransp As Byte = 255, Optional ByVal TrMode As Boolean = True) As Boolean
'************************************************************'
'* RETURN : TRUE IF THERE IS NO ERROR *'
'* HWND : HWND OF THE WINDOW TO MAKE TRANSPARENT *'
'* COL : COLOR TO MAKE TRANSPARENT IF TRMODE=FALSE *'
'* PCTRANSP : 0 À 255 >> 0 = TRANSPARENT -:- 255 = OPAQUE *'
'************************************************************'
Dim DisplayStyle As Long
On Error GoTo err1
VoirStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
If DisplayStyle <> (DisplayStyle Or WS_EX_LAYERED) Then
DisplayStyle = (DisplayStyle Or WS_EX_LAYERED)
Call SetWindowLong(hWnd, GWL_EXSTYLE, DisplayStyle)
End If
Transparency = (SetLayeredWindowAttributes(hWnd, Col, PcTransp, IIf(TrMode, LWA_COLORKEY Or LWA_Defaut, LWA_COLORKEY)) <> 0)
err1:
If Not Err.Number = 0 Then Err.Clear
End Function
Public Sub ActiveTransparency(M As Form, d As Boolean, F As Boolean, _
Transparency_Value As Integer, Optional Color As Long)
Dim B As Boolean
If d And F Then
'********************************************************************'
'* MAKES COLOR (HERE THE BACKGROUND COLOR OF THE SHAPE) TRANSPARENT *'
'* UPON VALUE OF TRANSPARENCY VALUE *'
'********************************************************************'
B = Transparency(M.hWnd, Color, T_Transparency_Value, False)
ElseIf d Then
'*****************************************************'
'* MAKES FORM, INCLUDING ALL COMPONENTS, TRANSPARENT *'
'* UPON VALUE OF T_TRANSPARENCY *'
'*****************************************************'
B = Transparency(M.hWnd, 0, Transparency_Value, True)
Else
'*****************************'
'* RESTORES THE FORM OPAQUE. *'
'*****************************'
B = Transparency(M.hWnd, , 255, True)
End If
End Sub
AND ON MY FORM THE SLIDER CONTROL HAS THIS:
Private Sub Slider_Scroll()
Dim Strength As Integer
Dim B As Boolean
For Strength = 0 To Slider.Value
B = Transparency(Me.hWnd, 0, Strength, True)
Next Strength
End Sub
-->>As you can see I also just increse and decrese the color strength of the Form,but I want the slider to reduce my actual Monitor Brightness as the slider is being adjusted to low or high!!!
-->>I hope I explained my self well now...
-->>Thank you for your efforts.
AndreRet 526 Senior Poster
If it is just the form's transparency you want to set, do the following in your form...
Private Const WS_EX_LAYERED = &H80000
Private Const GWL_EXSTYLE = (-20)
Private Const LWA_COLORKEY = &H1
Private Const LWA_ALPHA = &H2
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crColor As Long, ByVal nAlpha As Byte, ByVal dwFlags As Long) As Long
Private Sub Form_Load()
' enable layered window style
Call SetWindowLong(Me.hwnd, GWL_EXSTYLE, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
' set magenta to transparent
' set rest of window to 50% blend with the background
Call SetLayeredWindowAttributes(Me.hwnd, RGB(255, 0, 255), 128, LWA_ALPHA Or LWA_COLORKEY)
End Sub
In the last line of code, use your slider to change the value 128 to whatever value you returned from the slider. 255 will be non transparent, 128 is 50% and 0 is fully transparent.
To get the "gray/black/darkened effect, create another form with no borders and its background colour set to whatever dark colour you require. load this form FIRST, then load your transparent form. this will create the effect of a darkened monitor.
Bile 10 Newbie Poster
-->>Thank you Andre but the thing is lets nit think of ways of manipulating the controls in my application by setting up their colors to make it for a control to appear as dim or bright...
-->>I also did the same thing in my application but now I was thinking if there is a way of reaching the actual brightness of the screen by code..!
-->>As to my Computer I do press Fn-Button and a Button of the Down Arrow havung a Drawing such as a Sun to reduce the screen Brightness...
-->>So what I need is to have some code to which when I slide my "SLIDER" control on my form it did the same thing by actual redusing my Screen Brightness...
-->>I hope now its some how explained a bit...
-->>Thanks again.
AndreRet 526 Senior Poster
That is quite involved by making use of API's. I found this code I worked on a few years back, You will have to play with it and if you need any explanation on what a certain piece of code does, you will have to search google for it. The below code will dim the brightness etc...
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)
''This should set the brightness back to normal when user closes app...
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
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.