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

  1. /*
  2.  *
  3.  *   This program demonstrates the use of the GetBkColor function. GetBkColor
  4.  *   gets the double word value of the color that goes behind text, etc.
  5.  *   in the given device.
  6.  *
  7.  */
  8.  
  9. #include <windows.h>
  10. #include <stdio.h>
  11.  
  12. #define STR_PUR1    "The purpose of this program is too return the RGB value"
  13. #define STR_PUR2    " that represents the background color. The value is"
  14. #define STR_PUR3    " represented as (rrr, ggg, bbb)."
  15.  
  16. BOOL HelloInit (HANDLE);
  17. int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
  18. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  19.  
  20. char  szVerbage[47] = "The Background color is behind these characters";
  21.  
  22. /* Procedure called when the application is loaded for the first time */
  23. BOOL HelloInit(hInstance)
  24. HANDLE hInstance;
  25. {
  26.     WNDCLASS   rHelloClass;
  27.  
  28.     rHelloClass.hCursor        = LoadCursor(NULL,IDC_ARROW );
  29.     rHelloClass.hIcon            = NULL;
  30.     rHelloClass.lpszMenuName   = (LPSTR)NULL;
  31.     rHelloClass.lpszClassName     = (LPSTR)"Sample Application";
  32.     rHelloClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  33.     rHelloClass.hInstance      = hInstance;
  34.     rHelloClass.style          = CS_HREDRAW | CS_VREDRAW;
  35.     rHelloClass.lpfnWndProc    = HelloWndProc;
  36.  
  37.     if (!RegisterClass((LPWNDCLASS)&rHelloClass))
  38.         /* Initialization failed.
  39.          * Windows will automatically deallocate all allocated memory.
  40.          */
  41.         return FALSE;
  42.  
  43.     return TRUE;        /* Initialization succeeded */
  44. }
  45.  
  46.  
  47. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  48. HANDLE hInstance, hPrevInstance;
  49. LPSTR lpszCmdLine;
  50. int cmdShow;
  51. {
  52.     MSG   msg;
  53.     HWND  hWnd;
  54.     HMENU hMenu;
  55.      char     szPurpose[255];                /* Message box buffers */
  56.     char  szBuff[80];    
  57.     HDC   hDC;                          /*   display context   */
  58.     DWORD dwBkColor;            /* return value from GetBkColr */
  59.  
  60.     HelloInit( hInstance );
  61.     hWnd = CreateWindow((LPSTR)"Sample Application",
  62.                                 (LPSTR)"GetBkColor",
  63.                                 WS_OVERLAPPEDWINDOW,
  64.                                 CW_USEDEFAULT,
  65.                                 CW_USEDEFAULT,
  66.                                 CW_USEDEFAULT,
  67.                                 CW_USEDEFAULT,
  68.                         (HWND)NULL,        /* no parent */
  69.                         (HMENU)NULL,       /* use class menu */
  70.                         (HANDLE)hInstance, /* handle to window instance */
  71.                         (LPSTR)NULL        /* no params to pass on */
  72.                         );
  73.  
  74.     /* Make window visible according to the way the app is activated */
  75.     ShowWindow( hWnd, cmdShow );
  76.     UpdateWindow( hWnd );
  77.  
  78.     hDC = GetDC(hWnd);
  79.  
  80.      sprintf(szPurpose,"%s%s%s\0",STR_PUR1,STR_PUR2,STR_PUR3);
  81.  
  82.     MessageBox(hWnd,(LPSTR)szPurpose,(LPSTR)"GET BACKGROUNDCOLOR",MB_OK);
  83.  
  84.     TextOut(hDC,5,5,(LPSTR)szVerbage,(short)47);
  85.  
  86.     dwBkColor = GetBkColor(hDC);  /* Get the background color of the DC */
  87.  
  88.     sprintf(szBuff,
  89.             "RGB value of the background color: (%hu, %hu, %hu)",
  90.             GetRValue(dwBkColor), GetGValue(dwBkColor),
  91.             GetBValue(dwBkColor));
  92.     MessageBox(hWnd, (LPSTR)szBuff, (LPSTR)"GET BACKGROUND COLOR", MB_OK);
  93.  
  94.     SetBkColor(hDC,RGB(0x00,0x00,0x00)); /* change the background to black */
  95.     SetTextColor(hDC,RGB(0xff,0xff,0xff));
  96.     TextOut(hDC,5,25,(LPSTR)szVerbage,(short)47);
  97.  
  98.     dwBkColor = GetBkColor(hDC);  /* Get the background color of the DC */
  99.  
  100.     sprintf(szBuff,
  101.             "RGB value of the background color: (%hu, %hu, %hu)",
  102.             GetRValue(dwBkColor), GetGValue(dwBkColor),
  103.             GetBValue(dwBkColor));
  104.     MessageBox(hWnd, (LPSTR)szBuff, (LPSTR)"GET BACKGROUND COLOR", MB_OK);
  105.  
  106.     ReleaseDC(hWnd,hDC);
  107.  
  108.     /* Polling messages from event queue */
  109.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  110.         TranslateMessage((LPMSG)&msg);
  111.         DispatchMessage((LPMSG)&msg);
  112.         }
  113.  
  114.     return (int)msg.wParam;
  115. }
  116.  
  117. /* Procedures which make up the window class. */
  118. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  119. HWND hWnd;
  120. unsigned message;
  121. WORD wParam;
  122. LONG lParam;
  123. {
  124.     PAINTSTRUCT ps;
  125.  
  126.     switch (message)
  127.     {
  128.     case WM_SYSCOMMAND:
  129.     return DefWindowProc(hWnd,message,wParam,lParam);
  130.         break;
  131.  
  132.     case WM_DESTROY:
  133.         PostQuitMessage(0);
  134.         break;
  135.  
  136.     case WM_PAINT:
  137.         BeginPaint(hWnd,(LPPAINTSTRUCT)&ps);
  138.         EndPaint(hWnd,(LPPAINTSTRUCT)&ps);
  139.         break;
  140.  
  141.     default:
  142.         return DefWindowProc(hWnd,message,wParam,lParam );
  143.         break;
  144.     }
  145.     return(0L);
  146. }
  147.