- 性别
- 保密
- 积分
- 30
- 积分
- 120
- 精华
- 1
- 阅读权限
- 20
- 注册时间
- 2013-6-21
- 最后登录
- 2013-10-22
- 帖子
- 17
- 性别
- 保密
|
作者:小许 日期:2013-10-08 原文地址:http://www.skywj.com/thread-9165-1-1.html
如何能够让电脑屏幕在调最暗后,还能继续暗下去?今天我来带领大家写一个简单的调节屏幕亮度的小程序,在这里呢,你可以学到Slider Control的用法,并且懂得如何在类中使用函数指针,因为我用类的方式封装了调节亮度的方法,这里呢,我们用到微软的dll(gdi32.dll)里面的SetDeviceGammaRamp,当然了,有Set就有Get,但是呢我们的程序不需要用到这个,所以忽略。程序运行变暗后图如下,
typedef BOOL (WINAPI *Type_SetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);
Type_SetDeviceGammaRamp pSetDeviceGammaRamp;
上面我们定义了一个函数指针,并定义了一个该指针的变量.hGDI32 = ::和谐LoadLibrary("gdi32.dll");
if(hGDI32 != NULL)
{
pSetDeviceGammaRamp = (Type_SetDeviceGammaRamp)GetProcAddress(hGDI32, "SetDeviceGammaRamp");
}
接下来获取dll中的函数地址,也就是SetDeviceGammaRamp.
接下来我们往对话框添加控件,效果图如下:
我们为滑块控件添加一个成员变量,为控件类型CSliderCtrl m_silderCtrl;
接下来呢,我们通过这个滑块来调整当i请按亮度,通过添加消息ON_WM_HSCROLL可以响应滑块的事件:
void CChangeBrightnessDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
m_cb.SetBrightness(NULL,m_silderCtrl.GetPos() * 2);
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
上面代码呢是m_cb是我们的亮度类的类变量,SetBrightness是我们设置的主要函数,他的第一个参数是一个DC,我们传NULL,我会在这个函数里面判断为NULL的话,我就会自动获取DC,第二个参数呢是亮度,默认值为128。
BOOL ChangeBrightness::SetBrightness(HDC hDC, WORD wBrightness)
{
BOOL bReturn = FALSE;
HDC hGammaDC = hDC;
if(hDC == NULL)
hGammaDC = GetDC(NULL);
if(hGammaDC != NULL)
{
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;
}
bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
}
if(hDC == NULL)
ReleaseDC(NULL, hGammaDC);
return bReturn;
}
上面的是我们的主要函数,第二个参数为屏幕的伽马值,for循环呢是计算屏幕伽马值的一个小算子。SetDeviceGammaRamp(hGammaDC, GammaArray)实现如下:
BOOL ChangeBrightness::SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
{
if(LoadLibraryIfNeeded())
return pSetDeviceGammaRamp(hDC, lpRamp);
else
return FALSE;
}
其中函数LoadLibraryIfNeeded()是判断是否需要重新加载dll,防止对指向函数地址的指针为空调用崩溃。
接下来是源代码段,其实很简单,只是算子有点复杂。- #ifndef GAMMARAMP_H_
- #define GAMMARAMP_H_
- #include <windows.h>
- class ChangeBrightness
- {
- private:
- HMODULE hGDI32;
- HDC hScreenDC;
- typedef BOOL (WINAPI *Type_SetDeviceGammaRamp)(HDC hDC, LPVOID lpRamp);
- Type_SetDeviceGammaRamp pSetDeviceGammaRamp;
- BOOL LoadLibrary();
- void FreeLibrary();
- BOOL LoadLibraryIfNeeded();
- BOOL SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp);
-
- public:
- ChangeBrightness();
- ~ChangeBrightness();
- BOOL SetBrightness(HDC hDC, WORD wBrightness);
- };
- #endif
复制代码- #include "stdafx.h"
- #include "ChangeBrightnessInfo.h"
- ChangeBrightness::ChangeBrightness()
- {
- hGDI32 = NULL;
- hScreenDC = NULL;
- pSetDeviceGammaRamp = NULL;
- }
- ChangeBrightness::~ChangeBrightness()
- {
- FreeLibrary();
- }
-
- BOOL ChangeBrightness::LoadLibrary()
- {
- BOOL bReturn = FALSE;
- FreeLibrary();
-
- hGDI32 = ::LoadLibrary("gdi32.dll");
- if(hGDI32 != NULL)
- {
- pSetDeviceGammaRamp = (Type_SetDeviceGammaRamp)GetProcAddress(hGDI32, "SetDeviceGammaRamp");
- if(pSetDeviceGammaRamp == NULL)
- FreeLibrary();
- else
- bReturn = TRUE;
- }
- return bReturn;
- }
- void ChangeBrightness::FreeLibrary()
- {
- if(hGDI32 != NULL)
- {
- ::FreeLibrary(hGDI32);
- hGDI32 = NULL;
- }
- }
- BOOL ChangeBrightness::LoadLibraryIfNeeded()
- {
- BOOL bReturn = FALSE;
- if(hGDI32 == NULL)
- LoadLibrary();
- if(pSetDeviceGammaRamp != NULL)
- bReturn = TRUE;
- return bReturn;
- }
-
-
- BOOL ChangeBrightness::SetDeviceGammaRamp(HDC hDC, LPVOID lpRamp)
- {
- if(LoadLibraryIfNeeded())
- return pSetDeviceGammaRamp(hDC, lpRamp);
- else
- return FALSE;
- }
- BOOL ChangeBrightness::SetBrightness(HDC hDC, WORD wBrightness)
- {
- BOOL bReturn = FALSE;
- HDC hGammaDC = hDC;
-
- if(hDC == NULL)
- hGammaDC = GetDC(NULL);
- if(hGammaDC != NULL)
- {
- 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;
- }
- bReturn = SetDeviceGammaRamp(hGammaDC, GammaArray);
- }
- if(hDC == NULL)
- ReleaseDC(NULL, hGammaDC);
- return bReturn;
- }
复制代码- // ChangeBrightnessDlg.h : 头文件
- //
- #pragma once
- #include "afxcmn.h"
- #include "ChangeBrightnessInfo.h"
- // CChangeBrightnessDlg 对话框
- class CChangeBrightnessDlg : public CDialog
- {
- // 构造
- public:
- CChangeBrightnessDlg(CWnd* pParent = NULL); // 标准构造函数
- // 对话框数据
- enum { IDD = IDD_CHANGEBRIGHTNESS_DIALOG };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- protected:
- HICON m_hIcon;
- // 生成的消息映射函数
- virtual BOOL OnInitDialog();
- afx_msg void OnPaint();
- afx_msg HCURSOR OnQueryDragIcon();
- DECLARE_MESSAGE_MAP()
- public:
- afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
- afx_msg void OnBnClickedButtonReset();
- public:
- ChangeBrightness m_cb;
- CSliderCtrl m_silderCtrl;
- };
复制代码- // ChangeBrightnessDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "ChangeBrightness.h"
- #include "ChangeBrightnessDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // CChangeBrightnessDlg 对话框
- CChangeBrightnessDlg::CChangeBrightnessDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CChangeBrightnessDlg::IDD, pParent)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CChangeBrightnessDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Control(pDX, IDC_SLIDER_CHANGE, m_silderCtrl);
- }
- BEGIN_MESSAGE_MAP(CChangeBrightnessDlg, CDialog)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_HSCROLL()
- ON_BN_CLICKED(IDC_BUTTON_RESET, &CChangeBrightnessDlg::OnBnClickedButtonReset)
- END_MESSAGE_MAP()
- // CChangeBrightnessDlg 消息处理程序
- BOOL CChangeBrightnessDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- //设置滑块范围
- m_silderCtrl.SetRange(0,128);
- //初始化为默认值
- m_silderCtrl.SetPos(64);
- m_cb.SetBrightness(NULL,128);
- return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CChangeBrightnessDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作区矩形中居中
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标
- //显示。
- HCURSOR CChangeBrightnessDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CChangeBrightnessDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- //响应滑块事件,每次调节两个亮度点
- m_cb.SetBrightness(NULL,m_silderCtrl.GetPos() * 2);
- CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
- }
- void CChangeBrightnessDlg::OnBnClickedButtonReset()
- {
- //恢复默认亮度
- m_silderCtrl.SetPos(64);
- m_cb.SetBrightness(NULL,128);
- }
复制代码 |
|