百度首页 | 百度空间
 
查看文章
 
Windows编程-锁屏幕(原创)
2008年05月18日 星期日 17:39

    昨天在无聊,逛论坛无意中发现了一个小东西,就是锁屏幕,好像是 雨林?还是霏凡 记不太清楚了,

看看效果还行,想想自己应该也可以写出来,于是自己花了大半天的时间就写了这样一个,还好吧。

总体上比较使用,美观,是MM的界面。

登陆界面:

锁屏幕界面:

源代码:

#include <windows.h>
#include <stdio.h>
#include "stdlib.h"
#include "resource.h"
#include "winuser.h"


CONST   RECT   lpRect={1024,768,1024,768};

HINSTANCE hInst;
char szChar[20];
char passd[20];
static int Pstate=0;
bool Is_cut=0;

static HBITMAP map;     //静态的 不是静态,map的值会被释放
static BITMAP bm;
//Bitmap memBmp;    //内存位图

static HDC hdcMem;
static HDC hdc;

LRESULT CALLBACK WinSunProc(
        HWND hwnd,      // 窗口句柄
        UINT uMsg,      // 消息id
        WPARAM wParam, // 附加信息
        LPARAM lParam   // 附加信息
        );

int WINAPI WinMain(
       HINSTANCE hInstance,      // 应用程序实例
       HINSTANCE hPrevInstance, //
       LPSTR lpCmdLine,          // 命令行参数
       int nCmdShow              // 显示模式
       )
{
hInst=hInstance;
char text[256];
//定义窗口类
////////////////////////////////////////////////////////////////////////////////////////
WNDCLASS win;
win.style=/*WS_CAPTION|*/CS_VREDRAW ;//类型窗口的样式 风格 水平重画和垂直重画
win.lpfnWndProc=WinSunProc;//窗口过程函数(回调函数)函数指针

win.cbClsExtra=0;   // 一般为0   附加内存空间
win.cbWndExtra=0;   // 一般为0   窗口附加内存
win.hbrBackground=(HBRUSH)GetStockObject(0);//背景画刷句柄
win.hCursor=LoadCursor(NULL,IDC_CROSS); //光标句柄
win.hIcon=LoadIcon(NULL,IDI_ERROR); //窗口类的图标句柄
win.hInstance=hInstance;            //包含窗口过程的实例句柄

win.lpszClassName="ClassName_zhy";//窗口类名

win.lpszMenuName=NULL ;

/////////////////////////////////////////////////////////////////////////////////////////////
//定义窗口类
RegisterClass(&win);//注册窗口类

HWND hwnd;
hwnd=CreateWindow("ClassName_zhy","zhy",WS_POPUP,//WS_POPUP 没窗体标题栏
   NULL,NULL,600,400,NULL,NULL,hInstance,NULL); //创建窗口
ShowWindow(hwnd,SW_SHOWMAXIMIZED );//最大面积显示窗口(覆盖任务栏)
UpdateWindow(hwnd);//更新窗口


//半透明处理
//////////////////////////////////////////////////////////////////////////
SetWindowLong(hwnd,GWL_EXSTYLE,GetWindowLong(hwnd,GWL_EXSTYLE)^0x80000);  
HINSTANCE hInst1 = LoadLibrary("User32.dll");
if(hInst1)
{           
   typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);         
   MYFUNC fun = NULL;
   //取得SetLayeredWindowAttributes函数指针    
   fun=(MYFUNC)GetProcAddress(hInst1, "SetLayeredWindowAttributes");
   fun(hwnd,0,220,2);// 参数3 0~255(不透明)
   FreeLibrary(hInst1);
}

//////////////////////////////////////////////////////////////////////////
MSG msg;

while(GetMessage(&msg,NULL,0,0))//接受所有的消息
{
   if (Is_cut)
   ClipCursor(&lpRect);
   
   SetForegroundWindow(hwnd);
/* HWND close=(::GetForegroundWindow());
  
   HWND focusWindow=::GetForegroundWindow();*/
   if(!::IsWindow(hwnd))
   {
    ::EnableWindow(hwnd,FALSE);  
    ::EnableWindow(FindWindow("ClassName_zhy",NULL),false);
   
   }

   TranslateMessage(&msg);//解析消息
   DispatchMessage(&msg); //派发消息
   SetForegroundWindow(hwnd);
   //SystemParametersInfo(SPI_SCREENSAVERRUNNING,1,NULL,0);
   //SetWindowPos(hwnd,   NULL,   0,   0,   0,   0,   SW_SHOWMAXIMIZED);  

}
/////////////////////////////////////////////
  

return 0;
}

