屏幕保护程序是一个Win32应用程序,与一般的Win32应用程序不同之处在于:1、扩展名要求为 SCR ;2、命令行要有一定的格式,以便操作系统向其传递信息,如 运行模式,父窗口句柄(Handle to Parent Window)等 ;3、其他一些消息相应方面的要求。本文将首先介绍屏幕保护程序的命令行格式及实现的方法,然后介绍各个相应函数,并通过Window主函数WinMin()勾画出屏幕保护程序的主框架,最后介绍编译步骤和注意事项
屏幕保护程序的命令行格式 :文件名 / [运行模式] /[窗口句柄]。
其中运行模式有五种选择:
1. “运行模式”= ‘c’ 或 ‘C ’, 句柄为一串数字, 或文件名后没有任何参数。
屏保程序设置方式,Window 显示属性_屏幕保护程序_设置按钮调用,数字为调用函数的窗口句柄(Handle to Parent Window)(十进制),如果没有数字,句柄为NULL。
2. “运行模式”=‘t’或‘T’。
测试方式,忽略句柄数字。
3. “运行模式”=‘p’或‘P’。
预览方式,Window 显示属性_屏幕保护程序_预览按钮调用,句柄为调用函数的窗口句柄。
4. “运行模式”=‘a’或‘A’。
密码设置方式, Window 显示属性_屏幕保护程序_密码保护_更改按钮调用。句柄为调用函数的Window 句柄。
5. 其它(通常“运行模式”=‘s’)
屏幕保护程序正常运行模式。
因此,编写屏幕保护程序的首要任务是过滤命令行,提取对应的系统调用方式和其他信息,本文用自定义函数ParseCommandline( )实现:
//用enum定义五种调用方式: enum SaverMode { sm_config, sm_preview, sm_full, sm_test, sm_passwordchange };
//命令行过滤函数,命令行获得函数是用API GetCommandLine( )。 SaverMode ParseCommandLine( TCHAR* pstrCommandLine ) { g_hWndParent = NULL; //全局变量(global varibale) 在头函数或主文件开始处定义。
// 跳过长文件名中的路径和空格。 if (*pstrCommandLine == TEXT('/"')) { pstrCommandLine++; while (*pstrCommandLine != TEXT('/0') && *pstrCommandLine != TEXT('/"')) pstrCommandLine++; If( *pstrCommandLine == TEXT('/"') ) pstrCommandLine++; } else { while (*pstrCommandLine != TEXT('/0') && *pstrCommandLine != TEXT(' ')) pstrCommandLine++; if( *pstrCommandLine == TEXT(' ') ) pstrCommandLine++; }
// 跳过"/" 或 "-" while ( *pstrCommandLine != TEXT('/0') && *pstrCommandLine != TEXT('/') && *pstrCommandLine != TEXT('-') ) pstrCommandLine++;
// 如果没有任何参数,为设置模式。 if ( *pstrCommandLine == TEXT('/0') ) return sm_config;
// 如果有参数,查看参数内容。 switch ( *(++pstrCommandLine) ) { case 'c': case 'C': pstrCommandLine++; while ( *pstrCommandLine && !isdigit(*pstrCommandLine) ) pstrCommandLine++; if ( isdigit(*pstrCommandLine) ) { #ifdef _WIN64 //考虑64位编译情况。 CHAR strCommandLine[2048]; DXUtil_ConvertGenericStringToAnsiCb( strCommandLine, pstrCommandLine, sizeof(strCommandLine)); //该函数仅在64位编译情况下使用。 g_hWndParent = (HWND)(_atoi64(strCommandLine)); #else g_hWndParent = (HWND)LongToHandle(_ttol(pstrCommandLine)); //数字串变为 /Window句柄 #endif } else { g_hWndParent = NULL; } return sm_config;
case 't': case 'T': return sm_test;
case 'p': case 'P': //预览模式,后面有Window句柄,为十进制数字 pstrCommandLine++; while ( *pstrCommandLine && !isdigit(*pstrCommandLine) ) pstrCommandLine++; if ( isdigit(*pstrCommandLine) ) { #ifdef _WIN64 CHAR strCommandLine[2048]; DXUtil_ConvertGenericStringToAnsiCb(strCommandLine, pstrCommandLine, sizeof(strCommandLine)); g_hWndParent = (HWND)(_atoi64(strCommandLine)); #else g_hWndParent = (HWND)LongToHandle(_ttol(pstrCommandLine)); #endif } return sm_preview;
case 'a': case 'A': //密码设置模式,后面有Window句柄,为十进制数字 pstrCommandLine++; while ( *pstrCommandLine && !isdigit(*pstrCommandLine) ) pstrCommandLine++; if ( isdigit(*pstrCommandLine) ) { #ifdef _WIN64 CHAR strCommandLine[2048]; DXUtil_ConvertGenericStringToAnsiCb(strCommandLine, pstrCommandLine, sizeof(strCommandLine)); g_hWndParent = (HWND)(_atoi64(strCommandLine)); #else g_hWndParent = (HWND)LongToHandle(_ttol(pstrCommandLine)); #endif } return sm_passwordchange;
default: //其他选项,屏保实际运行模式(通常/s) return sm_full; } } /////////////////////// |
ParseCommandLine( ) 返回后,程序根据不同的返回值进行响应:
返回值=sm_preview或者sm_test 或者sm_full:
程序根据返回的运行模式和Window句柄使用CreateWindow函数创建窗口(Window)并返回指向该窗口的句柄。这部分功能包含在自定义的CreateSaverWindow()函数中。在sm_preview情况下,程序用消息循环的方式等待500ms使操作系统的控制面板有足够的时间初始化。然后,读注册表检查屏保是否设定了密码,如果是,在Win9x情况下,用LoadLibrary()和GetProcessAdress()函数从动态链接库(DLL)中获得密码验证函数指针供程序退出时使用,密码验证函数类型为BOOL PASCAL (HWND)。这部分功能包含在自定义函数InitSaver()中。
以上窗口创建使用同一个窗口类(Window Class(WNDCLASS)),当然也是同一个消息响应函数(Window’s Procedure)。最后显示窗口,开始消息循环。
返回值=sm_passwordchange:
用LoadLibrary()和 GetProcessAdress()API获得密码设置函数指针,密码设置函数类型为:DOWORD PASCAL ( LPCSTR, HWND, DWORD, LPVOID ),然后调用该函数进行密码更改。
返回值=sm_config
显示一个对话框(Dialog),获取用户信息,在程序中进行相应的更改。 <  
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
1/2 1 2 下一页 尾页 |