第一步:监听锁屏事件;LRESULT CLockAPPSampleDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
LRESULT CLockAPPSampleDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{
case WM_WTSSESSION_CHANGE:
{
//MessageBox("WM_WTSSESSION_CHANGE", "Esmile", MB_OK);
switch(wParam)
{
case WTS_SESSION_LOCK:
{
//锁屏事件
}
break;
case WTS_SESSION_UNLOCK:
{
//解锁时间
}
break;
default:
break;
}
}
break;
case WM_DESTROY:
WTSUnRegisterSessionNotification(m_hWnd);
break;
default:
break;
}
return CDialog::WindowProc(message, wParam, lParam);
}
第二步:将图片拷贝C:\Windows\System32\oobe\info\Backgrounds目录下,名字可以任意;
void CWin7DesktopUtil::copyFile(LPTSTR lpPicFile)
{
TCHAR szPath[MAX_PATH] = { 0 };
_tcscpy(szPath, _T("C:\\Windows\\System32\\oobe"));
if (FALSE == PathFileExists(szPath)) {
if(FALSE == CreateDirectory(szPath, NULL)){
return;
}
}
_tcscat(szPath, _T("\\info"));
if (FALSE == PathFileExists(szPath)) {
if(FALSE == CreateDirectory(szPath, NULL)){
return;
}
}
_tcscat(szPath, _T("\\Backgrounds"));
if (FALSE == PathFileExists(szPath)) {
if(FALSE == CreateDirectory(szPath, NULL)){
return;
}
}
_tcscat(szPath, _T("\\backgroundDefault.jpg"));
::CopyFile(lpPicFile, szPath, FALSE);
}
第三步:设置注册表
将SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI\\Background下的
OEMBackground键值设为1
BOOL CWin7DesktopUtil::setOEMBackground(DWORD dwValue)
{
HKEY hKey;
LPCTSTR lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI\\Background");
long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE|KEY_WOW64_64KEY, &hKey);
if(lRet== ERROR_SUCCESS)
{
lRet = RegSetValueEx(hKey, _T("OEMBackground"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
if (ERROR_SUCCESS == lRet)
{
RegCloseKey(hKey);
return TRUE;
}
else
{
DWORD dwError = GetLastError();
}
RegCloseKey(hKey);
}
return FALSE;
}
PS:
1、此处需要注意图片大小不能大于256K,否则设置无效。
2、考虑64位系统情况,拷贝之前调用Wow64DisableWow64FsRedirection
这个很简单。
第一步:检索图片文件夹,保存到m_strPathArray数组中
void CRandomImage::search(LPTSTR path)
{
HANDLE hFind;
WIN32_FIND_DATA wfd;
TCHAR tempPath[MAX_PATH];
ZeroMemory(&wfd, sizeof(WIN32_FIND_DATA));
memset(tempPath, 0, sizeof(tempPath));
sprintf(tempPath, "%s\\*.*", path);
hFind = FindFirstFile(tempPath, &wfd);
if(INVALID_HANDLE_VALUE == hFind)
{
return;
}
do
{
if('.' == wfd.cFileName[0])
{
continue;
}
if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
sprintf(tempPath, "%s\\%s", path, wfd.cFileName);
search(tempPath);
}
else
{
sprintf(tempPath, "%s\\%s", path, wfd.cFileName);
m_strPathArray.Add(tempPath);
}
}while(FindNextFile(hFind, &wfd));
FindClose(hFind);
}
第二部:从图片文件夹中随机选取一张图片
const CString CRandomImage::getRandomImage()
{
INT32 length = m_strPathArray.GetCount();
if (length == 0) {
return _T("");
}
{
srand((unsigned)time(NULL));
m_iRandomIndex = rand() % length;
}while(m_iRandomIndex == length);
return m_strPathArray.GetAt(m_iRandomIndex);
}
将SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI\\Background下的
OEMBackground键值设为0