home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / _bbs3 / f1530.zip / CLRTEST.C < prev    next >
C/C++ Source or Header  |  1991-08-25  |  11KB  |  382 lines

  1. /*
  2.  
  3. Main application module for test of color selector custom control
  4.  
  5. (C) Scott Gourley/Clickon Software, 1991.
  6.  
  7. */
  8.  
  9. #include <windows.h>
  10.  
  11. #include "clrctrl.h"
  12. #include "clrtest.h"
  13.  
  14. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  15.  
  16. COLORREF    clrText = CLRTEST_TEXT_COLOR;
  17. COLORREF    clrBack = CLRTEST_BACKGROUND_COLOR;
  18.  
  19. int PASCAL WinMain
  20.     (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
  21. {
  22.     static char szAppName [] = "ClrTest";
  23.  
  24.     WNDCLASS    wndclass;
  25.     HANDLE      hClrLib;
  26.     HANDLE      hAccel;
  27.     HWND        hWnd;
  28.     MSG         msg;
  29.  
  30.     /* load custom control DLL library, end program on error */
  31.  
  32.     if ((hClrLib = LoadLibrary (CLRCTRL_DLLNAME)) < 32)
  33.         return 0;
  34.  
  35.     /* register the window class if this is the only program instance */
  36.     
  37.     if (!hPrevInstance)
  38.     {
  39.         /* register the frame window class */
  40.  
  41.         wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  42.         wndclass.lpfnWndProc    = WndProc;
  43.         wndclass.cbClsExtra     = 0;
  44.         wndclass.cbWndExtra     = 0;
  45.         wndclass.hInstance      = hInstance;
  46.         wndclass.hIcon          = LoadIcon (hInstance, szAppName);
  47.         wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  48.         wndclass.hbrBackground  = NULL;
  49.         wndclass.lpszMenuName   = szAppName;
  50.         wndclass.lpszClassName  = szAppName;
  51.     
  52.         RegisterClass (&wndclass);      
  53.     }
  54.  
  55.     /* create the application window */
  56.  
  57.     hWnd = CreateWindow (szAppName, "Color Selector Test", 
  58.                                 WS_OVERLAPPEDWINDOW,
  59.                                 CW_USEDEFAULT, CW_USEDEFAULT,
  60.                                 240, 100,
  61.                                 NULL, NULL, hInstance, NULL);
  62.  
  63.     /* load the keyboard accelerators */
  64.                             
  65.     hAccel = LoadAccelerators (hInstance, "Accelerators");
  66.   
  67.     /* make the frame window visible */
  68.     
  69.     ShowWindow (hWnd, nCmdShow);
  70.     UpdateWindow (hWnd);
  71.     
  72.     /* enter the message loop */
  73.     
  74.     while (GetMessage (&msg, NULL, 0, 0))
  75.     {
  76.         /* if there is no accel key to translate, process the message */
  77.         
  78.         if (!hAccel || !TranslateAccelerator (hWnd, hAccel, &msg))
  79.         {
  80.             TranslateMessage (&msg);
  81.             DispatchMessage (&msg);
  82.         }
  83.     }
  84.  
  85.     FreeLibrary (hClrLib);
  86.     
  87.     return msg.wParam;
  88. }
  89.  
  90. BOOL FAR PASCAL ClrTestDlgProc
  91.     (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  92. {
  93.     HWND            hClrCtrl;
  94.  
  95.     switch (wMsg)
  96.     {
  97.         case WM_INITDIALOG:
  98.  
  99.             /* get a handle to the text color selector */
  100.             
  101.             hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_TEXT);
  102.  
  103.             /* delete the dark magenta item, just for fun */
  104.  
  105.             SendMessage (hClrCtrl, CB_DELETESTRING, 5, 0L);
  106.             
  107.             /* put a sky-blue item at position 3, just for fun */
  108.  
  109.             SendMessage (hClrCtrl, CB_INSERTSTRING, 3,
  110.                 RGB (0x00, 0x80, 0xFF));
  111.             
  112.             /* set the default text color (to clrText's initial value) */
  113.  
  114.             SendMessage (hClrCtrl, CLRM_SETCURCOLOR, 0, clrText);
  115.  
  116.             /* get a handle to the background color selector */
  117.  
  118.             hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_BACKGROUND);
  119.  
  120.             /* add an orangish item to the end of the list, just for fun */
  121.             
  122.             SendMessage (hClrCtrl, CB_ADDSTRING, 0,
  123.                 RGB (0xC0, 0x40, 0x00));
  124.             
  125.             /* set the default background color (to clrBack's init value) */
  126.  
  127.             SendMessage (hClrCtrl, CLRM_SETCURCOLOR, 0, clrBack);
  128.  
  129.             return TRUE;
  130.  
  131.             break;
  132.             
  133.         case WM_COMMAND:
  134.             switch (wParam)
  135.             {
  136.                 case IDOK:
  137.  
  138.                     /* access the text color selector... */
  139.  
  140.                     hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_TEXT);
  141.                     
  142.                     /* ...and retrieve the current color setting */
  143.  
  144.                     clrText = SendMessage (hClrCtrl, CLRM_GETCURCOLOR, 0, 0L);
  145.  
  146.                     /* access the background color selector... */
  147.                     
  148.                     hClrCtrl = GetDlgItem (hDlg, ID_CLRSEL_BACKGROUND);
  149.                     
  150.                     /* ...and retrieve the current color setting */
  151.  
  152.                     clrBack = SendMessage (hClrCtrl, CLRM_GETCURCOLOR, 0, 0L);
  153.  
  154.                     /* fall through to end the dialog */
  155.  
  156.                 case IDCANCEL:
  157.  
  158.                     /* end the dialog */
  159.                     
  160.                     EndDialog (hDlg, 0);
  161.  
  162.                     return TRUE;
  163.  
  164.                 case ID_CLRSEL_RESET:
  165.  
  166.                     /* reset the current text color selection */
  167.                     
  168.                     SendMessage (GetDlgItem (hDlg, ID_CLRSEL_TEXT),
  169.                         CLRM_SETCURCOLOR, 0, CLRTEST_TEXT_COLOR);
  170.                     
  171.                     /* reset the current background color selection */
  172.  
  173.                     SendMessage (GetDlgItem (hDlg, ID_CLRSEL_BACKGROUND),
  174.                         CLRM_SETCURCOLOR, 0, CLRTEST_BACKGROUND_COLOR);
  175.  
  176.                     break;
  177.             }
  178.             break;
  179.     }
  180.     
  181.     return FALSE;
  182. }
  183.  
  184. BOOL FAR PASCAL AboutDlgProc
  185.     (HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  186. {
  187.     switch (wMsg)
  188.     {
  189.         case WM_INITDIALOG:
  190.             
  191.             return TRUE;
  192.             
  193.             break;
  194.             
  195.         case WM_COMMAND:
  196.             switch (wParam)
  197.             {
  198.                 case IDOK:
  199.                 case IDCANCEL:
  200.  
  201.                     /* end the dialog */
  202.                     
  203.                     EndDialog (hDlg, 0);
  204.  
  205.                     return TRUE;
  206.  
  207.                     break;
  208.             }
  209.             break;
  210.     }
  211.     
  212.     return FALSE;
  213. }
  214.  
  215. LONG FAR PASCAL WndProc
  216.     (HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  217. {
  218.     static FARPROC  lpfnClrTestDlgProc;
  219.     static FARPROC  lpfnAboutDlgProc;
  220.     static HANDLE   hInstance;
  221.     
  222.     switch (wMsg)
  223.     {
  224.         /* create the application window */
  225.         
  226.         case WM_CREATE:
  227.             /* save the program's instance handle */
  228.             
  229.             hInstance = ((LPCREATESTRUCT) lParam)->hInstance;
  230.             
  231.             /* create instance "thunks" for the dialog box procedures */
  232.         
  233.             lpfnClrTestDlgProc =
  234.                 MakeProcInstance (ClrTestDlgProc, hInstance);
  235.             lpfnAboutDlgProc =
  236.                 MakeProcInstance (AboutDlgProc, hInstance);
  237.  
  238.             return 0;
  239.             
  240.         case WM_COMMAND:
  241.             switch (wParam)
  242.             {
  243.                 case IDM_EXIT:
  244.  
  245.                     /* end the application */
  246.  
  247.                     SendMessage (hWnd, WM_CLOSE, 0, 0L);
  248.  
  249.                     return 0;
  250.  
  251.                     break;
  252.                     
  253.                 case IDM_CLRTEST:
  254.  
  255.                     /* put up the dial test dialog box */
  256.  
  257.                     DialogBox (hInstance, "ClrTest",
  258.                         hWnd, lpfnClrTestDlgProc);
  259.  
  260.                     /* repaint the window, to get the new selected colors */
  261.                     
  262.                     InvalidateRect (hWnd, NULL, TRUE);
  263.  
  264.                     return 0;
  265.                     
  266.                     break;
  267.  
  268.                 case IDM_INDEXHELP:
  269.                     
  270.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_INDEX, 0L);
  271.  
  272.                     break;
  273.                 
  274.                 case IDM_COMMANDHELP:
  275.  
  276.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_CONTEXT, 1L);
  277.                     
  278.                     break;
  279.  
  280.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_INDEX, 0L);
  281.  
  282.                     break;
  283.                 
  284.                 case IDM_USINGHELP:
  285.  
  286.                     WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_HELPONHELP, 0L);
  287.  
  288.                     break;
  289.                 
  290.                 case IDM_ABOUT:
  291.  
  292.                     /* put up the dial test about dialog box */
  293.  
  294.                     DialogBox (hInstance, "About",
  295.                         hWnd, lpfnAboutDlgProc);
  296.  
  297.                     return 0;
  298.                     
  299.                     break;
  300.             }
  301.  
  302.             break;
  303.  
  304.         case WM_PAINT:
  305.         {
  306.             PAINTSTRUCT ps;
  307.             HBRUSH      hBrush;
  308.             HPEN        hPen;
  309.             RECT        rect;
  310.             HDC         hDC;
  311.  
  312.             /* start painting, and get a device context */
  313.             
  314.             hDC = BeginPaint (hWnd, &ps);
  315.  
  316.             /* determine the size of the application window to paint on */
  317.             
  318.             GetClientRect (hWnd, &rect);
  319.  
  320.             /* set the appropriate text and text background colors */
  321.             
  322.             SetTextColor (hDC, clrText);
  323.             SetBkColor (hDC, clrBack);
  324.             SetBkMode (hDC, TRANSPARENT);
  325.             
  326.             /* create solid colored brush and pen to paint the background */
  327.             
  328.             hBrush = CreateSolidBrush (clrBack);
  329.             hPen = CreatePen (PS_INSIDEFRAME, 2, clrBack);
  330.  
  331.             /* select the brush and pen into the DC, saving the old values */
  332.  
  333.             hBrush = SelectObject (hDC, hBrush);
  334.             hPen = SelectObject (hDC, hPen);
  335.             
  336.             /* paint the window with the background color */
  337.             
  338.             Rectangle (hDC, rect.left, rect.top, rect.right, rect.bottom);
  339.  
  340.             /* paint the text, centered in the window, in the correct color */
  341.             
  342.             DrawText (hDC, (LPSTR) "Change colors using Options!", -1, &rect,
  343.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  344.  
  345.             /* restore the old brush and pen */
  346.             
  347.             hBrush = SelectObject (hDC, hBrush);
  348.             hPen = SelectObject (hDC, hPen);
  349.             
  350.             /* delete the created brush */
  351.             
  352.             DeleteObject (hBrush);
  353.             DeleteObject (hPen);
  354.             
  355.             /* end the painting */
  356.             
  357.             EndPaint (hWnd, &ps);
  358.  
  359.             return 0;
  360.  
  361.             break;
  362.         }
  363.  
  364.         case WM_DESTROY :
  365.             
  366.             /* close the help utility */
  367.             
  368.             WinHelp (hWnd, CLRCTRL_HLPNAME, HELP_QUIT, 0L);
  369.  
  370.             /* end the program */
  371.  
  372.             PostQuitMessage (0);
  373.  
  374.             return 0;
  375.     }
  376.     
  377.     /* hand all unprocessed messages to the default window-proc in Windows */
  378.     
  379.     return DefWindowProc (hWnd, wMsg, wParam, lParam);
  380. }
  381.  
  382.