home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / blanker / spextn.zip / STICKS.C < prev    next >
Text File  |  1990-06-15  |  4KB  |  208 lines

  1. /* Sample source for STICKS screen saver for Screen Peace */
  2. /* Copyright 1990 Anthony Andersen                        */
  3.  
  4. #include <windows.h>
  5.  
  6. #include "sticks.h"
  7.  
  8. #define IDD_CHECKBOX 10
  9. #define IDD_EDIT     11
  10.  
  11. unsigned long seed;
  12. char buffer[10];
  13. int  enabled;
  14. unsigned erasecount;
  15.  
  16. char savername[] = "Sticks";
  17. char appname[] = "Screen Peace";
  18. char keyname[] = "Sticks On";
  19. char key2name[] = "Num Sticks";
  20.  
  21. /* this is called by the default dll entry code during the  */
  22. /* load library call - always happens BEFORE any other call */
  23. /* in the dll.                                              */
  24.  
  25. int FAR PASCAL LibMain(hModule, wDataSeg, cbHeapSize, lpszCmdLine)
  26.  
  27. HANDLE    hModule;
  28. WORD     wDataSeg;
  29. WORD     cbHeapSize;
  30. LPSTR    lpszCmdLine;
  31.  
  32. {
  33.  
  34.    /* set up things here */
  35.  
  36.     erasecount = GetProfileInt(appname,key2name,250);
  37.     GetProfileString(appname,keyname,"y",buffer,9);
  38.     if (buffer[0] != 'n' && buffer[0] != 'N') {
  39.         enabled = TRUE;
  40.     }
  41.     return (1);
  42. }
  43.  
  44.  
  45. /* this is called by Windows when the dll is released */
  46.  
  47. int FAR PASCAL WEP (bSystemExit)
  48. int  bSystemExit;
  49.  
  50. {
  51.     /* get rid of things you allocated in the LibMain proc here */
  52.  
  53.     return (1);
  54. }
  55.  
  56. /* this is called (probably lots of times) by Screen Peace */
  57. /* be careful not to allocate memory or objects here       */
  58.  
  59. char FAR * FAR PASCAL saverinit(BOOL far *savenabled)
  60.  
  61. {
  62.     /* tell enable state */
  63.  
  64.     *savenabled = enabled;
  65.  
  66.     /* You must return either the saver's name or NULL */
  67.  
  68.     return(savername);
  69. }
  70.  
  71. /* dialog proc for user customization */
  72.  
  73. BOOL FAR PASCAL saverdlgproc(HWND hdlg,unsigned message,WORD wparam,LONG lparam)
  74.  
  75. {
  76.     switch (message) {
  77.  
  78.     case WM_INITDIALOG:
  79.  
  80.         /* set the dialog items */
  81.  
  82.         SetDlgItemInt(hdlg,IDD_EDIT,erasecount,FALSE);
  83.         SendDlgItemMessage(hdlg,IDD_CHECKBOX,BM_SETCHECK,enabled,0);
  84.         return (TRUE);
  85.  
  86.     case WM_COMMAND:
  87.  
  88.         if (wparam == IDOK) {
  89.  
  90.             /* get the new dialog items */
  91.  
  92.             erasecount = GetDlgItemInt(hdlg,IDD_EDIT,NULL,FALSE);
  93.             enabled = (int)SendDlgItemMessage(hdlg,IDD_CHECKBOX,BM_GETCHECK,0,0);
  94.  
  95.             /* write the profile information */
  96.  
  97.             if (enabled) buffer[0] = 'y';
  98.             else buffer[0] = 'n';
  99.             buffer[1] = '\0';
  100.             WriteProfileString(appname,keyname,buffer);
  101.             wsprintf(buffer,"%u",erasecount);
  102.             WriteProfileString(appname,key2name,buffer);
  103.  
  104.             /* return value in EndDialog doesn't matter */
  105.  
  106.             EndDialog(hdlg,TRUE);
  107.             return (TRUE);
  108.         }
  109.         else if (wparam == IDCANCEL) {
  110.  
  111.             /* just return - return value in EndDialog doesn't matter */
  112.  
  113.             EndDialog(hdlg,FALSE);
  114.             return (TRUE);
  115.         }
  116.         break;
  117.  
  118.     }
  119.  
  120.     return (FALSE);
  121. }
  122.  
  123. /* returns pseudorandom number from 0 to x-1 */
  124.  
  125. int arand(int x)
  126. {
  127.     seed = seed*0x343fd+0x269ec3;
  128.     return (int)(((seed>>16)&0x7fff)*x>>15);
  129. }
  130.  
  131. /* the main drawing routine */
  132.  
  133. VOID FAR PASCAL saverdraw(HWND hwnd,HDC hdc,HANDLE hinst,BOOL (FAR PASCAL *yieldproc)(VOID)) 
  134.  
  135. {
  136.     RECT rect;
  137.     HPEN hpen;
  138.     HPEN hOldPen;
  139.     HBRUSH hbrush;
  140.     int  x1,x2,y1,y2;
  141.     int  pw,pc;
  142.     unsigned count;
  143.  
  144.     /* the rect will fill the screen */
  145.  
  146.     GetWindowRect(hwnd,&rect);
  147.  
  148.     /* seed the pseudorandom number generator */
  149.  
  150.     seed = GetTickCount();
  151.  
  152.     /* get black brush for erasing screen */
  153.  
  154.     hbrush = GetStockObject(BLACK_BRUSH);
  155.  
  156.     count = 0;
  157.  
  158.     /* You MUST "black out" the screen if you want the screen blacked out */
  159.  
  160.     FillRect(hdc,&rect,hbrush);
  161.  
  162.     /* make sure to call yieldproc OFTEN - windows is locked up until */
  163.     /* yieldproc is called.                                           */
  164.  
  165.     while ((*yieldproc)()) {
  166.  
  167.         /* get "random" color and width */
  168.  
  169.          pc = arand(16);
  170.         pw = arand(10);
  171.  
  172.         /* select pen and save old pen */
  173.  
  174.         hpen = CreatePen(PS_SOLID,pw,pc+0x1000000);
  175.         hOldPen = SelectObject(hdc,hpen);
  176.  
  177.         /* get coordinates of endpoints of line */
  178.  
  179.         x1 = arand(rect.right);
  180.         x2 = arand(rect.right);
  181.         y1 = arand(rect.bottom);
  182.         y2 = arand(rect.bottom);
  183.  
  184.         /* draw line */
  185.  
  186.         MoveTo(hdc,x1,y1);
  187.         LineTo(hdc,x2,y2);
  188.  
  189.         /* delete pen */
  190.  
  191.         SelectObject(hdc,hOldPen);
  192.         DeleteObject(hpen);
  193.  
  194.         /* black out screen if appropriate */
  195.  
  196.         count++;
  197.         if (count == erasecount) {
  198.             count = 0;
  199.             FillRect(hdc,&rect,hbrush);
  200.         }
  201.     }
  202.  
  203. }
  204.  
  205.  
  206. 
  207. 
  208.