LRESULT CALLBACK WinSunProc(   //回掉函数
        HWND hwnd,      // handle to window
        UINT uMsg,      // message identifier
        WPARAM wParam, // first message parameter
        LPARAM lParam   // second message parameter
        )
{
static int n,m,k;
int x,y;
static int len=0;
switch(uMsg)
{
case WM_SYSKEYDOWN:

case WM_CREATE:
   k=0;
   map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP1);
   break;
case WM_KEYDOWN:

   //switch(wParam)
   //{
   //case VK_ESCAPE:
   // DestroyWindow(hwnd);
   // break;
   //case VK_LWIN:

   // return 1;
   //case VK_SPACE:
    //break;
   //}

   break;
case WM_CHAR: //接受按键消息
  
   char str[2];

   if (!Pstate)
   {
    if (strlen(szChar)>5)
     return 1;
    sprintf(str,"%c",wParam);
    if (str[0]==13)
    {
     if (strlen(szChar)==0)
     {
      MessageBox(hwnd,"密码不可以为空!","新会以冷-锁屏",MB_OK);
      return 1;
     }
     map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP2);
     Is_cut=true;

     Pstate=1;
     SetTimer(hwnd,1,2200,0);

    }
    if (str[0]==8|str[0]==27)
    {
     strcpy(szChar,"");
     ::InvalidateRect(hwnd,NULL,true);
     break;
    }
    if (str[0]<123&&str[0]>47)
    {
     strcat(szChar,str);
     ::InvalidateRect(hwnd,NULL,true);
    }
   
   }else{
    if (strlen(passd)>5)
     return 1;
    char str[2];
    sprintf(str,"%c",wParam);
    strcat(passd,str);
    if (str[0]==szChar[len])
    {
     len++;
     if (len==(strlen(szChar)))
     {
      DestroyWindow(hwnd);
     }
    }else{
     len=0;
     strcpy(passd,"");
     map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP5);
     Pstate=2;
    }
  
   }
   ::InvalidateRect(hwnd,NULL,true);
   break;
case WM_TIMER:
   if (!k)
   {
    map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP2);
   }
   k=1;
   m++;
   if (m>9)
   {
    if (n%3==0)
    {
     map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP2);
    }
    if (n%3==1)
    {
     map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP3);
    }
    if (n%3==2)
    {
     map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP4);
    }
    n++;
    m=0;
   }

   ::InvalidateRect(hwnd,NULL,true);
   break;
case WM_RBUTTONDOWN://接受鼠标右健消息
  
case WM_LBUTTONDOWN: //得到当鼠标左键按下时的鼠标位置
   /* 699 306
    717 322

    612 348
    694 370*/
   x = LOWORD(lParam);
   y = HIWORD(lParam);

/* char info[20];
   sprintf(info,"%d,%d",x,y);
   MessageBox(hwnd,info,"鼠标左键",MB_OK);*/
   //点关闭
   if (x>699&&x<717&&y>306&&y<322)
   {
    DestroyWindow(hwnd);
   }
   //确定按钮
   if (x>612&&x<694&&y>348&&y<370)
   {
    if (strlen(szChar)==0)
    {
     MessageBox(hwnd,"密码不可以为空!","窗口标题",MB_OK);
     return 1;
    }
    map=LoadBitmap(hInst,(LPCSTR)IDB_BITMAP2);
    Is_cut=true;
  
    Pstate=1;
    SetTimer(hwnd,1,1200,0);
   }
   ::InvalidateRect(hwnd,NULL,true);
   break;
case WM_PAINT://接受绘制消息

   PAINTSTRUCT ps;
   hdc=BeginPaint(hwnd,&ps);

  
   hdcMem =::CreateCompatibleDC(hdc);

   ::SelectObject(hdcMem,map);

   if (!m)
   {
    BitBlt(hdc,300,300,500,300,hdcMem,0,0,SRCCOPY);
   }
   if (Pstate==0)
   {
    BitBlt(hdc,300,300,500,300,hdcMem,0,0,SRCCOPY);
    TextOut(hdc,416,352,"*********",strlen(szChar));
    //TextOut(hdc,416,382,"请牢记密码,想要恢复时只要正确的连续输入密码即可!",strlen("请牢记密码,想要恢复时只要正确的连续输入密码即可!"));
   }
   if (Pstate==1|k==1)
   {
    BitBlt(hdc,0,0,1024,768,hdcMem,0,0,SRCCOPY);
   }

   DeleteDC(hdcMem);
   EndPaint(hwnd,&ps);
   break;
case WM_CLOSE://接受关闭消息
   //if(IDYES==MessageBox(hwnd,"是否真的结束?","窗口标题",MB_YESNO))
   //{
   // //DestroyWindow(hwnd);
   //}
   break;
case WM_DESTROY://接受销毁窗口消息
   PostQuitMessage(0);
   break;
default:
   return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
return 0;
}

源代码下载:点我

锁屏幕程序下载:点我


类别:c++ | 添加到搜藏 | 浏览() | 评论 (1)
 
最近读者:
 
网友评论:
1
2008年05月24日 星期六 11:55
为什么不能下载呢???等老半天,也不可以,楼主去YLMF看看怎么回事情!!!
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu