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