home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / ALCKEY.ZIP / ALCKEY.C < prev    next >
C/C++ Source or Header  |  1993-03-08  |  20KB  |  616 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM:  alckey
  4.  
  5.     PURPOSE:  Microsoft Sample Application that demonstrates ALC_ flags
  6.               and the ShowKeyboard API for Windows for Pens.
  7.               
  8.               Author:  Cynthia G. Anderson, 
  9.                        PSS Developer Support, Windows SDK Developer Support
  10.               Created: 9/15/92
  11.  
  12.     FUNCTIONS:
  13.  
  14.          WinMain() - calls initialization function, processes message loop
  15.          MainWndProc() - processes messages
  16.          About() - processes messages for "About" dialog box
  17.          InitApplication() - initializes window data and registers window
  18.          InitInstance() - saves instance handle and creates main window
  19.          AlcDlgProc() - Dialog procedure for setting ALC options
  20.          SetAlcBits() - sets bits if ALC_BITMAP selected in ALC Dialog
  21.          
  22. ****************************************************************************/
  23.  
  24. // COPYRIGHT:
  25. //
  26. //   (C) Copyright Microsoft Corp. 1993.  All rights reserved.
  27. //
  28. //   You have a royalty-free right to use, modify, reproduce and
  29. //   distribute the Sample Files (and/or any modified version) in
  30. //   any way you find useful, provided that you agree that
  31. //   Microsoft has no warranty obligations or liability for any
  32. //   Sample Application Files which are modified.
  33. //
  34.  
  35. #include "windows.h"
  36. #include "penwin.h"
  37. #include "string.h"
  38. #include "math.h"
  39. #include "stdlib.h"
  40. #include "alckey.h"
  41.  
  42.  
  43. int _pascal WinMain( HANDLE, HANDLE, LPSTR, int );
  44. long _far _pascal MainWndProc( HWND, unsigned, WORD, LONG );
  45. BOOL _far _pascal About( HWND, unsigned, WORD, LONG );
  46. BOOL _far _pascal AlcDlgProc( HWND, unsigned, WORD, LONG );
  47. static BOOL InitApplication( HANDLE );
  48. static BOOL InitInstance( HANDLE, int );
  49. VOID SetAlcBits (LPRC lprc, LPSTR lp);
  50.  
  51.  
  52. static HANDLE hInst;
  53. static HWND   hwnd;            /* handle to main window */
  54. static HWND   hWndHedit, hWndBedit;  /* handle to child windows..hedit and bedit */
  55. static HWND   hWndKeyBdButton;  /* handle to pen windows keyboard button*/
  56. static HWND   hWndAlcButton;  /* handle to alc settings button */
  57. static HWND   heditFocusWnd = NULL;  //keeper of the last window to have focus
  58. static HWND   hPenWin;               /* handle to penwin.dll if present */
  59. static HBITMAP  hKBUp, hKBDown;   /* showkeyboard bitmaps handles */
  60.  
  61.  
  62. /****************************************************************************
  63.  
  64.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  65.  
  66.     PURPOSE: calls initialization function, processes message loop
  67.  
  68. ****************************************************************************/
  69.  
  70. int _pascal WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  71.                     LPSTR lpCmdLine,  int nCmdShow)
  72.     {
  73.     MSG msg;
  74.  
  75.     if (hPrevInstance == 0)
  76.         if (InitApplication(hInstance) == 0)
  77.             return (FALSE);
  78.  
  79.     if (InitInstance(hInstance, nCmdShow) == 0)
  80.         return (FALSE);
  81.  
  82.     while (GetMessage(&msg, 0, 0, 0) != 0)
  83.         {
  84.         TranslateMessage(&msg);
  85.         DispatchMessage(&msg);
  86.         }
  87.  
  88.     return (msg.wParam);
  89.     }
  90.  
  91.  
  92. /****************************************************************************
  93.  
  94.     FUNCTION: InitApplication(HANDLE)
  95.  
  96.     PURPOSE: Initializes window data and registers window class
  97.  
  98. ****************************************************************************/
  99.  
  100. static BOOL InitApplication(HANDLE hInstance)
  101.     {
  102.     WNDCLASS  wc;
  103.     char      szMenu[11], szClass[12];
  104.  
  105.     LoadString (hInstance, ID_MENUSTR, szMenu, sizeof (szMenu));
  106.     LoadString (hInstance, ID_CLASSSTR, szClass, sizeof (szClass));
  107.  
  108.     wc.style          = 0;
  109.     wc.lpfnWndProc    = (WNDPROC)MainWndProc;
  110.     wc.cbClsExtra     = 0;
  111.     wc.cbWndExtra     = 0;
  112.     wc.hInstance      = hInstance;
  113.     wc.hIcon          = LoadIcon(hInstance, "ALCKEY");
  114.     wc.hCursor        = LoadCursor(0, IDC_ARROW);
  115.     wc.hbrBackground  = (HBRUSH)COLOR_WINDOW+1 ;
  116.     wc.lpszMenuName   = szMenu;
  117.     wc.lpszClassName  = szClass;
  118.  
  119.     return (RegisterClass(&wc));
  120.     }
  121.  
  122.  
  123. /****************************************************************************
  124.  
  125.     FUNCTION:  InitInstance(HANDLE, int)
  126.  
  127.     PURPOSE:  Saves instance handle and creates main window
  128.  
  129. ****************************************************************************/
  130.  
  131. static BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  132.     {
  133.     char szClass[12], szTitle[40];
  134.  
  135.     LoadString (hInstance, ID_CLASSSTR, szClass, sizeof (szClass));
  136.     LoadString (hInstance, ID_CAPTIONSTR, szTitle, sizeof (szTitle));
  137.  
  138.     hInst = hInstance;
  139.  
  140.     hwnd = CreateWindow(
  141.              szClass,
  142.              szTitle,
  143.              WS_OVERLAPPEDWINDOW,
  144.              CW_USEDEFAULT,
  145.              CW_USEDEFAULT,
  146.              CW_USEDEFAULT,
  147.              CW_USEDEFAULT,
  148.              0,
  149.              0,
  150.              hInstance,
  151.              0 );
  152.  
  153.     if (hwnd == 0 )
  154.         return ( FALSE );
  155.  
  156.     ShowWindow(hwnd, nCmdShow);
  157.     UpdateWindow(hwnd);
  158.     return (TRUE);
  159.     }
  160.  
  161.  
  162. /****************************************************************************
  163.  
  164.     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
  165.  
  166.     PURPOSE:  Processes messages
  167.  
  168.           
  169. ****************************************************************************/
  170.  
  171. long _far _pascal MainWndProc(HWND hWnd, unsigned message,
  172.                               WORD wParam, LONG lParam)
  173.     {
  174.  
  175.     FARPROC  lpProcAbout, lpProcAlcDlg;
  176.     char szDlgBox[9], szMsgBoxCap[12], szStatus1[14], szStatus2[14];
  177.     char szAlcDlgBox[10];
  178.  
  179.     LoadString (hInst, ID_DLGBOX, szDlgBox, sizeof (szDlgBox));
  180.     LoadString (hInst, ID_MSGBOXCAP, szMsgBoxCap, sizeof (szMsgBoxCap));
  181.     LoadString (hInst, ID_STATUS1, szStatus1, sizeof (szStatus1));
  182.     LoadString (hInst, ID_STATUS2, szStatus2, sizeof (szStatus2));
  183.     
  184.     LoadString (hInst, ID_ALCDLGBOX, szAlcDlgBox, sizeof(szAlcDlgBox));
  185.     
  186.  
  187.     switch ( message )
  188.         {
  189.         case WM_CREATE:
  190.          {
  191.          RECT crect;
  192.          int x,y,cx,cy;
  193.          
  194.          //create keyboard button top,left corner
  195.          x=y=0;
  196.          cx=cy=40;    //make buttons 40 pixels square
  197.          hWndKeyBdButton = CreateWindow(
  198.                             (LPSTR)"button",
  199.                             (LPSTR)"ShowKeyBoard",
  200.                             WS_CHILD|WS_VISIBLE | WS_BORDER|BS_OWNERDRAW,
  201.                             x,
  202.                             y,
  203.                             cx,
  204.                             cy,
  205.                             hWnd, 
  206.                             IDC_KEYBDBUTTON,
  207.                             GetWindowWord(hWnd, GWW_HINSTANCE),
  208.                             (LPSTR)NULL);
  209.          
  210.          //load up the keyboard bitmaps, discard when app closes.
  211.          hPenWin = GetSystemMetrics(SM_PENWINDOWS);
  212.  
  213.          hKBDown = LoadBitmap(hPenWin, MAKEINTRESOURCE(OBM_SKBBTNDOWN));
  214.          hKBUp = LoadBitmap(hPenWin, MAKEINTRESOURCE(OBM_SKBBTNUP));
  215.          
  216.          //create alc settings button top,left corner next to keyboard button
  217.          x=40; y=0;
  218.          cx=cy=40;
  219.          hWndAlcButton = CreateWindow(
  220.                             (LPSTR)"button",
  221.                             (LPSTR)"ALC",
  222.                             WS_CHILD|WS_VISIBLE | WS_BORDER,
  223.                             x,
  224.                             y,
  225.                             cx,
  226.                             cy,
  227.                             hWnd, 
  228.                             IDC_ALCBUTTON,
  229.                             GetWindowWord(hWnd, GWW_HINSTANCE),
  230.                             (LPSTR)NULL);
  231.                
  232.          // create two edit controls, an hedit and a bedit
  233.          GetClientRect(hWnd, &crect);
  234.          
  235.          x =  (crect.right-crect.left)/8;
  236.          cx = (crect.right-crect.left)*4/8;
  237.          y =  (crect.bottom-crect.top)/8;
  238.          cy = (crect.bottom-crect.top)*2/8;
  239.          
  240.          hWndHedit = CreateWindow(
  241.                (LPSTR)"hedit",
  242.                (LPSTR)NULL,
  243.                WS_CHILD|WS_VISIBLE |ES_MULTILINE|ES_LEFT | ES_AUTOVSCROLL|WS_VSCROLL | WS_BORDER | WS_TABSTOP,
  244.                x,
  245.                y,
  246.                cx,
  247.                cy,
  248.                hWnd, 
  249.                (HMENU)NULL,
  250.                GetWindowWord(hWnd, GWW_HINSTANCE),
  251.                (LPSTR)NULL);
  252.                
  253.          x =  (crect.right-crect.left)/8;
  254.          cx = (crect.right-crect.left)*4/8;
  255.          y =  (crect.bottom-crect.top)*4/8;
  256.          cy = (crect.bottom-crect.top)*2/8;
  257.          
  258.          hWndBedit = CreateWindow(
  259.                (LPSTR)"bedit",
  260.                (LPSTR)NULL,
  261.                WS_CHILD|WS_VISIBLE |ES_MULTILINE|ES_LEFT | ES_AUTOVSCROLL | WS_VSCROLL|WS_BORDER | WS_TABSTOP,
  262.                x,
  263.                y,
  264.                cx,
  265.                cy,
  266.                hWnd, 
  267.                (HMENU)NULL,
  268.                GetWindowWord(hWnd, GWW_HINSTANCE),
  269.                (LPSTR)NULL);
  270.         
  271.          }
  272.          
  273.          //set focus to hedit control as a default
  274.          SetFocus(hWndHedit);
  275.          heditFocusWnd = hWndHedit;
  276.          
  277.          break;
  278.         
  279.         case WM_SETFOCUS:
  280.         SetFocus(heditFocusWnd);
  281.         return 0;
  282.         break;
  283.         
  284.         case WM_COMMAND:
  285.             /* Edit control commands
  286.             */
  287.             if (HIWORD(lParam) == EN_SETFOCUS)
  288.               {
  289.               /* Field focus is being set */
  290.               heditFocusWnd = LOWORD(lParam);
  291.               break;
  292.               }
  293.             
  294.             switch ( wParam )
  295.                 {
  296.                 case IDM_ABOUT:
  297.                     lpProcAbout = MakeProcInstance( About, hInst );
  298.                     DialogBox(hInst, szDlgBox, hWnd, lpProcAbout);
  299.                     FreeProcInstance( lpProcAbout );
  300.                     break;
  301.  
  302.                 case IDC_ALCBUTTON:
  303.                     lpProcAlcDlg = MakeProcInstance(AlcDlgProc, hInst);
  304.                     DialogBox(hInst,szAlcDlgBox, hWnd, lpProcAlcDlg);
  305.                     FreeProcInstance( lpProcAlcDlg);
  306.                     break;
  307.                     
  308.                 case IDC_KEYBDBUTTON:
  309.                     {
  310.                     SKBINFO skb;
  311.                     
  312.                     if (heditFocusWnd)
  313.                       SetFocus(heditFocusWnd);
  314.                     
  315.                     ShowKeyboard(hWnd,SKB_QUERY,NULL, &skb);
  316.                     if (!skb.fVisible)
  317.                      {
  318.                      ShowKeyboard(hWnd,SKB_SHOW,NULL,&skb);
  319.                      }
  320.                     else
  321.                      {
  322.                      ShowKeyboard(hWnd, SKB_HIDE,NULL,&skb);
  323.                      }
  324.  
  325.                      
  326.                     }
  327.                     break;
  328.                     
  329.  
  330.                 }
  331.             break;
  332.         
  333.         case WM_MEASUREITEM:
  334.           {
  335.           int nIDCtl;
  336.           LPMEASUREITEMSTRUCT lpmisCtl;
  337.           
  338.           nIDCtl = (int) wParam;                      /* control identifier   */
  339.           lpmisCtl = (MEASUREITEMSTRUCT FAR*) lParam; /* address of structure */
  340.           }
  341.           break;
  342.         
  343.         case WM_DRAWITEM:
  344.           {
  345.           int nIDCtl;
  346.           LPDRAWITEMSTRUCT lpdis;
  347.           HDC hMemDC;
  348.           HBITMAP hOldBitmap;
  349.           BITMAP bm;
  350.           
  351.           nIDCtl = (int) wParam;                     /* control identifier */
  352.          // lpdis = (const DRAWITEMSTRUCT FAR*) lParam;    /* structure     */
  353.           lpdis = (LPDRAWITEMSTRUCT) lParam; 
  354.           GetObject(hKBUp,sizeof(BITMAP),&bm);
  355.           
  356.           //check for state of button .. the only ownerdraw control in this app.
  357.           switch (lpdis->itemAction) {
  358.  
  359.             case ODA_DRAWENTIRE:
  360.             /* Redraw the entire control or menu. */
  361.             case ODA_SELECT:
  362.             /* Redraw to reflect current selection state. */
  363.             
  364.             hMemDC = CreateCompatibleDC(lpdis->hDC);
  365.             
  366.             if ((lpdis->itemState && ODS_SELECTED) != 0)     //bit is set if not equal to zero
  367.                hOldBitmap = SelectObject(hMemDC,hKBDown);
  368.             else
  369.                hOldBitmap = SelectObject(hMemDC, hKBUp);
  370.                
  371.             StretchBlt(lpdis->hDC,0,0,40,40,hMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
  372.             SelectObject(hMemDC, hOldBitmap);
  373.             DeleteDC(hMemDC);
  374.             
  375.             return TRUE;
  376.  
  377.             case ODA_FOCUS:
  378.             /* Redraw to reflect current focus state. */
  379.             hMemDC = CreateCompatibleDC(lpdis->hDC);
  380.             
  381.             if ((lpdis->itemState && ODS_FOCUS) != 0)     //bit is set if not equal to zero
  382.                hOldBitmap = SelectObject(hMemDC,hKBDown);
  383.             else
  384.                hOldBitmap = SelectObject(hMemDC, hKBUp);
  385.                
  386.             StretchBlt(lpdis->hDC,0,0,40,40,hMemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
  387.             SelectObject(hMemDC, hOldBitmap);
  388.             DeleteDC(hMemDC);
  389.             
  390.             return TRUE;
  391.  
  392.             
  393.            } //end of switch
  394.            
  395.           }
  396.           break;
  397.  
  398.         case WM_DESTROY:
  399.             PostQuitMessage(0);
  400.             break;
  401.  
  402.         default:
  403.             return (DefWindowProc(hWnd, message, wParam, lParam));
  404.      }
  405.  
  406.     return (FALSE);
  407.     }
  408.  
  409.  
  410. /****************************************************************************
  411.  
  412.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  413.  
  414.     PURPOSE:  Processes messages for "About" dialog box
  415.  
  416.     MESSAGES:
  417.  
  418.         WM_INITDIALOG - initialize dialog box
  419.         WM_COMMAND    - Input received
  420.  
  421. ****************************************************************************/
  422.  
  423. BOOL _far _pascal About(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  424.     {
  425.     switch (message)
  426.         {
  427.         case WM_INITDIALOG:
  428.             return (TRUE);
  429.  
  430.         case WM_COMMAND:
  431.             if (wParam == IDOK || wParam == IDCANCEL)
  432.                 {
  433.                 EndDialog(hDlg, TRUE);
  434.                 return (TRUE);
  435.                 }
  436.             break;
  437.         }
  438.     return (FALSE);
  439.     }
  440.  
  441. /****************************************************************************
  442.  
  443.     FUNCTION: AlcDlgProc(HWND, unsigned, WORD, LONG)
  444.  
  445.     PURPOSE:  Processes messages for "Alc Dialog" dialog box
  446.  
  447.     MESSAGES:
  448.  
  449.         WM_INITDIALOG - initialize dialog box
  450.         WM_COMMAND    - Input received
  451.  
  452. ****************************************************************************/
  453.  
  454. BOOL _far _pascal AlcDlgProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam)
  455.     {
  456.     DWORD dwIndex;
  457.     RC rc;
  458.     char szAlcTmp[30];
  459.     LPSTR lpAlcBeg, lpAlcEnd, lpAlc, lpAlcOrg;
  460.     int j;
  461.     LONG  y;
  462.     DWORD dwx;
  463.     HRSRC hAlcValues;   //alc string resource handle in rc file
  464.     HGLOBAL hAlcGlobal;  //global memory handle of alc resource string
  465.     BOOL fContinue = TRUE;
  466.     
  467.     
  468.     switch (message)
  469.         {
  470.         case WM_INITDIALOG:
  471.         {
  472.         //load values into listbox for ALC use
  473.         if (hAlcValues = FindResource(hInst, (LPCSTR)ID_ALCSTRINGS, (LPCSTR)RT_RCDATA))
  474.           if (hAlcGlobal = LoadResource(hInst, hAlcValues))
  475.             lpAlc = (LPSTR)LockResource(hAlcGlobal);
  476.         lpAlcOrg = lpAlc;
  477.         
  478.         lpAlcBeg = lpAlc;
  479.         lpAlcEnd = lpAlc;
  480.         
  481.        while (fContinue)
  482.         {
  483.         lpAlc++;
  484.         //must find a way to check for eof right off..
  485.         if (!(*lpAlc != 0))
  486.           break;
  487.           
  488.           if (*lpAlcEnd != '{') 
  489.            {
  490.             lpAlcEnd++;
  491.            }
  492.           else
  493.           {
  494.             //initialize tmp array back to null
  495.             for (j=0; j<30; j++)
  496.               szAlcTmp[j] = 0;
  497.               
  498.             _fmemcpy((void _far *)szAlcTmp,lpAlcBeg,(int)(lpAlcEnd-lpAlcBeg));
  499.             //is this the terminating string?
  500.             if ((szAlcTmp[0] == 'E')&&(szAlcTmp[1] == 'O')&&(szAlcTmp[2] == 'S'))
  501.               {
  502.               fContinue = FALSE;
  503.               break; //out of the loop
  504.               }
  505.               
  506.             dwIndex = SendDlgItemMessage(hDlg, ID_ALCLISTBOX, LB_ADDSTRING, 0, (LPARAM)((LPSTR)szAlcTmp));
  507.             //pick up alc hex value
  508.             
  509.             for (j=0; j<30; j++)
  510.               szAlcTmp[j] = 0;
  511.             _fmemcpy((void _far *)szAlcTmp,lpAlcEnd+1,10); //format has 10 numerics in it
  512.             dwx = atol(szAlcTmp);
  513.             //associate the value with that listbox item
  514.             SendDlgItemMessage(hDlg,ID_ALCLISTBOX,LB_SETITEMDATA,(WORD)dwIndex,(LPARAM)dwx);
  515.             y = SendDlgItemMessage(hDlg,ID_ALCLISTBOX,LB_GETITEMDATA,(WORD)dwIndex,0L);
  516.             
  517.             //go to start of next valid alc code
  518.             while (*lpAlcEnd != '!')
  519.               lpAlcEnd++;
  520.             lpAlcBeg = ++lpAlcEnd;
  521.           //increment lpAlc to new position
  522.           lpAlc = lpAlcEnd;  
  523.           }
  524.         }
  525.         
  526.         
  527.             return (TRUE);
  528.         }
  529.         
  530.         
  531.         case WM_COMMAND:
  532.            switch (wParam)
  533.             {
  534.               case ID_SELECT:
  535.                 {
  536.                 LPSTR lp;
  537.                 HWND hwndMyEdit;
  538.                 char szBuffer[32] ;
  539.                 int count, retct;
  540.                 int szCount[32];
  541.  
  542.                 LONG alcvalue=0;
  543.                 int i;
  544.  
  545.                 hwndMyEdit = GetDlgItem(hDlg, ID_ALCEDIT);
  546.                 SendMessage(hwndMyEdit, WM_GETTEXT, sizeof(szBuffer),
  547.                            (LPARAM) ((LPSTR) szBuffer));
  548.                 lp = szBuffer;
  549.  
  550.                 //get rc for hedit control and reset to new values for alc
  551.                 if (SendMessage(hWndHedit, WM_HEDITCTL, HE_GETRC, (LONG)((LPRC)&rc)))
  552.                  {
  553.                     rc.alc = alcvalue = ALC_USEBITMAP;
  554.                     //initialize szCount to nothing
  555.                     
  556.                     count = (int)SendDlgItemMessage(hDlg,ID_ALCLISTBOX,LB_GETSELCOUNT,0,0L);
  557.                     for (i=0; i<count; i++)
  558.                       szCount[i] = 0;
  559.                     retct = (int)SendDlgItemMessage(hDlg,ID_ALCLISTBOX,LB_GETSELITEMS,(WPARAM)count,(LPARAM)(int FAR*)szCount);
  560.                     
  561.                      
  562.                     for (i=0; i <count; i++)
  563.                     {
  564.                       alcvalue = SendDlgItemMessage(hDlg,ID_ALCLISTBOX,LB_GETITEMDATA,(WORD)szCount[i],0L);
  565.                       rc.alc = rc.alc|alcvalue;
  566.                     }
  567.                     
  568.                     if (rc.alc&ALC_USEBITMAP)
  569.                        SetAlcBits(&rc, lp);
  570.  
  571.                     SendMessage(hWndHedit, WM_HEDITCTL, HE_SETRC, (LONG)((LPRC)&rc));
  572.                     
  573.                  } //end if
  574.                  
  575.           
  576.                 }
  577.               break;
  578.               
  579.               case ID_DONE:
  580.                 {
  581.                 EndDialog(hDlg, TRUE);
  582.                 return (TRUE);
  583.                 }
  584.               break;
  585.               
  586.               default:
  587.               break;
  588.             }//end switch
  589.             
  590.         }
  591.     return (FALSE);
  592.     }
  593.  
  594.  
  595. /*----------------------------------------------------------
  596. Purpose: Set the bits for ALC_USEBITMAP field
  597. Returns: --
  598. */
  599. VOID SetAlcBits(
  600.    LPRC lprc,
  601.    LPSTR lp)
  602.    {
  603.    int ib;
  604.  
  605. //wipe out all the old values (initialization to '0')
  606.    for (ib=0; ib<32; ib++)
  607.       lprc->rgbfAlc[ib] = 0;   //this is faster than using the macros
  608.  
  609.    for ( ; *lp != 0; lp++)
  610.       {
  611.       SetAlcBitAnsi (lprc, *lp);  //use the ALC macro here
  612.       }
  613.    }
  614.  
  615. /* END OF FILE */
  616.