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

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

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

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

【聚杰网VC】用Visual C++编写完整的屏幕保护程序

4、 InitSaver()函数

VOID InitSaver()
{
 //检查操作系统版本
 OSVERSIONINFO osvi;
 osvi.dwOSVersionInfoSize = sizeof(osvi);
 GetVersionEx( &osvi );
 g_bIs9x = (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS);

 // 在运行模式下,如果操作系统是win9x,需要载入密码验证动态链接库。
 if ( g_SaverMode == sm_full && g_bIs9x )
 {
  // 检查注册表查看屏保是否设定了密码
  HKEY hKey;
  if ( RegCreateKeyEx( HKEY_CURRENT_USER, REGSTR_PATH_SCREENSAVE, 0, NULL, 0,  KEY_READ, NULL, &hKey, NULL ) == ERROR_SUCCESS )
  {
   DWORD dwVal;
   DWORD dwSize = sizeof(dwVal);

   if ( (RegQueryValueEx( hKey, REGSTR_VALUE_USESCRPASSWORD, NULL, NULL,
(BYTE *)&dwVal, &dwSize ) == ERROR_SUCCESS) && dwVal )
   {
    g_hPasswordDLL = LoadLibrary( TEXT("PASSWORD.CPL") );
    if ( g_hPasswordDLL )
     g_VerifySaverPassword= (VERIFYPWDPROC)GetProcAddress( g_hPasswordDLL, "VerifyScreenSavePwd" );
     RegCloseKey( hKey );
   }
  }
 }

 if ( g_SaverMode == sm_full )
 {
  BOOL bUnused;
  SystemParametersInfo( SPI_SCREENSAVERRUNNING, TRUE, &bUnused, 0 ); //通知操作系统屏幕保护程序开始运行。
 }
}

  5、屏保退出函数ShutdownSaver( )和InteruptSaver( ):

VOID ShutdownSaver()
{
 // 通知操作系统屏幕保护程序退出
 if ( g_SaverMode == sm_full )
 {
  BOOL bUnused;
  SystemParametersInfo( SPI_SCREENSAVERRUNNING, FALSE, &bUnused, 0 );
 }

 if ( g_hPasswordDLL != NULL )
 {
  FreeLibrary( g_hPasswordDLL );
  g_hPasswordDLL = NULL;
 }

  PostQuitMessage( 0 );
}
/////////////////////////////////
VOID InterruptSaver()
{
 BOOL bPasswordOkay = FALSE;
 if( g_SaverMode == sm_test ||g_SaverMode == sm_full&&!g_bCheckingSaverPassword )
 {
  if( g_bIs9x && g_SaverMode == sm_full )
  {
   // Win9x下如果g_VerifySaverPassword==NULL,则没有设屏保密码。
   if ( g_VerifySaverPassword != NULL )
   {
    g_bCheckingSaverPassword = TRUE;//告诉消息响应函数正在验证密码
    bPasswordOkay = g_VerifySaverPassword( g_hWnd );
    g_bCheckingSaverPassword = FALSE; //密码验证结束。
    if ( !bPasswordOkay )
    {
     //屏保程序继续运行…
     SetCursor( NULL );
     g_dwSaverMouseMoveCount = 0;
     return;
    }
   }
  }
  ShutdownSaver();
 }
}

  屏保设置方式下的响应程序为自定义Doconfig( ),因篇幅的关系不再详细介绍,读者应当很容易自己加上,也可以定义为空函数:VOID Doconfig ( ) { return;}。

  黑屏屏幕保护程序主入口函数如下:

#include <windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <regstr.h>
#include "Saver.h"
#include "resource.h"

HINSTANCE g_hinstance;
HWND g_hWndParent;
BOOL g_bWaitForInputIdle;
HWND g_hWnd;


LRESULT CALLBACK SaverProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
 // TODO: Place code here.
 MSG msg;
 g_bCheckingSaverPassword = FALSE;
 g_bIs9x = FALSE;
 g_dwSaverMouseMoveCount = 0;
 g_hWndParent = NULL;
 g_hPasswordDLL = NULL;
 g_hWnd = NULL;
 g_VerifySaverPassword = NULL;
 g_hinstance=hInstance;

 WNDCLASS cls;
 cls.hCursor = LoadCursor( NULL, IDC_ARROW );
 cls.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON) );
 cls.lpszMenuName = NULL;
 cls.lpszClassName = TEXT("SaverWndClass");
 cls.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
 cls.hInstance = hInstance;
 cls.style = CS_VREDRAW|CS_HREDRAW;
 cls.lpfnWndProc = SaverProc;
 cls.cbWndExtra = 0;
 cls.cbClsExtra = 0;

 if(!RegisterClass( &cls ))
  MessageBox(NULL,TEXT("Cant register window class"),TEXT("SaverWndClass"),MB_ICONERROR);

 SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_IDLE );
 TCHAR* pstrCmdLine = GetCommandLine();
 g_SaverMode = ParseCommandLine(pstrCmdLine);

 switch(g_SaverMode)
 {
  case sm_preview:
  case sm_full:
  case sm_test:
  g_hWnd=CreateSaverWindow(g_SaverMode,g_hWndParent,hInstance);
  if ( g_hWnd == NULL )
  {
   MessageBox(NULL,TEXT("Can't Create Window"), TEXT("Create Window Terminated"),MB_ICONERROR);
   return 0;
  }else
  {
   ShowWindow(g_hWnd,nCmdShow);
   UpdateWindow(g_hWnd);
  }
  InitSaver();
  while(GetMessage(&msg,NULL,0,0))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
  break;
 case sm_config:
  Doconfig();
  break;
 case sm_passwordchange:
  ChangePassword();
  break;
 }
 return 0;
}

 

上一页 1 2 3 4 下一页

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

匿名发表   验证码: