一.如何获得显示属性
使用GetDeviceCaps函数获取分辨率相关信息
View Code
void CDemoDlg::OnTest()
{
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();
//创建显示设备上下文
HDC hdc = CreateDC(_T("display"), NULL, NULL, NULL);
//颜色深度
int nBitsPerPixel=GetDeviceCaps(hdc, BITSPIXEL);
//水平分辨率
int nWidth = GetDeviceCaps(hdc, HORZRES);
//垂直分辨率
int nHeight = GetDeviceCaps(hdc, VERTRES);
//刷新率
int nDisplayFrequency = GetDeviceCaps(hdc, VREFRESH);
CString strText = _T("");
strText.Format(_T("颜色深度: %d 位"), nBitsPerPixel);
pListBox->AddString(strText);
strText.Format(_T("水平分辨率: %d 像素"), nWidth);
pListBox->AddString(strText);
strText.Format(_T("垂直分辨率: %d 像素"), nHeight);
pListBox->AddString(strText);
strText.Format(_T("刷新率: %d 赫兹"), nDisplayFrequency);
pListBox->AddString(strText);
}
{
CListBox* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
pListBox->ResetContent();
//创建显示设备上下文
HDC hdc = CreateDC(_T("display"), NULL, NULL, NULL);
//颜色深度
int nBitsPerPixel=GetDeviceCaps(hdc, BITSPIXEL);
//水平分辨率
int nWidth = GetDeviceCaps(hdc, HORZRES);
//垂直分辨率
int nHeight = GetDeviceCaps(hdc, VERTRES);
//刷新率
int nDisplayFrequency = GetDeviceCaps(hdc, VREFRESH);
CString strText = _T("");
strText.Format(_T("颜色深度: %d 位"), nBitsPerPixel);
pListBox->AddString(strText);
strText.Format(_T("水平分辨率: %d 像素"), nWidth);
pListBox->AddString(strText);
strText.Format(_T("垂直分辨率: %d 像素"), nHeight);
pListBox->AddString(strText);
strText.Format(_T("刷新率: %d 赫兹"), nDisplayFrequency);
pListBox->AddString(strText);
}
二.如何设置显示属性
填充DEVMODE结构体信息,使用ChangeDisplaySettings函数修改显示设置
View Code
void CDemoDlg::OnTest()
{
DEVMODE DevMode;
//颜色深度
if (m_ctrlBitsPerPixel.GetCurSel() == 0)
{
DevMode.dmBitsPerPel = 16;
}
else if (m_ctrlBitsPerPixel.GetCurSel() == 1)
{
DevMode.dmBitsPerPel = 32;
}
//分辨率
if (m_ctrlPixels.GetCurSel() == 0)
{
DevMode.dmPelsWidth = 800;
DevMode.dmPelsHeight = 600;
}
else if (m_ctrlPixels.GetCurSel() == 1)
{
DevMode.dmPelsWidth = 1024;
DevMode.dmPelsHeight = 768;
}
//刷新率
if (m_ctrlDispalyFrequencry.GetCurSel() == 0)
{
DevMode.dmDisplayFrequency = 60;
}
else if (m_ctrlDispalyFrequencry.GetCurSel() == 1)
{
DevMode.dmDisplayFrequency = 75;
}
DevMode.dmSize = sizeof(DEVMODE);
DevMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT |
DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
//设置显示属性
LONG nResult = ChangeDisplaySettings(&DevMode, 0);
if (nResult == DISP_CHANGE_SUCCESSFUL)
{
//用新的设置参数更新注册表
ChangeDisplaySettings(&DevMode, CDS_UPDATEREGISTRY);
AfxMessageBox(_T("设置显示属性成功。"));
}
else
{
//恢复默认设置
ChangeDisplaySettings(NULL, 0);
AfxMessageBox(_T("设置显示属性失败。"));
}
}
{
DEVMODE DevMode;
//颜色深度
if (m_ctrlBitsPerPixel.GetCurSel() == 0)
{
DevMode.dmBitsPerPel = 16;
}
else if (m_ctrlBitsPerPixel.GetCurSel() == 1)
{
DevMode.dmBitsPerPel = 32;
}
//分辨率
if (m_ctrlPixels.GetCurSel() == 0)
{
DevMode.dmPelsWidth = 800;
DevMode.dmPelsHeight = 600;
}
else if (m_ctrlPixels.GetCurSel() == 1)
{
DevMode.dmPelsWidth = 1024;
DevMode.dmPelsHeight = 768;
}
//刷新率
if (m_ctrlDispalyFrequencry.GetCurSel() == 0)
{
DevMode.dmDisplayFrequency = 60;
}
else if (m_ctrlDispalyFrequencry.GetCurSel() == 1)
{
DevMode.dmDisplayFrequency = 75;
}
DevMode.dmSize = sizeof(DEVMODE);
DevMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT |
DM_BITSPERPEL | DM_DISPLAYFREQUENCY;
//设置显示属性
LONG nResult = ChangeDisplaySettings(&DevMode, 0);
if (nResult == DISP_CHANGE_SUCCESSFUL)
{
//用新的设置参数更新注册表
ChangeDisplaySettings(&DevMode, CDS_UPDATEREGISTRY);
AfxMessageBox(_T("设置显示属性成功。"));
}
else
{
//恢复默认设置
ChangeDisplaySettings(NULL, 0);
AfxMessageBox(_T("设置显示属性失败。"));
}
}
三.如何设置显示属性
发送SC_MONITORPOWER命令,传递不同参数,即设置显示器显示的模式
View Code
void CDemoDlg::OnTest()
{
//设置显示器为省电模式
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
Sleep(2000);
//打开显示器
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
Sleep(1000);
//关闭显示器
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
Sleep(2000);
//打开显示器
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
}
{
//设置显示器为省电模式
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 1);
Sleep(2000);
//打开显示器
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
Sleep(1000);
//关闭显示器
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
Sleep(2000);
//打开显示器
::SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
}
四.如何获得和设置鼠标的双击时间
GetDoubleClickTime和SetDoubleClickTime,一般没必要去修改
void CDemoDlg::OnTest1()
{
//获得鼠标的双击时间
UINT nInterval = GetDoubleClickTime();
SetDlgItemInt(IDC_TEXT, nInterval);
}
void CDemoDlg::OnTest2()
{
UINT nInterval = GetDlgItemInt(IDC_TEXT, NULL, FALSE);
//设置鼠标的双击时间
SetDoubleClickTime(nInterval);
}
{
//获得鼠标的双击时间
UINT nInterval = GetDoubleClickTime();
SetDlgItemInt(IDC_TEXT, nInterval);
}
void CDemoDlg::OnTest2()
{
UINT nInterval = GetDlgItemInt(IDC_TEXT, NULL, FALSE);
//设置鼠标的双击时间
SetDoubleClickTime(nInterval);
}
五.如何获得鼠标的按键数
使用GetSystemMetrics函数的SM_CMOUSEBUTTONS参数
void CDemoDlg::OnTest()
{
//获得鼠标的按键数
int nCount = GetSystemMetrics(SM_CMOUSEBUTTONS);
CString strText = _T("");
strText.Format(_T("鼠标按键数:%d"), nCount);
AfxMessageBox(strText);
}
{
//获得鼠标的按键数
int nCount = GetSystemMetrics(SM_CMOUSEBUTTONS);
CString strText = _T("");
strText.Format(_T("鼠标按键数:%d"), nCount);
AfxMessageBox(strText);
}
六.如何切换鼠标的左键和右键
SwapMouseButton函数,传递TRUE,FALSE
void CDemoDlg::OnTest()
{
m_bSwap = !m_bSwap;
//切换鼠标左右键
::SwapMouseButton(m_bSwap);
}
{
m_bSwap = !m_bSwap;
//切换鼠标左右键
::SwapMouseButton(m_bSwap);
}
七.如何获得键盘虚拟键的状态
使用GetKeyboardState获取,The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer
void CDemoDlg::OnTest1()
{
BYTE KeyboardState[256];
//获得SHIFT键状态
::GetKeyboardState(KeyboardState);
CString strText = _T("");
if (KeyboardState[VK_SHIFT] & 0x80)
{
strText = _T("SHIFT键被按下。");
}
else
{
strText = _T("SHIFT键被释放。");
}
AfxMessageBox(strText);
}
{
BYTE KeyboardState[256];
//获得SHIFT键状态
::GetKeyboardState(KeyboardState);
CString strText = _T("");
if (KeyboardState[VK_SHIFT] & 0x80)
{
strText = _T("SHIFT键被按下。");
}
else
{
strText = _T("SHIFT键被释放。");
}
AfxMessageBox(strText);
}
八.如何获得键盘的类型
使用GetKeyboardType函数,均有规定
View Code
void CDemoDlg::OnTest()
{
CString strText = _T("");
//获得键盘类型
int nType = ::GetKeyboardType(0);
if (nType == 1)
{
strText = _T("IBM PC/XT or compatible (83-key) keyboard");
}
else if (nType == 2)
{
strText = _T("Olivetti ICO (102-key) keyboard");
}
else if (nType == 3)
{
strText = _T("IBM PC/AT (84-key) or similar keyboard");
}
else if (nType == 4)
{
strText = _T("IBM enhanced (101- or 102-key) keyboard");
}
else if (nType == 5)
{
strText = _T("Nokia 1050 and similar keyboards");
}
else if (nType == 6)
{
strText = _T("Nokia 9140 and similar keyboards");
}
else if (nType == 7)
{
strText = _T("Japanese keyboard");
}
AfxMessageBox(strText);
}
{
CString strText = _T("");
//获得键盘类型
int nType = ::GetKeyboardType(0);
if (nType == 1)
{
strText = _T("IBM PC/XT or compatible (83-key) keyboard");
}
else if (nType == 2)
{
strText = _T("Olivetti ICO (102-key) keyboard");
}
else if (nType == 3)
{
strText = _T("IBM PC/AT (84-key) or similar keyboard");
}
else if (nType == 4)
{
strText = _T("IBM enhanced (101- or 102-key) keyboard");
}
else if (nType == 5)
{
strText = _T("Nokia 1050 and similar keyboards");
}
else if (nType == 6)
{
strText = _T("Nokia 9140 and similar keyboards");
}
else if (nType == 7)
{
strText = _T("Japanese keyboard");
}
AfxMessageBox(strText);
}
九.如何获得键盘按键的名称
使用GetKeyNameText函数,这个应该最实用
BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
TCHAR szKeyName[32];
//获得键盘按键的名称
::GetKeyNameText(pMsg->lParam, szKeyName, 32);
SetDlgItemText(IDC_TEXT, szKeyName);
}
return CDialog::PreTranslateMessage(pMsg);
}
{
if (pMsg->message == WM_KEYDOWN)
{
TCHAR szKeyName[32];
//获得键盘按键的名称
::GetKeyNameText(pMsg->lParam, szKeyName, 32);
SetDlgItemText(IDC_TEXT, szKeyName);
}
return CDialog::PreTranslateMessage(pMsg);
}
十.如何将键盘的扫描码转换成虚拟键值
使用MapVirtualKey函数进行转换
BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
//获得键盘扫描码
UINT nScanCode = HIWORD(pMsg->lParam);
SetDlgItemInt(IDC_TEXT1, nScanCode, FALSE);
//获得虚拟键值
UINT nVKCode = ::MapVirtualKey(nScanCode, 3);
SetDlgItemInt(IDC_TEXT2, nVKCode, FALSE);
}
return CDialog::PreTranslateMessage(pMsg);
}
{
if (pMsg->message == WM_KEYDOWN)
{
//获得键盘扫描码
UINT nScanCode = HIWORD(pMsg->lParam);
SetDlgItemInt(IDC_TEXT1, nScanCode, FALSE);
//获得虚拟键值
UINT nVKCode = ::MapVirtualKey(nScanCode, 3);
SetDlgItemInt(IDC_TEXT2, nVKCode, FALSE);
}
return CDialog::PreTranslateMessage(pMsg);
}