| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 434 人关注过本帖
标题:很烦呀,送点代码给大家好好研究!
只看楼主 加入收藏
haolongo
Rank: 2
等 级:论坛游民
帖 子:6
专家分:39
注 册:2013-1-19
结帖率:0
收藏
已结贴  问题点数:1 回复次数:1 
很烦呀,送点代码给大家好好研究!
change_brightness.zip (12.04 KB)
Changing the screen brightness programmingly - By using the Gamma Ramp API  
Some video cards allows you to programmingly modify the Gamma Ramp values. You can use this feature to change the brightness of the entire screen.
The SetDeviceGammaRamp API function receive an array of 256 RGB values. Increasing the values in this array will make your screen brighter, and decreasing these values will make your screen darker. You can also increase or decrease the intesity of the red/green/blue components. For example: Increasing the blue component in all RGB values will add more blue color to the entire display.

The following class encapsulates the calls to GetDeviceGammaRamp and SetDeviceGammaRamp, and provides the SetBrightness function for setting the brightness of the screen.

Class header file: (gammaramp.h) #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.


*/

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);

};
#endif

 


Class C++ File: (gammaramp.cpp) #include <windows.h>
#include "gammaramp.h"


/*
CGammaRamp class

Encapsulates the Gamma Ramp API and changes the brightness of
the entire screen.

Written by Nir Sofer.
http://www.


*/

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;
}

 


Example for using the CGammaRamp class: #include <windows.h>
#include "gammaramp.h"


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.

    CGammaRamp GammaRamp;

    //Make the screen darker:
    GammaRamp.SetBrightness(NULL, 64);

    //Wait 3 seconds:
    Sleep(3000);

    //Return back to normal:
    GammaRamp.SetBrightness(NULL, 128);

    return 0;
}


 

                                    //大家如果喜欢请支持一下,如果不好请大家提点意见。
搜索更多相关主题的帖子: receive function decrease increase change 
2013-01-28 19:30
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:1 
太长,无有兴趣

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-01-29 18:56
快速回复:很烦呀,送点代码给大家好好研究!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012840 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved