home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code1 / iniacces / ini.c < prev    next >
C/C++ Source or Header  |  1993-08-17  |  10KB  |  389 lines

  1. //---------------------------------------------------------------------------
  2. // ini.c
  3. // 
  4. // Written and Copyright 8/93 Walter F. Dexter
  5. // Distribute freely per "ini.wri".
  6. //---------------------------------------------------------------------------
  7. //
  8. // Compiled using BCC 3.1.
  9. //
  10.  
  11. #include <windows.h>
  12. #include <vbapi.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include "ini.h"
  16.  
  17. //---------------------------------------------------------------------------
  18. // Global Variables
  19. //---------------------------------------------------------------------------
  20. #define MAX_READLEN     256
  21.  
  22. BOOL bDevTimeInit = FALSE;
  23. HANDLE hModDLL;
  24. HBITMAP HBitmap;
  25. WORD cVBXusers = 0;
  26. USHORT bmWidth;
  27. USHORT bmHeight;
  28. static char scratch[MAX_READLEN];
  29.  
  30. /***  Function prototypes  ***/
  31. VOID PASCAL PaintControl(HWND hwnd);
  32.  
  33.  
  34. /****************************************************************************
  35.  *
  36.  *  FUNCTION:  IniCtlProc(HCTL, HWND, USHORT, USHORT, LONG)
  37.  *
  38.  *  PURPOSE:   This routine is the subclassed window procedure.  Visual Basic
  39.  *             passes VBM_, VBN_, and WM_ messages to this routine.
  40.  *             The custom control determines which messages to process.
  41.  *             Any messages that are not processed need to passed on to the
  42.  *             default message processing routine VBDefControlProc().
  43.  *
  44.  *  Unlike most of this, which was essentially written by Microsoft, I actually
  45.  *  wrote (or at least put together from multiple examples) most of the code 
  46.  *  in this function.
  47.  *
  48.  *  Walt Dexter, 8/93.
  49.  *
  50.  ******************************************************************************/
  51.  
  52. LONG FAR PASCAL _export IniCtlProc(HCTL hctl, HWND hwnd, USHORT msg,
  53.             USHORT wp, LONG lp)
  54. {
  55.     PINI            pIni;
  56.     //LPSTR            lpStr;
  57.  
  58.     switch (msg) 
  59.     {
  60.         case WM_SIZE:
  61.             /*
  62.              *    Set the sizing border of the control to the exact size
  63.              *    of the bitmap.
  64.              */
  65.             SetWindowPos(hwnd, NULL, 0, 0, bmWidth, bmHeight,
  66.                 SWP_NOMOVE | SWP_NOZORDER);
  67.             break;
  68.  
  69.         case VBM_CREATED:
  70.             /*
  71.              *    The Generic custom control is an invisible control.  If we
  72.              *    are in design mode, display it; if we are not in design mode,
  73.              *    don't allow the control to be displayed.
  74.              */
  75.             if (VBGetMode() == MODE_RUN)
  76.                 return 0L;
  77.     
  78.             break;
  79.  
  80.         case VBM_SETPROPERTY:
  81.             /*
  82.              *    When the Action property is set to a valid value, execute the
  83.              *    the specified action based on other property values.  The
  84.              *    Action property acts as a pseudo-method.
  85.              */
  86.             pIni = (PINI)VBDerefControl(hctl);
  87.         
  88.             if (wp != IPROP_INI_ACTION)
  89.             {
  90.                 break;
  91.             }
  92.  
  93.             pIni->enAction = lp;
  94.  
  95.             switch ((short)lp) 
  96.             {
  97.                 case ACTION_NONE:
  98.                     break;
  99.  
  100.                 case ACTION_WRITE:
  101.                     {
  102.                         if ((pIni->hszSection) &&
  103.                             (pIni->hszEntry) &&
  104.                             (pIni->hszText) &&
  105.                             (pIni->hszFilename))
  106.                         {
  107.                             if (WritePrivateProfileString(
  108.                                     VBLockHsz(pIni->hszSection),
  109.                                     VBLockHsz(pIni->hszEntry),
  110.                                     VBLockHsz(pIni->hszText),
  111.                                     VBLockHsz(pIni->hszFilename)))
  112.                             {
  113.                                 pIni->enAction = 0;
  114.                             }
  115.  
  116.                             VBUnlockHsz(pIni->hszSection);
  117.                             VBUnlockHsz(pIni->hszEntry);
  118.                             VBUnlockHsz(pIni->hszText);
  119.                             VBUnlockHsz(pIni->hszFilename);
  120.  
  121.                         }
  122.                     }
  123.                     break;
  124.  
  125.                 case ACTION_READ:
  126.                     {
  127.                         if ((pIni->hszSection) &&
  128.                             (pIni->hszEntry) &&
  129.                             (pIni->hszDefault) &&
  130.                             (pIni->hszFilename))
  131.                         {
  132.                             if (GetPrivateProfileString(
  133.                                     VBLockHsz(pIni->hszSection),
  134.                                     VBLockHsz(pIni->hszEntry),
  135.                                     VBLockHsz(pIni->hszDefault),
  136.                                     (LPSTR)scratch,
  137.                                     MAX_READLEN,
  138.                                     VBLockHsz(pIni->hszFilename)))
  139.                             {
  140.                                 pIni->enAction = 0;
  141.                             }
  142.  
  143.                             VBUnlockHsz(pIni->hszSection);
  144.                             VBUnlockHsz(pIni->hszEntry);
  145.                             VBUnlockHsz(pIni->hszDefault);
  146.                             VBUnlockHsz(pIni->hszFilename);
  147.  
  148.                             if (pIni->hszText)
  149.                             {
  150.                                 VBDestroyHsz(pIni->hszText);
  151.                                 pIni->hszText = VBCreateHsz(
  152.                                                     FP_SEG((void far *)hctl),
  153.                                                     (LPSTR)scratch);
  154.                             }
  155.                         }
  156.                     }
  157.                     break;
  158.                 case ACTION_DEL_ENTRY:
  159.                     {
  160.                         if ((pIni->hszSection) &&
  161.                             (pIni->hszEntry) &&
  162.                             (pIni->hszFilename))
  163.                         {
  164.                             if (WritePrivateProfileString(
  165.                                     VBLockHsz(pIni->hszSection),
  166.                                     VBLockHsz(pIni->hszEntry),
  167.                                     (LPSTR)0L,
  168.                                     VBLockHsz(pIni->hszFilename)))
  169.                             {
  170.                                 pIni->enAction = 0;
  171.                             }
  172.  
  173.                             VBUnlockHsz(pIni->hszSection);
  174.                             VBUnlockHsz(pIni->hszEntry);
  175.                             VBUnlockHsz(pIni->hszFilename);
  176.  
  177.                         }
  178.                     }
  179.                     break;
  180.                 case ACTION_DEL_SECTION:
  181.                     {
  182.                         if ((pIni->hszSection) &&
  183.                             (pIni->hszFilename))
  184.                         {
  185.                             if (WritePrivateProfileString(
  186.                                     VBLockHsz(pIni->hszSection),
  187.                                     (LPSTR)0L,
  188.                                     (LPSTR)0L,
  189.                                     VBLockHsz(pIni->hszFilename)))
  190.                             {
  191.                                 pIni->enAction = 0;
  192.                             }
  193.  
  194.                             VBUnlockHsz(pIni->hszSection);
  195.                             VBUnlockHsz(pIni->hszEntry);
  196.                             VBUnlockHsz(pIni->hszFilename);
  197.  
  198.                         }
  199.                     }break;
  200.             }
  201.             return 0L;
  202.  
  203.         case WM_SETTEXT:
  204.             {
  205.                 HSZ hsz;
  206.                 PINI pIni = (PINI)VBDerefControl(hctl);
  207.  
  208.                 if (pIni->hszText)
  209.                 {
  210.                     VBDestroyHsz(pIni->hszText);
  211.                 }
  212.                 hsz = VBCreateHsz(FP_SEG((void far *)hctl),(LPSTR)lp);
  213.                 pIni = (PINI)VBDerefControl(hctl);
  214.                 pIni->hszText = hsz;
  215.             }
  216.             return 0L;
  217.  
  218.         case WM_GETTEXT:
  219.             {
  220.                 LPSTR  lpstr;
  221.                 USHORT cch;
  222.                 PINI pIni = (PINI)VBDerefControl(hctl);
  223.  
  224.                 if (pIni->hszText == NULL)
  225.                 {
  226.                     *(LPSTR)lp = 0L;
  227.                     wp = 1;
  228.                 }
  229.                 else
  230.                 {
  231.                     lpstr = VBDerefHsz(pIni->hszText);
  232.                     cch = (USHORT)(lstrlen(lpstr) + 1);
  233.                     if (wp > cch)
  234.                         wp = cch;
  235.                     _fstrncpy((LPSTR)lp, lpstr, wp);
  236.                     ((LPSTR)lp)[wp - 1] = '\0';
  237.                 }
  238.             }
  239.             return (LONG)(wp - 1);
  240.  
  241.         case WM_GETTEXTLENGTH:
  242.             {
  243.                 PINI pIni = (PINI)VBDerefControl(hctl);
  244.  
  245.                 if (pIni->hszText == NULL)
  246.                     return 0L;
  247.                 else
  248.                     return lstrlen(VBDerefHsz(pIni->hszText));
  249.             }
  250.  
  251.         case WM_PAINT:
  252.             /*
  253.              *    The Generic custom control must do it's own painting.
  254.              *    Since the control is never created in run mode, painting
  255.              *    only occurs in design mode.
  256.              */
  257.             PaintControl(hwnd);
  258.             break;
  259.     }
  260.  
  261.     return VBDefControlProc(hctl, hwnd, msg, wp, lp);
  262. }
  263.  
  264.  
  265. /****************************************************************************
  266.  *
  267.  *  FUNCTION:  PaintControl(HWND)
  268.  *
  269.  *  PURPOSE:   This routine handles the painting of the control by creating
  270.  *             a memory image of the bitmap and BitBlt'ing it to the screen.
  271.  *
  272.  *  Pure Microsoft.
  273.  *
  274.  ******************************************************************************/
  275.  
  276. VOID PASCAL PaintControl(HWND hwnd)
  277. {
  278.     HDC        hdcMem;
  279.     PAINTSTRUCT ps;
  280.     
  281.     /*
  282.      *    Set up the display space for the bitmap.
  283.      */
  284.     BeginPaint(hwnd, &ps);
  285.     hdcMem = CreateCompatibleDC(ps.hdc);
  286.     if (!hdcMem)
  287.         return;
  288.  
  289.        SelectObject(hdcMem, HBitmap);
  290.  
  291.     /*
  292.      *    Display the bitmap in the sizing rectangle.
  293.      */
  294.     BitBlt(ps.hdc, 0, 0, bmWidth, bmHeight, hdcMem, 0, 0, SRCCOPY);
  295.  
  296.        DeleteDC(hdcMem);
  297.     EndPaint(hwnd, &ps);
  298. }
  299.  
  300.  
  301. /****************************************************************************
  302.  *
  303.  *  FUNCTION:  LibMain(HANDLE, WORD, WORD, LPSTR)
  304.  *
  305.  *  PURPOSE:   Initialize library. This routine is called when the
  306.  *             first client loads the DLL.
  307.  *
  308.  *  Pure Microsoft.
  309.  *
  310.  ******************************************************************************/
  311.  
  312. int FAR PASCAL LibMain(HANDLE hModule, WORD wDataSeg, WORD cbHeapSize,
  313.                         LPSTR  lpszCmdLine)
  314. {
  315.     // Avoid warnings on unused (but required) formal parameters
  316.     wDataSeg    = wDataSeg;
  317.     cbHeapSize    = cbHeapSize;
  318.     lpszCmdLine = lpszCmdLine;
  319.  
  320.     hModDLL = hModule;
  321.  
  322.     return 1;
  323. }
  324.  
  325.  
  326. /****************************************************************************
  327.  *
  328.  *  FUNCTION:  VBINITCC(USHORT, BOOL)
  329.  *
  330.  *  PURPOSE:   Register custom control.  This routine is called by Visual Basic
  331.  *             or the Visual Basic emulation layer when the custom control DLL
  332.  *             is loaded for use.
  333.  *
  334.  *  Pure Microsoft.
  335.  *
  336.  ******************************************************************************/
  337.  
  338. BOOL FAR PASCAL _export VBINITCC(USHORT usVersion, BOOL fRuntime)
  339. {
  340.     BITMAP bmp;
  341.  
  342.     usVersion = usVersion;
  343.     
  344.     /*
  345.      *    Count the number of hosts using this VBX.  A host can be VB.EXE,
  346.      *    any .EXE compiled from Visual Basic which uses this custom control,
  347.      *    or any other program which loads and uses VBX files.
  348.      */
  349.     ++cVBXusers;
  350.  
  351.     /*
  352.      *    Load the bitmap resource so that it can be used by WM_PAINT.
  353.      */
  354.     if (!fRuntime && !bDevTimeInit) {
  355.         HBitmap = LoadBitmap(hModDLL, MAKEINTRESOURCE(IDBMP_INI_UP));
  356.         if (HBitmap == (HANDLE)NULL)
  357.         {
  358.             return 0L;
  359.         }
  360.  
  361.         GetObject(HBitmap, sizeof(BITMAP), (LPSTR)&bmp);
  362.         bmWidth = (USHORT)bmp.bmWidth;
  363.         bmHeight = (USHORT)bmp.bmHeight;
  364.  
  365.         // We successfully initialized the stuff we need at dev time
  366.         bDevTimeInit = TRUE;
  367.     }
  368.  
  369.     // Register control(s)
  370.     return VBRegisterModel(hModDLL, &modelIni);
  371. }
  372.  
  373. //---------------------------------------------------------------------------
  374. // Unregister custom control.  This routine is called by Visual Basic when
  375. // the custom control DLL (VBX) is being unloaded.
  376. //
  377. // Pure Microsoft.
  378. // 
  379. //---------------------------------------------------------------------------
  380. VOID FAR PASCAL _export VBTERMCC(VOID)
  381. {
  382.     --cVBXusers;
  383.  
  384.     if (cVBXusers == 0 && bDevTimeInit) {
  385.         // Free any resources created for Dev environment
  386.         DeleteObject(HBitmap);
  387.     }
  388. }
  389.