黑客帝国屏保图代码

来源:互联网 发布:海口数据共享交换平台 编辑:程序博客网 时间:2024/04/30 18:54
黑客帝国屏幕保护设置

1. 打开VS2010或以上版本,新建—>项目,Visual C++,win32,选择win32控制台应用程序,名称填写为"hacker"。

2. 将以下代码复制到项目源文件中


[cpp] view plain copy
print?
  1. // hacker_console.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3. #include "stdafx.h"  
  4. #include <windows.h>  
  5.   
  6. #define ID_TIMER    1   
  7. #define STRMAXLEN  25 //一个显示列的最大长度   
  8. #define STRMINLEN  8  //一个显示列的最小长度  
  9.   
  10. LRESULT CALLBACK WndProc (HWNDUINTWPARAMLPARAM) ;   
  11. //////////////////////////////////////////////////////////////////   
  12. //////////////////////////////////////////////////////////////////   
  13. typedef struct tagCharChain //整个当作屏幕的一个显示列,这是个双向列表   
  14. {   
  15.     struct tagCharChain *prev; //链表的前个元素   
  16.     TCHAR  ch;                  //一个显示列中的一个字符   
  17.     struct tagCharChain *next; //链表的后个元素   
  18. }CharChain, *pCharChain;  
  19.   
  20. typedef struct tagCharColumn   
  21. {   
  22.     CharChain *head, *current, *point;   
  23.     int x, y, iStrLen; //显示列的开始显示的x,y坐标,iStrLen是这个列的长度   
  24.     int iStopTimes, iMustStopTimes; //已经停滞的次数和必须停滞的次数,必须停滞的次数是随机的   
  25. }CharColumn, *pCharColumn;  
  26.   
  27. int main(HINSTANCE hInstance, HINSTANCE hPrevInstance,   
  28.     PSTR szCmdLine, int iCmdShow)   
  29. {   
  30.     static TCHAR szAppName[] = TEXT ("matrix") ;   
  31.     HWND            hwnd ;   
  32.     MSG            msg ;   
  33.     WNDCLASS    wndclass ;  
  34.   
  35.     wndclass.style                = CS_HREDRAW | CS_VREDRAW ;   
  36.     wndclass.lpfnWndProc        = WndProc ;   
  37.     wndclass.cbClsExtra        = 0 ;   
  38.     wndclass.cbWndExtra        = 0 ;   
  39.     wndclass.hInstance        = hInstance ;  
  40.   
  41.     wndclass.hIcon                = LoadIcon (NULL, IDI_APPLICATION) ;   
  42.     wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;   
  43.     wndclass.hbrBackground        = (HBRUSH) GetStockObject (BLACK_BRUSH) ;   
  44.     wndclass.lpszMenuName        = NULL ;   
  45.     wndclass.lpszClassName        = szAppName ;  
  46.   
  47.     if(!RegisterClass (&wndclass))   
  48.     {   
  49.         MessageBox (NULL, TEXT ("此程序必须运行在NT下!"), szAppName, MB_ICONERROR) ;   
  50.         return 0;   
  51.     }  
  52.   
  53.     hwnd = CreateWindow (szAppName, NULL,   
  54.         WS_DLGFRAME | WS_THICKFRAME | WS_POPUP,   
  55.         0, 0,   
  56.         GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),   
  57.         NULL, NULL, hInstance,   
  58.         NULL) ;  
  59.   
  60.     ShowWindow (hwnd, SW_SHOWMAXIMIZED) ; //最大化显示   
  61.     UpdateWindow (hwnd) ;   
  62.     ShowCursor(FALSE); //隐藏鼠标光标   
  63.   
  64.     srand ((int) GetCurrentTime ()) ; //初始化随机数发生器   
  65.     while (GetMessage (&msg, NULL, 0, 0))   
  66.     {   
  67.         TranslateMessage (&msg) ;   
  68.         DispatchMessage (&msg) ;   
  69.     }   
  70.     ShowCursor(TRUE); //显示鼠标光标   
  71.     return msg.wParam ;   
  72. }  
  73.   
  74. TCHAR randomChar() //随机字符产生函数   
  75. {   
  76.     return (TCHAR)(rand()%2+0x30); //33到126之间   
  77. }  
  78.   
  79. int init(CharColumn *cc, int cyScreen, int x) //初始化   
  80. {   
  81.     int j;   
  82.     cc->iStrLen = rand()%(STRMAXLEN-STRMINLEN) + STRMINLEN; //显示列的长度   
  83.     cc->x = x+3 ;        //显示列的开始显示的x坐标   
  84.     cc->y =rand()%3?rand()%cyScreen:0; //显示列的开始显示的y坐标   
  85.     cc->iMustStopTimes = rand()%6 ;   
  86.     cc->iStopTimes    = 0 ;   
  87.     cc->head = cc->current =   
  88.         (pCharChain)calloc(cc->iStrLen, sizeof(CharChain)); //生成显示列   
  89.     for(j=0; j<cc->iStrLen-1; j++)   
  90.     {   
  91.         cc->current->prev = cc->point; //cc->point一个显示列的前个元素   
  92.         cc->current->ch  = '\0';   
  93.         cc->current->next = cc->current+1; //cc->current+1一个显示列的后个元素   
  94.         cc->point          = cc->current++; //cc->point = cc->current; cc->current++;   
  95.     }   
  96.     cc->current->prev = cc->point; //最后一个节点   
  97.     cc->current->ch  = '\0';   
  98.     cc->current->next = cc->head;   
  99.     cc->head->prev    = cc->current; //头节点的前一个为此链的最后一个元素  
  100.   
  101.     cc->current = cc->point = cc->head; //free掉申请的内存要用current当参数   
  102.     cc->head->ch = randomChar(); // 对链表头的 元素填充   
  103.     return 0;   
  104. }  
  105.   
  106. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)   
  107. {   
  108.     HDC          hdc ;   
  109.     //ctn 用来确定一个显示链是否 向下前进,如果等待次数超过必须等待的次数,ctn就代表要向下前进   
  110.     int i, j, temp, ctn; //j为一个显示链中除链表头外的在屏幕上显示的y坐标,temp绿色过度到黑色之用   
  111.     static  HDC hdcMem;   
  112.     HFONT    hFont;   
  113.     static  HBITMAP hBitmap;   
  114.     static  int cxScreen, cyScreen; //屏幕的宽度 高度.   
  115.     static  int iFontWidth=10, iFontHeight=15, iColumnCount; //字体的宽度 高度, 列数   
  116.     static  CharColumn *ccChain;  
  117.   
  118.     switch (message)   
  119.     {   
  120.     case WM_CREATE:   
  121.         cxScreen = GetSystemMetrics(SM_CXSCREEN) ; //屏幕宽度   
  122.         cyScreen = GetSystemMetrics(SM_CYSCREEN) ;   
  123.         SetTimer (hwnd, ID_TIMER, 10, NULL) ;  
  124.   
  125.         hdc = GetDC(hwnd);   
  126.         hdcMem = CreateCompatibleDC(hdc);   
  127.         hBitmap = CreateCompatibleBitmap(hdc, cxScreen, cyScreen);   
  128.         SelectObject(hdcMem, hBitmap);   
  129.         ReleaseDC(hwnd, hdc);   
  130.         //创建字体   
  131.         hFont = CreateFont(iFontHeight, iFontWidth-5, 0, 0, FW_BOLD, 0, 0, 0,   
  132.             DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,   
  133.             DRAFT_QUALITY, FIXED_PITCH | FF_SWISS, TEXT("Fixedsys"));   
  134.         SelectObject(hdcMem, hFont);   
  135.         DeleteObject (hFont) ;   
  136.         SetBkMode(hdcMem, TRANSPARENT); //设置背景模式为 透明   
  137.         iColumnCount = cxScreen/(iFontWidth*3/2); //屏幕所显示字母雨的列数  
  138.   
  139.         ccChain = (pCharColumn)calloc(iColumnCount, sizeof(CharColumn));   
  140.         for(i=0; i<iColumnCount; i++)   
  141.         {   
  142.             init(ccChain+i, cyScreen, (iFontWidth*3/2)*i);   
  143.         }   
  144.         return 0 ;  
  145.   
  146.     case WM_TIMER:   
  147.         hdc = GetDC(hwnd);   
  148.         PatBlt (hdcMem, 0, 0, cxScreen, cyScreen, BLACKNESS) ; //将内存设备映像刷成黑色   
  149.         for(i=0; i<iColumnCount; i++)   
  150.         {   
  151.             ctn = (ccChain+i)->iStopTimes++ > (ccChain+i)->iMustStopTimes;   
  152.             //   
  153.             (ccChain+i)->point = (ccChain+i)->head; //point用于遍历整个显示列   
  154.   
  155.             //第一个字符显示为 白色   
  156.             SetTextColor(hdcMem, RGB(255, 255, 255));   
  157.             TextOut(hdcMem, (ccChain+i)->x, (ccChain+i)->y, &((ccChain+i)->point->ch), 1);   
  158.             j = (ccChain+i)->y;    
  159.             (ccChain+i)->point = (ccChain+i)->point->next;   
  160.             //遍历整个显示列,将这个显示列里的字符从下往上显示   
  161.             temp = 0 ; //temp绿色过度到黑色之用   
  162.             while((ccChain+i)->point != (ccChain+i)->head && (ccChain+i)->point->ch)   
  163.             {   
  164.                 SetTextColor(hdcMem, RGB(0, 255-(255*(temp++)/(ccChain+i)->iStrLen), 0));   
  165.                 TextOut(hdcMem, (ccChain+i)->x, j-=iFontHeight, &((ccChain+i)->point->ch), 1);   
  166.                 (ccChain+i)->point = (ccChain+i)->point->next;   
  167.             }   
  168.             if(ctn)   
  169.                 (ccChain+i)->iStopTimes = 0 ;   
  170.             else continue;   
  171.             (ccChain+i)->y += iFontHeight; //下次开始显示的y坐标 为当前的y坐标加上 一个字符的高度   
  172.             //如果开始显示的y坐标减去 整个显示列的长度超过了屏幕的高度   
  173.             if( (ccChain+i)->y-(ccChain+i)->iStrLen*iFontHeight > cyScreen)   
  174.             {   
  175.                 free( (ccChain+i)->current );   
  176.                 init(ccChain+i, cyScreen, (iFontWidth*3/2)*i);   
  177.             }   
  178.             //链表的头 为此链表的前个元素,因为下次开始显示的时候 就相当与在整个显示列的开头添加个元素,然后在开始往上显示   
  179.             (ccChain+i)->head = (ccChain+i)->head->prev;   
  180.             (ccChain+i)->head->ch = randomChar();   
  181.         }   
  182.   
  183.         BitBlt(hdc, 0, 0, cxScreen, cyScreen, hdcMem, 0, 0, SRCCOPY);   
  184.         ReleaseDC(hwnd, hdc);   
  185.         return 0;  
  186.   
  187.     case WM_RBUTTONDOWN:   
  188.         KillTimer (hwnd, ID_TIMER) ;   
  189.         return 0;  
  190.   
  191.     case WM_RBUTTONUP:   
  192.         SetTimer (hwnd, ID_TIMER, 10, NULL) ;   
  193.         return 0;  
  194.   
  195.         //处理善后工作   
  196.     case WM_KEYDOWN:   
  197.     case WM_LBUTTONDOWN:   
  198.     case WM_DESTROY:   
  199.         KillTimer (hwnd, ID_TIMER) ;   
  200.         DeleteObject(hBitmap);   
  201.         DeleteDC(hdcMem);   
  202.         for(i=0; i<iColumnCount; i++)   
  203.         {   
  204.             free( (ccChain+i)->current );   
  205.         }   
  206.         free(ccChain);   
  207.         PostQuitMessage (0) ;   
  208.         return 0 ;   
  209.     }   
  210.     return DefWindowProc (hwnd, message, wParam, lParam) ;   
  211. }  

原代码基础上修改一处错别字、修改一处随机字符为0或者1。


得到的屏保截图如下,确实很酷呀:

0 0
原创粉丝点击