Results 1 to 23 of 23

Thread: conver Visual C++ 6.0 improve Brightness code to vb6

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    conver Visual C++ 6.0 improve Brightness code to vb6

    Hi all.
    I have the following code in Visual C++ 6.0.
    to dim the screen.
    I know there is wmi but I use a TV that doesn't work.
    This code works fine but I would like to be able to make the screen darker.
    It could be converted to vb6.
    I tried it but I didn't get the required effect.


    GammaRamp.cpp
    Code:
    #include <windows.h>
    #include "gammaramp.h"
    #include <cmath>
    
    /*
    CGammaRamp class
    
    Encapsulates the Gamma Ramp API and changes the brightness of 
    the entire screen.
    
    Written by Nir Sofer.
    http://www.nirsoft.net
    
    
    */
    
    CGammaRamp::CGammaRamp()
    {
    	//Initialize all variables.
    	hGDI32 = NULL;
    	hScreenDC = NULL;
    	pGetDeviceGammaRamp = NULL;
    	pSetDeviceGammaRamp = NULL;
    }
    
    
    CGammaRamp::~CGammaRamp()
    {
    	FreeLibrary();
    }
    
    
    BOOL CGammaRamp::LoadLibrary()
    {
    	BOOL bReturn = FALSE;
    
    	FreeLibrary();
    	//Load the GDI library.
    	hGDI32 = ::LoadLibrary("gdi32.dll");
    	if (hGDI32 != NULL)
    	{
    		//Get the addresses of GetDeviceGammaRamp and SetDeviceGammaRamp API functions.
    		pGetDeviceGammaRamp = (Type_SetDeviceGammaRamp)GetProcAddress(hGDI32, "GetDeviceGammaRamp");
    		pSetDeviceGammaRamp = (Type_SetDeviceGammaRamp)GetProcAddress(hGDI32, "SetDeviceGammaRamp");
    		
    		//Return TRUE only if these functions exist.
    		if (pGetDeviceGammaRamp == NULL || pSetDeviceGammaRamp == NULL)
    			FreeLibrary();
    		else
    			bReturn = TRUE;
    	}
    
    	return bReturn;
    }
    
    
    void CGammaRamp::FreeLibrary()
    {
    	//Free the GDI library.
    	if (hGDI32 != NULL) 
    	{
    		::FreeLibrary(hGDI32);
    		hGDI32 = NULL;
    	}
    }
    
    
    BOOL CGammaRamp::LoadLibraryIfNeeded()
    {
    	BOOL bReturn = FALSE;
    
    	if (hGDI32 == NULL)
    		LoadLibrary();
    
    	if (pGetDeviceGammaRamp != NULL && pSetDeviceGammaRamp != NULL)
    		bReturn = TRUE;
    
    	return bReturn;
    }
    
    
    BOOL CGammaRamp::SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
    {
    	//Call to SetDeviceGammaRamp only if this function is successfully loaded.
    	if (LoadLibraryIfNeeded())
    	{
    		return pSetDeviceGammaRamp(hDC, lpRamp);
    	}
    	else
    		return FALSE;
    }
    
    
    BOOL CGammaRamp::GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
    {
    	//Call to GetDeviceGammaRamp only if this function is successfully loaded.
    	if (LoadLibraryIfNeeded())
    	{
    		return pGetDeviceGammaRamp(hDC, lpRamp);
    	}
    	else
    		return FALSE;
    
    }
    
    
    BOOL CGammaRamp::SetBrightness(HDC hDC, WORD wBrightness)
    {
    	/*
    	Changes the brightness of the entire screen.
    	This function may not work properly in some video cards.
    
    	The wBrightness value should be a number between 0 and 255.
    	128 = Regular brightness
    	above 128 = brighter
    	below 128 = darker
    
        If hDC is NULL, SetBrightness automatically load and release 
    	the display device context for you.
    
    	*/
    	BOOL bReturn = FALSE;
    	HDC hGammaDC = hDC;
    
    	//Load the display device context of the entire screen if hDC is NULL.
    	if (hDC == NULL)
    		hGammaDC = GetDC(NULL);
    
    	if (hGammaDC != NULL)
    	{
    		//Generate the 256-colors array for the specified wBrightness value.
    		WORD GammaArray[3][256];
    
    		for (int iIndex = 0; iIndex < 256; iIndex++)
    		{
    			int iArrayValue = iIndex * (wBrightness + 128);
    
    			if (iArrayValue > 65535)
    				iArrayValue = 65535;
    
    			GammaArray[0][iIndex] = 
    			GammaArray[1][iIndex] = 
    			GammaArray[2][iIndex] = (WORD)iArrayValue;
    			
    		}
    
    		//Set the GammaArray values into the display device context.
    		bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
    	}
    
    	if (hDC == NULL)
    		ReleaseDC(NULL, hGammaDC);
    
    	return bReturn;
    }
    
    
    BOOL CGammaRamp::SetBrightness2(HDC hDC, WORD wBrightness, float fContrast)
    {
    	/*
    	Changes the brightness of the entire screen.
    	This function may not work properly in some video cards.
    
    	The wBrightness value should be a number between 0 and 255.
    	128 = Regular brightness
    	above 128 = brighter
    	below 128 = darker
    
        If hDC is NULL, SetBrightness automatically load and release 
    	the display device context for you.
    
    	*/
    	BOOL bReturn = FALSE;
    	HDC hGammaDC = hDC;
    
    	//Load the display device context of the entire screen if hDC is NULL.
    	if (hDC == NULL)
    		hGammaDC = GetDC(NULL);
    
    	if (hGammaDC != NULL)
    	{
    		//Generate the 256-colors array for the specified wBrightness value.
    		WORD GammaArray[3][256];
    
    		for (int iIndex = 0; iIndex < 256; iIndex++)
    		{
                int iArrayValue = iIndex * (wBrightness + 128) - 32768;
    
                // Ajusta el valor para el contraste
                iArrayValue = (int)(iArrayValue * fContrast) + 32768;
    
                // Asegura que el valor esté en el rango permitido
                if (iArrayValue < 0)
                    iArrayValue = 0;
                else if (iArrayValue > 65535)
                    iArrayValue = 65535;
    
                GammaArray[0][iIndex] =
                    GammaArray[1][iIndex] =
                    GammaArray[2][iIndex] = (WORD)iArrayValue;
    			
    		}
    
    		//Set the GammaArray values into the display device context.
    		bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
    	}
    
    	if (hDC == NULL)
    		ReleaseDC(NULL, hGammaDC);
    
    	return bReturn;
    }
    
    BOOL CGammaRamp::SetBrightness3(HDC hDC, WORD wBrightness)
    {
        BOOL bReturn = FALSE;
        HDC hGammaDC = hDC;
    
        // Load the display device context of the entire screen if hDC is NULL.
        if (hDC == NULL)
            hGammaDC = GetDC(NULL);
    
        if (hGammaDC != NULL)
        {
            // Generate the 256-colors array for the specified wBrightness value.
            WORD GammaArray[3][256];
    
            for (int iIndex = 0; iIndex < 256; iIndex++)
            {
                int iArrayValue = iIndex * (wBrightness + 128);
    
                if (iArrayValue > 65535)
                    iArrayValue = 65535;
    
                // Adjusting not only brightness but also making the color darker (towards black).
    			GammaArray[0][iIndex] =
    			GammaArray[1][iIndex] =
    			GammaArray[2][iIndex] = static_cast<WORD>((iArrayValue - 32768) > 0 ? (iArrayValue - 32768) / 257 : 0);  // Adjust towards black
            }
    									//(WORD)iArrayValue;
            // Set the GammaArray values into the display device context.
            bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
        }
    
        if (hDC == NULL)
            ReleaseDC(NULL, hGammaDC);
    
        return bReturn;
    }
    
    
    BOOL CGammaRamp::SetDimmerOverlay(HDC hDC, int dimmerOpacity)
    {
    	BOOL bReturn = FALSE;
        HDC hGammaDC = GetDC(NULL);
    
        if (hGammaDC != NULL)
        {
            // Generate the 256-colors array for darkening the screen.
            WORD GammaArray[3][256];
    
            for (int iIndex = 0; iIndex < 256; iIndex++)
            {
                // Set all color channels to a darkened value based on dimmer opacity
                int iArrayValue = iIndex * dimmerOpacity / 100;
    
                GammaArray[0][iIndex] =
                    GammaArray[1][iIndex] =
                    GammaArray[2][iIndex] = (WORD)iArrayValue;
            }
    
            // Set the GammaArray values into the display device context.
            bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
        }
    
        // Release the device context
        ReleaseDC(NULL, hGammaDC);
    
    	return bReturn;
    }
    
    BOOL CGammaRamp::SetBrightness4(HDC hDC, WORD wBrightness, float fContrast)
    {
        /*
        Changes the brightness and contrast of the entire screen.
        This function may not work properly in some video cards.
    
        The wBrightness value should be a number between 0 and 255.
        128 = Regular brightness
        above 128 = brighter
        below 128 = darker
    
        If hDC is NULL, SetBrightness automatically loads and releases 
        the display device context for you.
        */
        BOOL bReturn = FALSE;
        HDC hGammaDC = hDC;
    
        // Load the display device context of the entire screen if hDC is NULL.
        if (hDC == NULL)
            hGammaDC = GetDC(NULL);
    
        if (hGammaDC != NULL)
        {
            // Generate the 256-colors array for the specified wBrightness value.
            WORD GammaArray[3][256];
    
            for (int iIndex = 0; iIndex < 256; iIndex++)
            {
                int iArrayValue = iIndex * (wBrightness + 128) - 32768;
    
                // Adjust the value for contrast
                iArrayValue = (int)(iArrayValue * fContrast) + 32768;
    
                // Reduce brightness (make it darker) even when wBrightness > 0
                iArrayValue = (int)(iArrayValue * 0.8);  // You can adjust the factor for further darkness
    
                // Ensure that the value is in the allowed range
                if (iArrayValue < 0)
                    iArrayValue = 0;
                else if (iArrayValue > 65535)
                    iArrayValue = 65535;
    
                GammaArray[0][iIndex] =
                    GammaArray[1][iIndex] =
                    GammaArray[2][iIndex] = (WORD)iArrayValue;
            }
    
            // Set the GammaArray values into the display device context.
            bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
        }
    
        if (hDC == NULL)
            ReleaseDC(NULL, hGammaDC);
    
        return bReturn;
    }
    GammaRamp.h
    Code:
    #ifndef GAMMARAMP_H_
    #define GAMMARAMP_H_
    
    
    /*
    CGammaRamp class
    
    Encapsulates the Gamma Ramp API and changes the brightness of 
    the entire screen.
    
    Written by Nir Sofer.
    http://www.nirsoft.net
    
    
    */
    
    class CGammaRamp
    {
    protected:
    	HMODULE hGDI32;
    	HDC hScreenDC;
    	typedef BOOL (WINAPI *Type_SetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);
    
    	Type_SetDeviceGammaRamp pGetDeviceGammaRamp;
    	Type_SetDeviceGammaRamp pSetDeviceGammaRamp;
    
    public:
    
    	CGammaRamp();
    	~CGammaRamp();
    	BOOL LoadLibrary();
    	void FreeLibrary();
    	BOOL LoadLibraryIfNeeded();
    	BOOL SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);
    	BOOL GetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);
    	BOOL SetBrightness(HDC hDC, WORD wBrightness);
    	BOOL SetBrightness2(HDC hDC, WORD wBrightness, float fContrast);
    	BOOL SetBrightness3(HDC hDC, WORD wBrightness);
    	BOOL SetBrightness4(HDC hDC, WORD wBrightness, float fContrast);
    	BOOL SetDimmerOverlay(HDC hDC, int dimmerOpacity);
    };
    #endif
    main.cpp
    Code:
    #include <windows.h>
    #include "gammaramp.h"
    #include "windows.h"
    #include <shellapi.h>
    #include <stdio.h>
    #include <process.h>
    #include <tchar.h>
    #include <iostream>
    #include <cstdlib>
    
    
    int WINAPI WinMain(
      HINSTANCE hInstance,      // handle to current instance
      HINSTANCE hPrevInstance,  // handle to previous instance
      LPSTR lpCmdLine,          // command line
      int nCmdShow              // show state
    )
    {
    	//Example for changing the brightness with CGammaRamp class:
    	//Be aware that this exmaple may not work properly in all
    	//Video cards.
        int argc;
        LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
        if (argv == NULL) {
            wprintf(L"CommandLineToArgvW failed\n");
            return 1;
        }
        for (int i = 0; i < argc; i++) {
            wprintf(L"Argument %d: %s\n", i, argv[i]);
        }
    
    	int number = _wtoi(argv[1]);
        // Don't forget to free the allocated memory
        LocalFree(argv);
    	int start = 5;
    
    	std::cout << "Another instance is already running." << std::endl;
    	CGammaRamp GammaRamp;
    
    
            GammaRamp.SetBrightness(NULL, number);
    	return 0;
    }
    Greetings, thank you

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,081

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Quote Originally Posted by yokesee View Post
    Hi all.
    I have the following code in Visual C++ 6.0.
    to dim the screen.
    I know there is wmi but I use a TV that doesn't work.
    This code works fine but I would like to be able to make the screen darker.
    It could be converted to vb6.
    I tried it but I didn't get the required effect.
    Why not post the converted vb6 code that you tried? Maybe it's 98% correct, and someone can simply post a fix for your existing code, rather than expecting people to go through and convert all the code for you.

  3. #3
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,515

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Already had the APIs in tbShellLib:

    Code:
    Public Declare Function GetDeviceGammaRamp Lib "gdi32" (ByVal hdc As LongPtr, lpRamp As Any) As BOOL
    Public Declare Function SetDeviceGammaRamp Lib "gdi32" (ByVal hdc As LongPtr, lpRamp As Any) As BOOL
    Public Declare Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
    Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As LongPtr, ByVal hDC As LongPtr) As Long
    The key here is that GammaArray[3][256] is not equivalent to GammaArray(2, 255) like you'd think. VB orders multidimensional arrays differently. Maybe someone else knows offhand if not I'll recalculate the mapping tonight, don't have time right now.
    Last edited by fafalone; Dec 8th, 2023 at 03:15 PM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    This is not mine.
    I can't find my modification but it looks similar
    the screen instead of darkening becomes strange
    Code:
    Option Explicit
    Private Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, ByRef lpParam As Any) As Long
    Private Declare Function DestroyWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
    
    
    Private Declare Sub CopyMemory Lib "Kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    Private Declare Function GetDeviceGammaRamp Lib "GDI32.dll" (ByVal hDc As Long, lpV As Any) As Long
    Private Declare Function SetDeviceGammaRamp Lib "GDI32.dll" (ByVal hDc As Long, lpV As Any) As Long
    Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long
    
    Private arrGammaSave(0 To 255, 0 To 2) As Integer
    Private bGammaSaved As Boolean
    Private bRestoreOnExit As Boolean
    Private MIN_POS As Long
    Private MAX_POS As Long
    Private POS_FACTOR As Long
    
    Private m_hWnd      As Long
    Public Property Let MIN(ByVal lValue As Long)
        MIN_POS = lValue
    End Property
    Public Property Let MAX(ByVal lValue As Long)
        MAX_POS = lValue
    End Property
    Public Property Let RESTORE_ON_EXIT(ByVal bValue As Boolean)
        bRestoreOnExit = bValue
    End Property
    
    Private Sub Class_Initialize()
        m_hWnd = CreateWindowEx(0, "Static", "Brightness", 0, 0, 0, 0, 0, 0, 0, App.hInstance, 0&)
        If Not GetDeviceGammaRamp(GetDC(m_hWnd), arrGammaSave(0, 0)) = 0 Then
            bGammaSaved = True
            MIN_POS = 0
            MAX_POS = 40
            POS_FACTOR = 10.01
            bRestoreOnExit = True
        Else
            bGammaSaved = False
        End If
    End Sub
    Private Sub Class_Terminate()
        DestroyWindow m_hWnd
        m_hWnd = 0
        If bRestoreOnExit = True And bGammaSaved = True Then SetDeviceGammaRamp GetDC(m_hWnd), arrGammaSave(0, 0)
    End Sub
    
    Public Sub RestoreGamma()
        If bGammaSaved = True Then SetDeviceGammaRamp GetDC(m_hWnd), arrGammaSave(0, 0)
    End Sub
    Public Function SetGamma(dValue As Double)
        If bGammaSaved = False Then Exit Function
        SetGammaPos ((MAX_POS - dValue) / POS_FACTOR)
    End Function
    Public Function GetSavedGamma() As Integer
        If bGammaSaved = False Then Exit Function
        GetSavedGamma = GetGammaPos()
    End Function
    
    Private Sub SetGammaPos(Gamma As Double)
        If bGammaSaved = False Then Exit Sub
        Dim arrRamp(0 To 255, 0 To 2) As Integer
        Dim dblTest As Double
        Dim l As Long
        Dim i As Integer
        For i = 0 To 255
            dblTest = (((i + 1) / 256.01) ^ Gamma) * 65535 + 0.5
            If dblTest > 65535 Then
                l = 65535
            Else
                l = CLng(dblTest)
            End If
            '### Set the Low Word (2 bytes).
            arrRamp(i, 0) = LOWORD(l)
            arrRamp(i, 1) = LOWORD(l)
            arrRamp(i, 2) = LOWORD(l)
        Next i
        SetDeviceGammaRamp GetDC(m_hWnd), arrRamp(0, 0)
    End Sub
    Private Function GetGammaPos() As Integer
        If bGammaSaved = False Then Exit Function
        On Error Resume Next
        Dim rgb(2) As Double
        Dim i As Integer
        Dim j As Integer
        rgb(0) = 1.01
        rgb(1) = 1.01
        rgb(2) = 1.01
        For i = 0 To 2
            Dim Csum As Double
            Dim Ccount As Integer
            Csum = 0
            Ccount = 0
            For j = 0 To 255
                If j <> 0 And arrGammaSave(j, i) <> 0 And arrGammaSave(j, i) <> 65536 Then
                    Dim a As Double
                    Dim b As Double
                    Dim c As Double
                    b = (j Mod 256) / 256.01
                    a = arrGammaSave(j, i) / 65536.01
                    c = Log(a) / Log(b)
                    Csum = Csum + c
                    Ccount = Ccount + 1
                End If
            Next j
            rgb(i) = Csum / Ccount
        Next i
        GetGammaPos = Int(MAX_POS - (rgb(0) * POS_FACTOR))
    End Function
    Private Function LOWORD(ByVal lng As Long) As Integer
        CopyMemory LOWORD, lng, 2
    End Function
    Code:
    Dim oGamma As New clsGamma
    oGamma.RESTORE_ON_EXIT = False
    oGamma.SetGamma 0 '### Value ranges from 0 (darkest) to 37.5 (brightest)

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,515

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Did you compile the C++ code to see if it still even does the same thing itself? Sometimes the old APIs break. MSDN says:

    We strongly recommend that you don't use this API. Use of this API is subject to major limitations:

    SetDeviceGammaRamp implements heuristics to check whether a provided ramp will result in an unreadable screen. If a ramp violates those heuristics, then the function fails silently (that is, it returns TRUE, but it doesn't set your ramp). For that reason, you can't expect to use this function to set just any arbitrary gamma ramp. In particular, the heuristics prevent ramps that would result in nearly all pixels approaching a single value (such as fullscreen black/white) as this may prevent a user from recovering the screen.

    Because of the function's global nature, any other application on the system could, at any time, overwrite any ramp that you've set. In some cases the operating system itself may reserve the use of this function, causing any existing ramp to be overwritten. The gamma ramp is also reset on most display events (connecting/disconnecting a monitor, resolution changes, etc.). So you can't be certain that any ramp you set is in effect.

    This API has undefined behavior in HDR modes.

    This API has undefined interaction with both built-in and third-party color calibration solutions.
    Unless you need to support Windows XP, consider:

    Public Declare Function SetMonitorBrightness Lib "dxva2.dll" (ByVal hMonitor As LongPtr, ByVal dwNewBrightness As Long) As Long

    There's several examples to get the current hMonitor here.
    Last edited by fafalone; Dec 8th, 2023 at 03:28 PM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Yes, compiler works perfectly, but I wish it could be darker

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    I already tried those SetMonitorBrightness examples.
    But the problem I have is that I use a TV monitor and the brightness cannot be changed either through wmi
    that's why I use the GammaRamp option.

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,515

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    If the c++ version works, then it seems you should insert a couple lines to output a sample the values, see where the calculation goes wrong. You'd just need to copy the std::cout lines and put the variable for the numbers in place of the string... like

    std::cout << "Value=&H" << std::hex << (int)iArrayValue << '\n';

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    This code darkens but not as much as it would like.
    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 GetDeviceGammaRamp Lib "gdi32" (ByVal hdc As Long, ByRef lpv As Any) As Long
    Private Declare Function SetDeviceGammaRamp Lib "gdi32" (ByVal hdc As Long, ByRef lpv As Any) As Long
    Private Declare Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    
    Private Sub Form_Load()
        Dim iCtr As Integer
        Dim lVal As Long
        Call GetDeviceGammaRamp(GetWindowDC(GetDesktopWindow), 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 SetDeviceGammaRamp(GetWindowDC(GetDesktopWindow), 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 SetDeviceGammaRamp(GetWindowDC(GetDesktopWindow), Ramp1(0, 0))
    End Sub
    
    Public Function Int2Lng(IntVal As Integer) As Long
        Call CopyMemory(Int2Lng, IntVal, 2)
    End Function
    
    Public Function Lng2Int(Value As Long) As Integer
        Call CopyMemory(Lng2Int, Value, 2)
    End Function
    I know it can be done because other programs in Autohotkey can do it but I don't know how to convert to vb6
    https://www.autohotkey.com/boards/viewtopic.php?t=79220

  10. #10
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,515

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    So here's my take on the original C++ function:

    Code:
    Public Enum BOOL
        CFalse
        CTrue
    End Enum
    Public Declare Function GetDeviceGammaRamp Lib "gdi32" (ByVal hdc As LongPtr, lpRamp As Any) As BOOL
    Public Declare Function SetDeviceGammaRamp Lib "gdi32" (ByVal hdc As LongPtr, lpRamp As Any) As BOOL
    Public Declare Function GetDC Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
    Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As LongPtr, ByVal hDC As LongPtr) As Long
    
    Public Type garray
        GammaArray(255, 2) As Integer
    End Type
    Public Function SetBrightness(ByVal hDC As LongPtr, ByVal wBrightness As Integer) As BOOL
        Dim hGammaDC As LongPtr: hGammaDC = hDC
        If hDC = 0 Then
            hGammaDC = GetDC(0)
        End If
        If hGammaDC Then
            Dim g As garray
            Dim iIndex As Long
            For iIndex = 0 To 255
                Dim iArrayValue As Long: iArrayValue = iIndex * (wBrightness + 128)
                If (iArrayValue > 65535) Then iArrayValue = 65535
                g.GammaArray(iIndex, 0) = CUIntToInt(iArrayValue)
                g.GammaArray(iIndex, 1) = CUIntToInt(iArrayValue)
                g.GammaArray(iIndex, 2) = CUIntToInt(iArrayValue)
            Next
            SetBrightness = SetDeviceGammaRamp(hGammaDC, ByVal VarPtr(g))
            If hDC = 0 Then ReleaseDC(0, hGammaDC)
        End If
    End Function
    Public Function CUIntToInt(ByVal Value As Long) As Integer
    'Thanks to Krool for this function
    Const OFFSET_2 As Long = 65536
    Const MAXINT_2 As Integer = 32767
    If Value < 0 Or Value >= OFFSET_2 Then Err.Raise 6
    If Value <= MAXINT_2 Then
        CUIntToInt = Value
    Else
        CUIntToInt = Value - OFFSET_2
    End If
    End Function
    If you don't have LongPtr defined (oleexp.tlb defines it, or VB6LongPtr.tlb), change to Long or put:

    Code:
    Public Enum LongPtr
        [_]
    End Enum

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Thank you very much for the code.
    It is possible to make the darkest

  12. #12
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    One question about this intressting darkening....
    Isn't this used is some apps if a dialoge or a new document should be created and the background the darkens to draw attention from the new question?
    Last edited by nebeln; Dec 9th, 2023 at 07:16 PM.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    I have seen that effect in some program, I don't remember which one.
    This is more for personal use and making life easier.
    Many hours on the PC programming, watching videos, etc. many times at night the light is annoying.
    There are programs that do the same, but if you can in vb6 better.

  14. #14
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    This affects whole the screen and not to just effected hDC as I have realized...Is there anyway to affect just the background and keep main/focused window with no darkening? (some people got issues so they can't differ or have difficulties from knowing old question from new..so if the old question becomes darkened would be nice and the new question keeps bright in focus.
    Last edited by nebeln; Dec 9th, 2023 at 07:43 PM.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Put a transparent window behind, without borders and black backcolor

  16. #16
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Probably you have have seen this when a machine goes down i "energy mode" or a elctric grid plug is dissconnected.

  17. #17
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Tried it but that doesnt work. The whole Screen is darkened even if the hDC cource is changed.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Quote Originally Posted by nebeln View Post
    Tried it but that doesnt work. The whole Screen is darkened even if the hDC cource is changed.
    try this, it's ugly and fast
    add two form
    Code:
    Option Explicit
    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal uFlags As Long) As Long
    
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const SWP_NOMOVE = &H2
    Private Const SWP_NOSIZE = &H1
    Private Const SWP_SHOWWINDOW = &H40
    
    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 bAlpha As Byte, ByVal dwFlags As Long) As Long
    
    Private Const GWL_STYLE = (-16)
    Private Const GWL_EXSTYLE = -20
    Private Const GWL_HWNDPARENT = (-8)
    Private Const WS_EX_LAYERED = &H80000
    Private Const WS_EX_TOOLWINDOW = &H80
    Private Const WS_SYSMENU = &H80000
    Private Const WS_EX_TRANSPARENT = &H20
    Private Const WS_CAPTION = &HC00000
    Private Const LWA_ALPHA = &H2
    
    
    Private overlayForm As Form
    Private Sub Form_Load()
        Set overlayForm = New Form2
        With overlayForm
            .BorderStyle = 0
            .BackColor = vbBlack
            .Width = Screen.Width
            .Height = Screen.Height
            .Left = 0
            .Top = 0
            SetWindowTransparency .hWnd, 100
            TOPMOST .hWnd
            NOTOPMOST .hWnd
        End With
        SetOwnerForm Me, overlayForm
    End Sub
    
    
    Private Sub SetWindowTransparency(ByVal hWnd As Long, ByVal transparency As Byte)
        Dim exStyle As Long
        exStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
        SetWindowLong hWnd, GWL_EXSTYLE, exStyle Or WS_EX_LAYERED
        SetLayeredWindowAttributes hWnd, RGB(0, 0, 0), transparency, LWA_ALPHA
    End Sub
    Private Sub TOPMOST(ByVal hWnd As Long)
        SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
    End Sub
    Private Sub NOTOPMOST(ByVal hWnd As Long)
        SetWindowPos hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW
    End Sub
    
    Private Sub SetOwnerForm(ByRef childForm As Form, ByRef ownerForm As Form)
        SetWindowLong childForm.hWnd, GWL_HWNDPARENT, ownerForm.hWnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Unload overlayForm
    End Sub

  19. #19
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Realy ugly and it doesn't fit my purpose really..probably I done someting wrong becuase the main window lefts traces.

  20. #20
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    No, it doesn,t fill my pupose at all...in a blink everyting went darkened and the main window left lefts traces of lights where all controls been..not just the form.

  21. #21
    Lively Member
    Join Date
    May 2021
    Posts
    94

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    I just tried the code Fafalone provided and had the same result as you. I would point out, though, that the API SetDeviceGammaRamp has a wild number of caveats to it, according to MSDN: (link)

    Important
    We strongly recommend that you don't use this API. Use of this API is subject to major limitations:
    SetDeviceGammaRamp implements heuristics to check whether a provided ramp will result in an unreadable screen. If a ramp violates those heuristics, then the function fails silently (that is, it returns TRUE, but it doesn't set your ramp). For that reason, you can't expect to use this function to set just any arbitrary gamma ramp. In particular, the heuristics prevent ramps that would result in nearly all pixels approaching a single value (such as fullscreen black/white) as this may prevent a user from recovering the screen.
    Because of the function's global nature, any other application on the system could, at any time, overwrite any ramp that you've set. In some cases the operating system itself may reserve the use of this function, causing any existing ramp to be overwritten. The gamma ramp is also reset on most display events (connecting/disconnecting a monitor, resolution changes, etc.). So you can't be certain that any ramp you set is in effect.
    This API has undefined behavior in HDR modes.
    This API has undefined interaction with both built-in and third-party color calibration solutions.
    For color calibration, we recommend that you create an International Color Consortium (ICC) profile, and let the OS apply the profile. For advanced original equipment manufacturer (OEM) scenarios, there's a device driver model that you can use to customize color calibration more directly. See the Windows Color System for information on managing color profiles.
    For blue light filtering, Windows now provides built-in support called Night Light. We recommend directing users to this feature.
    For color adaptation (for example, adjusting color calibration based on ambient light sensors), Windows now provides built-in support, which we recommend for use by OEMs.
    For custom filter effects, there are a variety of built-in accessibility color filters to help with a range of cases.
    It seems odd to me, that although passing the API a specific hDC, it nonetheless adjusts the brightness of the entire screen.

  22. #22
    Fanatic Member
    Join Date
    Mar 2023
    Posts
    708

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    Yes, it’s really odd!! The whole screen darkens.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    497

    Re: conver Visual C++ 6.0 improve Brightness code to vb6

    It works well for me
    Name:  fondo oscuro.jpg
Views: 93
Size:  22.4 KB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width