home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / setpixel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.0 KB  |  88 lines

  1. /*
  2.  *  SetPixel
  3.  *  setpixel.c
  4.  *
  5.  *  This program demonstrates the use of the function SetPixel.
  6.  *  It sets the pixel at the point specified by the X and Y coordinates 
  7.  *  to the closest approxmiation to the color specified by the rgbcolor.
  8.  *  The point must be in the clipping region. If the point is not in the
  9.  *  clipping region, the function is ignored.
  10.  *
  11.  */
  12.  
  13. #include "windows.h"
  14.  
  15. /* Registering the parent window class */
  16. BOOL WinInit( hInstance )
  17. HANDLE hInstance;
  18. {
  19.     WNDCLASS   wcClass;
  20.  
  21.     /* registering the parent window class */
  22.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  23.     wcClass.hIcon          = LoadIcon (hInstance, (LPSTR)"WindowIcon");
  24.     wcClass.lpszMenuName   = (LPSTR)NULL;
  25.     wcClass.lpszClassName  = (LPSTR)"Setpixel";
  26.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  27.     wcClass.hInstance      = hInstance;
  28.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  29.     wcClass.lpfnWndProc    = DefWindowProc;
  30.     wcClass.cbClsExtra     = 0;
  31.     wcClass.cbWndExtra     = 0;
  32.  
  33.     RegisterClass( (LPWNDCLASS) &wcClass );
  34.     return TRUE;        /* Initialization succeeded */
  35. }
  36.  
  37.  
  38. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  39. HANDLE hInstance, hPrevInstance;
  40. LPSTR lpszCmdLine;
  41. int cmdShow;
  42. {
  43.     HWND  hWnd;                   /* Handles to the windows        */
  44.     HDC   hDC;                    /* Display context of the window */
  45.     short nPos;                   /* X-coordinate of the pixel     */
  46.     LONG  nResult;                /* return value of SetPixel      */
  47.  
  48.     WinInit (hInstance);
  49.  
  50.     /* creating the window */
  51.     hWnd = CreateWindow((LPSTR)"Setpixel",
  52.                         (LPSTR)"Setting Pixel",
  53.                         WS_OVERLAPPEDWINDOW,
  54.                         50,                /* x         */
  55.                         50,                /* y         */
  56.                         600,               /* width     */
  57.                         250,               /* height    */
  58.                         (HWND)NULL,        /* no parent */
  59.                         (HMENU)NULL,       /* use class menu */
  60.                         (HANDLE)hInstance, /* handle to window instance */
  61.                         (LPSTR)NULL        /* no params to pass on */
  62.                        );
  63.  
  64.     /* Make window visible according to the way the app is activated */
  65.     ShowWindow( hWnd, cmdShow );
  66.     UpdateWindow( hWnd );
  67.  
  68.     /* Getting the display context and draw a line by setting a series of 
  69.      * pixels in the client area. The rgbcolor is set by using RGB macro.
  70.      */
  71.     hDC = GetDC (hWnd);
  72.     
  73.     for (nPos = 50; nPos < 550; nPos++)
  74.       {
  75.         nResult = SetPixel (hDC, nPos, 50, RGB (0,0,0));
  76.         if (nResult == -1)
  77.           MessageBox (hWnd, (LPSTR)"SetPixel Function Failed", 
  78.                       (LPSTR)"ERROR!", MB_OK);
  79.       }
  80.  
  81.     ReleaseDC (hWnd, hDC);
  82.  
  83.     MessageBox (hWnd, (LPSTR)"Using SetPixel to draw a line",
  84.                 (LPSTR)"Done", MB_OK);
  85.  
  86.     return 0;
  87. }
  88.