1. 程式人生 > >鎖屏背景替換-Win7篇

鎖屏背景替換-Win7篇

1、背景介面替換原理

第一步:監聽鎖屏事件;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

2、定時器

這個很簡單。

第一步:檢索圖片資料夾,儲存到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);
}

3、恢復

將SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Authentication\\LogonUI\\Background下的

OEMBackground鍵值設為0