升级后,API 调用失败了,从 VB6 -> VB.net 2005 Express
原来的一段 VB6 的代码,已经可以成功运行了,用来调解屏幕亮度的,现在升级到了VB.net 里,程序在SaveGamma里的GetDeviceGammaRamp处,就结束了,搞不清怎么回事。
我不知道是不是还有些VB6语言的内容,vb.net 不支持的 :( 请大家帮帮忙
VB 的代码:
Option Explicit
'Gamma Type
Private Type Gamma
Red As Integer
Green As Integer
Blue As Integer
End Type
Private GammaDay As Gamma 'Day Gamma
Private GammaNight As Gamma 'Night Gamma
'Gamma APIs
Private Ramp1(0 To 255, 0 To 2) As Integer
Private Ramp2(0 To 255, 0 To 2) As Integer
Private Declare Function GetDeviceGammaRamp Lib "gdi32" (ByVal hdc As Long, lpv As Any) As Long
Private Declare Function SetDeviceGammaRamp Lib "gdi32" (ByVal hdc As Long, lpv As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
'Set Gamma
Public Sub SetGamma(ByVal intRed As Integer, ByVal intGreen As Integer, ByVal intBlue As Integer)
Dim i As Integer
Dim ScrDC As Long
'Get Screen's DC
ScrDC = GetDC(GetDesktopWindow)
intRed = intRed / 2
intGreen = intGreen / 2
intBlue = intBlue / 2
'Change Ramp
For i = 0 To 255
If intRed < 0 Then Ramp2(i, 0) = ConvToSignedValue(ConvToUnSignedValue(Ramp1(i, 0)) * (100 - Abs(intRed)) / 100)
If intRed = 0 Then Ramp2(i, 0) = Ramp1(i, 0)
If intRed > 0 Then Ramp2(i, 0) = ConvToSignedValue(65535 - ((65535 - ConvToUnSignedValue(Ramp1(i, 0))) * (100 - intRed) / 100))
If intGreen < 0 Then Ramp2(i, 1) = ConvToSignedValue(ConvToUnSignedValue(Ramp1(i, 1)) * (100 - Abs(intGreen)) / 100)
If intGreen = 0 Then Ramp2(i, 1) = Ramp1(i, 1)
If intGreen > 0 Then Ramp2(i, 1) = ConvToSignedValue(65535 - ((65535 - ConvToUnSignedValue(Ramp1(i, 1))) * (100 - intGreen) / 100))
If intBlue < 0 Then Ramp2(i, 2) = ConvToSignedValue(ConvToUnSignedValue(Ramp1(i, 2)) * (100 - Abs(intBlue)) / 100)
If intBlue = 0 Then Ramp2(i, 2) = Ramp1(i, 2)
If intBlue > 0 Then Ramp2(i, 2) = ConvToSignedValue(65535 - ((65535 - ConvToUnSignedValue(Ramp1(i, 2))) * (100 - intBlue) / 100))
Next
'Set Gamma
SetDeviceGammaRamp ScrDC, Ramp2(0, 0)
End Sub
'Save Gamma
Public Sub SaveGamma()
Dim ScrDC As Long
'Get Screen's DC
ScrDC = GetDC(GetDesktopWindow)
'Reset it
GetDeviceGammaRamp ScrDC, Ramp1(0, 0)
End Sub
'RestoreGamma
Public Sub RestoreGamma()
Dim ScrDC As Long
'Get Screen's DC
ScrDC = GetDC(GetDesktopWindow)
'Reset it
SetDeviceGammaRamp ScrDC, Ramp1(0, 0)
End Sub
Private Function ConvToSignedValue(lngValue As Long) As Integer
'Cheezy method for converting to signed integer
If lngValue <= 32767 Then
ConvToSignedValue = CInt(lngValue)
Exit Function
End If
ConvToSignedValue = CInt(lngValue - 65535)
End Function
Private Function ConvToUnSignedValue(intValue As Integer) As Long
'Cheezy method for converting to unsigned integer
If intValue >= 0 Then
ConvToUnSignedValue = intValue
Exit Function
End If
ConvToUnSignedValue = intValue + 65535
End Function
Private Sub Command1_Click()
GammaDay.Red = 0
GammaDay.Green = 0
GammaDay.Blue = 0
SetGamma GammaDay.Red, GammaDay.Green, GammaDay.Blue
End Sub
Private Sub Command2_Click()
GammaNight.Red = -64
GammaNight.Green = -64
GammaNight.Blue = -64
SetGamma GammaNight.Red, GammaNight.Green, GammaNight.Blue
End Sub
Private Sub Form_Load()
SaveGamma
End Sub
Private Sub Form_Unload(Cancel As Integer)
RestoreGamma
End Sub