【编程开发】AspAsp.NetCGIPHPJspXMLPERLC++C#VCVBDelphiPowerBuilderJAVA汇编数据库编程移动开发其它语言

您现在的位置:首页 > 网络学院 > 编程开发 > VC > 用Visual C++编写完整的屏幕保护程序

用Visual C++编写完整的屏幕保护程序

来源: 作者: 日期:2006-12-27

【聚杰网VC】用Visual C++编写完整的屏幕保护程序 屏幕保护程序是一个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;
 }
}
///////////////////////

 

1 2 3 4 下一页

评论   点击查看全部评论
您的评论参与,将为聚杰带来更大的动力!请不要吝啬!
快速回复
请使用文明语言让我们维护健康绿色网络环境!

匿名发表   验证码: