时间: 2021-07-31 作者:daque
e_mail:codehunter@sohu.com网址:http://codehunter.1yes.net纲要:正文经过一个简直的步调演练了windows下的屏幕养护步调的实行进程。一. 弁言视窗体例下的屏幕养护步调是一个鉴于吩咐行(command line)的运用步调。当屏保步调被挪用时操纵体例就用简直的吩咐行实行该步调。正文构造和处置了一切的吩咐行,囊括“/p”,“/s”,“/c”,“/a”,个中“/p”表白让屏保在预览窗口中表露;“/s”表白真实运转屏保;“/c”表白挪用树立对话框;而“/a”表白挪用暗号树立对话框(winnt中失效)。本步调尽大概大略地实行一个全功效的屏保,运转windows的屏保树立步调时你既不妨窜改暗号(winnt中失效)又不妨树立图片表露的频次并把频次数值生存到备案内外。当屏保运转时图片以你树立的频次变换表露场所。笔者还留了个功课给读者群,请看图第11中学的采用图片文献夹这个名目,按下欣赏按钮不妨树立图片的路途,笔者仍旧实行了欣赏按钮的功效并把获得的路途也生存到备案表中,并让屏保启用时读picdir的值,picdir即是"no"时的代码笔者已实行了,picdir不即是"no"时的代码由读者群实行。也即是让读者群实行一个能把picdir目次里的图片轮番表露的屏保步调。二. 实行本领开始引见几个api因变量。winmain因变量:int winapi winmain( hinstance hinstance, // 暂时范例句柄 hinstance hprevinstance, // 前一个范例句柄 lpstr lpcmdline, // 指向吩咐行参数的南针(本步调要运用的参数) int ncmdshow // 窗口的状况 );getwindowlong因变量:获得指定窗口消息的因变量 long getwindowlong( hwnd hwnd, //窗口句柄 int nindex //指定归来的消息 );setwindowlong因变量:变换窗口属性 long setwindowlong( hwnd hwnd, //窗口句柄 int nindex, // 指定要设定的值的消息 long dwnewlong // 新值 );setparent因变量:变换指定窗口的父窗口 hwnd setparent( hwnd hwndchild, //要变换父窗体的窗口句柄 hwnd hwndnewparent //新的父窗体的句柄 );getclientrect因变量:获得窗口的存户区 bool getclientrect( hwnd hwnd, // 窗口句柄 lprect lprect //rect构造的地方 );setwindowpos因变量:变换窗口的巨细,场所,顶级窗口等bool setwindowpos(hwnd hwnd, // 窗口句柄 hwnd hwndinsertafter, // 安置窗口程序的句柄(z order) int x, // horizontal position int y, // vertical position int cx, // width int cy, // height uint uflags // 窗口场所等标志 );systemparametersinfo因变量:考察或树立体例级的参数 bool systemparametersinfo( uint uiaction, // 指定要获得或树立的体例参数 uint uiparam, // depends on action to be taken pvoid pvparam, // depends on action to be taken uint fwinini // 用户摆设文献能否变换标志 );showcursor因变量:表露或湮没光标int showcursor(bool bshow // 鼠标看来度标志 );getversion因变量:获得体例的本子消息dword getversion(void)之上api因变量的简直消息不妨搜索相关mssdk文书档案。领会了基础因变量后笔者简述一下实行本领。1. 兴建一工程,减少两个窗体,将三个窗体辨别取名为mainform,frmconfig,frmcontrol。在mainform和frmcontrol窗体上各增添一个timer控件和timage控件,把两窗体的borderstyle设为bsnone,背局面设为玄色。在两个窗体的timage上各加一张图片,frmcontrol巨细设为:高130像素,宽160像素,timage的stretch属性设为真值。frmconfig的款式如图1。2. 生存工程文献为screensaver.cpp,其它单位辨别存为unitmain.cpp,unitcontrol.cpp,unitconfig.cpp。3. 编写代码,简直代码见第三局部的源步调。4. 编写翻译成可实行文献,并把文献扩充名改为scr。5. 结果把屏保步调正片到windows目次下就不妨尝试了。即使十足平常的话你将会看到图片在屏幕上以随机的场所表露。 图1三. 源代码以次是本步调的一切的源代码,个中screensaver.cpp, unitmain.cpp是中心代码。/*{*******************************}*//*{***** screensaver.cpp ****}*//*{*******************************}*///---------------------------------------------------------------------------#include #pragma hdrstopuseres("screensaver.res");useform("unitmain.cpp", frmmain);useform("unitconfig.cpp", frmconfig);useform("unitcontrol.cpp", frmcontrol);//---------------------------------------------------------------------------winapi winmain(hinstance, hinstance, lpstr p, int)//“p"是指向吩咐行参数的南针{ string starttype; ansistring command=p,temp; hwnd cpwindow =null; if(command=="") starttype = "/c"; else starttype=command.substring(1,2);//获得吩咐行的前两个参数 try { application->initialize(); if(starttype=="/c")//启用树立窗口 application->createform(__classid(tfrmconfig), &frmconfig); else if(starttype=="/s")启用屏保 application->createform(__classid(tfrmmain), &frmmain); else if(starttype=="/p")//预览 { application->createform(__classid(tfrmcontrol), &frmcontrol); temp=command.substring(3,command.length()-2);//获得吩咐行中的屏保预览窗口句柄的字符串情势 cpwindow =(long *)temp.toint();//将预览窗口句柄的字符串情势强迫变换为长整形南针 rect *lookrect;//创造一个rect构造南针 long style=getwindowlong(application->mainform->handle,gwl_style);//获得frmcontrol窗口的作风 style=style|ws_child; setwindowlong(application->mainform->handle,gwl_style,style);//树立窗口为子窗口 setparent(application->mainform->handle,cpwindow);//树立屏保预览窗口为frmcontrol的父窗口 getclientrect(cpwindow,lookrect);//获得屏保预览窗口的存户区 setwindowpos(application->mainform->handle,hwnd_top,0,0,lookrect->right,lookrect->bottom ,swp_nozorder|swp_noactivate|swp_showwindow);//将frmcontrol的窗口掩盖屏保预览窗口的存户区,并表露它 } else if(starttype=="/a")//启用暗号树立窗口 { temp=command.substring(3,command.length()-2); cpwindow =(long *)temp.toint(); //以次是动静挪用mpr.dll里的pwdchangepassworda因变量的进程 typedef uint(callback *fun)(lpstr,hwnd,uint,uint); hinstance hdll=loadlibrary("mpr.dll"); fun myfun; if(hdll!=null) { myfun=(fun)getprocaddress(hdll,"pwdchangepassworda"); if(!myfun)freelibrary(hdll); else myfun("scrsave", cpwindow, 0, 0);//因变量的挪用 } } application->run(); } catch (exception &exception) { application->showexception(&exception); } return 0;}//---------------------------------------------------------------------------/*{*******************************}*//*{***** unitmain.h ****}*//*{*******************************}*///---------------------------------------------------------------------------#ifndef unitmainh#define unitmainh//---------------------------------------------------------------------------#include #include #include #include #include #include #include //---------------------------------------------------------------------------class tfrmmain : public tform{__published: // ide-managed components ttimer *timer1; timage *image1; void __fastcall formcreate(tobject *sender); void __fastcall formkeydown(tobject *sender, word &key, tshiftstate shift); void __fastcall formmousedown(tobject *sender, tmousebutton button, tshiftstate shift, int x, int y); void __fastcall formclosequery(tobject *sender, bool &canclose); void __fastcall formclose(tobject *sender, tcloseaction &action); void __fastcall image1mousedown(tobject *sender, tmousebutton button, tshiftstate shift, int x, int y); void __fastcall image1mousemove(tobject *sender, tshiftstate shift, int x, int y); void __fastcall timer1timer(tobject *sender);private: // user declarations dword pwprotect; dword version; string picdir; int frequence;public: // user declarations __fastcall tfrmmain(tcomponent* owner);};//---------------------------------------------------------------------------extern package tfrmmain *frmmain;//---------------------------------------------------------------------------#endif//---------------------------------------------------------------------------/*{*******************************}*//*{***** unitmain.cpp ****}*//*{*******************************}*///---------------------------------------------------------------------------#include #pragma hdrstop#include #include "unitmain.h"#include //---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"tfrmmain *frmmain;//---------------------------------------------------------------------------__fastcall tfrmmain::tfrmmain(tcomponent* owner) : tform(owner){}//---------------------------------------------------------------------------void __fastcall tfrmmain::formcreate(tobject *sender){ //使窗口变成最高层的窗口 setwindowpos(this->handle, hwnd_topmost, 0, 0, 0, 0, swp_nomove|swp_nosize); //时窗口掩盖屏幕 this->width=screen->width; this->height=screen->height; this->top=0; this->left=0; version=getversion(); tregistry *registry = new tregistry; try { if(version>0x80000000){ registry->rootkey =hkey_current_user; registry->openkey("\\control panel\\desktop",false); pwprotect=registry->readinteger("screensaveusepassword");//检验和测定能否暗号养护 registry->closekey();} registry->rootkey =hkey_current_user; registry->openkey("\\software\\codehunter", true); picdir=registry->readstring("picdir");//获得图片目次 frequence=registry->readinteger("frequence");//获得图像表露的频次 if(picdir=="")picdir="no"; if(frequence<0||frequence>6) frequence=2; timer1->interval=1000*frequence;树立准时器 } __finally { delete registry; picdir="no"; } //检验和测定能否运转于 nt下 if(version!=0) if(pwprotect&&version>0x80000000)//即使体例诉求暗号养护并此体例为非nt那么把体例设为屏保状况 systemparametersinfo(spi_screensaverrunning, 1, 0, 0); //使光标消逝 while (!showcursor(false)< -5);}//---------------------------------------------------------------------------void __fastcall tfrmmain::formkeydown(tobject *sender, word &key, tshiftstate shift){this->close();}//---------------------------------------------------------------------------void __fastcall tfrmmain::formmousedown(tobject *sender, tmousebutton button, tshiftstate shift, int x, int y){this->close();}//---------------------------------------------------------------------------void __fastcall tfrmmain::formclosequery(tobject *sender, bool &canclose){if (pwprotect && version>0x80000000) { bool passchck; //表露光标,并挪用暗号对话框 while(!showcursor(true) > 5); //以次是verifyscreensavepwd因变量的动静挪用 typedef uint(callback *fun)(hwnd); hinstance hdll=loadlibrary("password.cpl"); fun myfun; if(hdll!=null) { myfun=(fun)getprocaddress(hdll,"verifyscreensavepwd"); if(!myfun)freelibrary(hdll); else passchck=myfun(this->handle); } if(passchck == false) { while(!showcursor(false) < -5); canclose = false; } }}//---------------------------------------------------------------------------void __fastcall tfrmmain::formclose(tobject *sender, tcloseaction &action){while(!showcursor(true) > 5); if(pwprotect&&version>0x80000000) systemparametersinfo(spi_screensaverrunning, 0, 0, 0);//退出屏保状况}//---------------------------------------------------------------------------void __fastcall tfrmmain::image1mousedown(tobject *sender, tmousebutton button, tshiftstate shift, int x, int y){this->close();}//---------------------------------------------------------------------------void __fastcall tfrmmain::image1mousemove(tobject *sender, tshiftstate shift, int x, int y){ static int mousemoves=0; mousemoves = mousemoves + 1; if(mousemoves >4) { this->close(); mousemoves = 0 ; }}//---------------------------------------------------------------------------void __fastcall tfrmmain::timer1timer(tobject *sender){if(picdir=="no"){int i ;randomize();i=rand()%2;if(i==0)i=-1;elsei=1;image1->top=i*(rand()%this->height);image1->left=i*(rand()%this->width);}}//---------------------------------------------------------------------------/*{*******************************}*//*{***** unitcontrol.h ****}*//*{*******************************}*///---------------------------------------------------------------------------#ifndef unitcontrolh#define unitcontrolh//---------------------------------------------------------------------------#include #include #include #include #include #include #include //---------------------------------------------------------------------------class tfrmcontrol : public tform{__published: // ide-managed components timage *image1; ttimer *timer1; void __fastcall timer1timer(tobject *sender); void __fastcall formcreate(tobject *sender);private: // user declarationspublic: // user declarations __fastcall tfrmcontrol(tcomponent* owner);};//---------------------------------------------------------------------------extern package tfrmcontrol *frmcontrol;//---------------------------------------------------------------------------#endif//---------------------------------------------------------------------------/*{*******************************}*//*{***** unitcontrol.cpp ****}*//*{*******************************}*///---------------------------------------------------------------------------#include #pragma hdrstop#include "unitcontrol.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"tfrmcontrol *frmcontrol;//---------------------------------------------------------------------------__fastcall tfrmcontrol::tfrmcontrol(tcomponent* owner) : tform(owner){}//---------------------------------------------------------------------------void __fastcall tfrmcontrol::timer1timer(tobject *sender){int i ;randomize();i=rand()%2;if(i==0)i=-1;elsei=1;image1->top=i*(rand()%this->height);image1->left=i*(rand()%this->width);}//---------------------------------------------------------------------------void __fastcall tfrmcontrol::formcreate(tobject *sender){image1->top=0;image1->left=0;image1->height=this->height ;image1->width=this->width ;}//---------------------------------------------------------------------------/*{*******************************}*//*{***** unitconfig.h ****}*//*{*******************************}*///---------------------------------------------------------------------------#ifndef unitconfigh#define unitconfigh//---------------------------------------------------------------------------#include #include #include #include #include #include #include #include //---------------------------------------------------------------------------class tfrmconfig : public tform{__published: // ide-managed components tpanel *panel1; tbutton *button1; tpanel *panel2; tlabel *label1; ttrackbar *trackbar1; tlabel *label2; tlabel *label3; tlabel *label4; tbutton *button2; void __fastcall button1click(tobject *sender); void __fastcall button2click(tobject *sender);private: // user declarations ansistring picdir; int frequence;public: // user declarations __fastcall tfrmconfig(tcomponent* owner);};//---------------------------------------------------------------------------extern package tfrmconfig *frmconfig;//---------------------------------------------------------------------------#endif//---------------------------------------------------------------------------/*{*******************************}*//*{***** unitconfig.cpp ****}*//*{*******************************}*///---------------------------------------------------------------------------#include #pragma hdrstop#include "unitconfig.h"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"tfrmconfig *frmconfig;//---------------------------------------------------------------------------__fastcall tfrmconfig::tfrmconfig(tcomponent* owner) : tform(owner){}//---------------------------------------------------------------------------void __fastcall tfrmconfig::button1click(tobject *sender){ if(selectdirectory("select picture dir","",picdir)) panel2->caption=picdir;}//---------------------------------------------------------------------------void __fastcall tfrmconfig::button2click(tobject *sender){//把消息写入备案表if(picdir=="") picdir="no";this->frequence=trackbar1->position;tregistry *reg = new tregistry; try { reg->rootkey = hkey_current_user; if (reg->openkey("\\software\\codehunter", true)) { reg->writestring("picdir",picdir); reg->writeinteger("frequence",frequence); reg->closekey(); } } __finally { delete reg; }this->close();}//--------------------------------------------------------------------------- 源代码载入(http://codehunter.1yes.net/download/lucysaver.zip)