您现在的位置:中国下载站学院中心网络编程Visual C++教程Visual C++实例教程 → 文章列表

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

作者:佚名  来源:不详  发布时间:2006-12-29 13:42:03   

减小字体 增大字体

 
 
屏幕保护程序是一个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()中。

  以上窗口

[1] [2]  下一页


在百度中搜索更多用Visual C++编写完整的屏幕保护程序相关网页 转贴于:中国下载站

  • 上一篇文章:VB+Access设计图书管理系统
  • 下一篇文章:实例解析C++/CLI的串行化
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!