home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / MSJMAR94.ZIP / DYNDLG.ZIP / DYNDLG1.ZIP / DIALOGS.C < prev    next >
C/C++ Source or Header  |  1994-03-01  |  6KB  |  237 lines

  1. /**************************************************************************
  2.     Dynamic Dialog Boxes Demo
  3.     Dialogs Module
  4.     Written by Atif Aziz
  5. **************************************************************************/
  6.  
  7. #define STRICT
  8.  
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include "winxext.h"
  12. #include "dialogs.h"
  13.  
  14.     // Constants
  15.  
  16. #define SZ_TEXTPROP     TEXT("DynDlg_TextSave")
  17.  
  18.     // Declarations
  19.  
  20. BOOL            g_fDefDlgEx = FALSE;
  21.  
  22.     // Function prototypes
  23.  
  24. static void     GetDlgCtlRect(HWND, int, LPRECT);
  25.  
  26. // -----------------------------------------------------------------------
  27. //  PURPOSE:
  28. //      Common default window procedure for all dialog.
  29. // -----------------------------------------------------------------------
  30.  
  31. LRESULT CommonDlg_DefProc(hwndDlg, uMessage, wParam, lParam)
  32. HWND    hwndDlg;
  33. UINT    uMessage;
  34. WPARAM  wParam;
  35. LPARAM  lParam;
  36. {
  37.   return DefDlgProcEx(hwndDlg, uMessage, wParam, lParam, &g_fDefDlgEx);
  38. }
  39.  
  40. // -----------------------------------------------------------------------
  41. //  PURPOSE:
  42. //      Shows or hides controls whose ID fall within a given range. 
  43. // -----------------------------------------------------------------------
  44.  
  45. void ShowCategory(hwndDlg, nFirstID, nLastID, fShow)
  46. HWND    hwndDlg;
  47. int     nFirstID;
  48. int     nLastID;
  49. BOOL    fShow;
  50. {
  51.   BOOL          fCtlText;
  52.   int           i;
  53.   HWND          hwndCtl;
  54.   int           nLen;
  55.   LPTSTR        lpszText;
  56.   HLOCAL        hlocText;
  57.   UINT          dwCtlCode;
  58.  
  59.   for (i = nFirstID; i <= nLastID; i++)
  60.   {
  61.     hwndCtl = GetDlgItem(hwndDlg, i);
  62.  
  63.     if (hwndCtl)
  64.     {
  65.       // Find out if the control is potential danger for mnemonic
  66.       // conflicts. This is especially to get around a bug in Windows
  67.       // which yields focus to hidden controls when they share
  68.       // mnemonics with visible controls on a dialog box. 
  69.  
  70.       fCtlText = TRUE;
  71.  
  72.       dwCtlCode = (UINT) SendMessage(hwndCtl, WM_GETDLGCODE, 0, 0);
  73.  
  74.       if (dwCtlCode & DLGC_WANTCHARS)
  75.     fCtlText = FALSE;
  76.  
  77.       if ((dwCtlCode & DLGC_STATIC) &&
  78.     (GetWindowStyle(hwndCtl) & SS_NOPREFIX))
  79.     fCtlText = FALSE;
  80.  
  81.       if (fShow)
  82.       {
  83.     // Restore the control's text?
  84.  
  85.     if (fCtlText)
  86.     {
  87.       // Get the text from the property in which it was saved.
  88.  
  89.       hlocText = GetProp(hwndCtl, SZ_TEXTPROP);
  90.  
  91.       if (hlocText)
  92.       {
  93.         // Restore the text and then free the allocation for the
  94.         // text and the property.
  95.  
  96.         lpszText = (LPTSTR) LocalLock(hlocText);
  97.         SetWindowText(hwndCtl, lpszText);
  98.         LocalUnlock(hlocText);
  99.         RemoveProp(hwndCtl, SZ_TEXTPROP);
  100.         LocalFree(hlocText);
  101.       }
  102.     }
  103.  
  104.     // NOTE: We show a control after setting its text to avoid
  105.     // unneccessary flicker due to re-draws.
  106.  
  107.     ShowWindow(hwndCtl, SW_SHOW);
  108.       }
  109.       else
  110.       {
  111.     // NOTE: We hide a control before setting its text to avoid
  112.     // unneccessary flicker due to re-draws.
  113.  
  114.     ShowWindow(hwndCtl, SW_HIDE);
  115.  
  116.     // Save and destroy the control's text?
  117.  
  118.     if (fCtlText)
  119.     {
  120.       // Get the length of the window text, make an equivalent
  121.       // allocation and then assign the handle to a property.
  122.  
  123.       nLen = GetWindowTextLength(hwndCtl) + 1;
  124.       hlocText = LocalAlloc(LHND, nLen * sizeof(TCHAR));
  125.       SetProp(hwndCtl, SZ_TEXTPROP, hlocText);
  126.  
  127.       if (hlocText)
  128.       {
  129.         lpszText = (LPTSTR) LocalLock(hlocText);
  130.         GetWindowText(hwndCtl, lpszText, nLen);
  131.         LocalUnlock(hlocText);
  132.  
  133.         // Destroy the control's text.
  134.  
  135.         SetWindowText(hwndCtl, TEXT(""));
  136.       }
  137.     }
  138.       }
  139.     }
  140.   }
  141. }
  142.  
  143. // -----------------------------------------------------------------------
  144. //  PURPOSE:
  145. //      Moves controls whose ID fall within a given range from one
  146. //      location to another. The displacement in location is measures
  147. //      by using two controls on the dialog box as relative references.
  148. // -----------------------------------------------------------------------
  149.  
  150. void MoveCategory(hwndDlg, nFirstID, nLastID, nSrcID, nDestID)
  151. HWND    hwndDlg;
  152. int     nFirstID;
  153. int     nLastID;
  154. int     nSrcID;
  155. int     nDestID;
  156. {
  157.   int   i;
  158.   RECT  rcSrc;
  159.   RECT  rcDest;
  160.   RECT  rcCtl;
  161.   int   nDeltaX;
  162.   int   nDeltaY;
  163.   HWND  hwndCtl;
  164.  
  165.   // Get the source and destination rectangle coordinates
  166.  
  167.   GetDlgCtlRect(hwndDlg, nSrcID, &rcSrc);
  168.   GetDlgCtlRect(hwndDlg, nDestID, &rcDest);
  169.  
  170.   // Calculate the offset by which to move each control
  171.  
  172.   nDeltaX = rcDest.left - rcSrc.left;
  173.   nDeltaY = rcDest.top - rcSrc.top;
  174.  
  175.   for (i = nFirstID; i <= nLastID; i++)
  176.   {
  177.     hwndCtl = GetDlgItem(hwndDlg, i);
  178.  
  179.     if (hwndCtl)
  180.     {
  181.       GetDlgCtlRect(hwndDlg, i, &rcCtl);
  182.  
  183.       // Displace the control
  184.  
  185.       SetWindowPos(hwndCtl, NULL, rcCtl.left + nDeltaX,
  186.     rcCtl.top + nDeltaY, 0, 0,
  187.     SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  188.     }
  189.   }
  190. }
  191.  
  192. // -----------------------------------------------------------------------
  193. //  PURPOSE:
  194. //      Gets a control's window coordinates relative its parent.
  195. // -----------------------------------------------------------------------
  196.  
  197. static void GetDlgCtlRect(hwndDlg, nID, lprc)
  198. HWND    hwndDlg;
  199. int     nID;
  200. LPRECT  lprc;
  201. {
  202.   GetWindowRect(GetDlgItem(hwndDlg, nID), lprc);
  203.   MapWindowRect(NULL, hwndDlg, lprc);
  204. }
  205.  
  206. // -----------------------------------------------------------------------
  207. //  PURPOSE:
  208. //      Clears the text save property and allocations for the given
  209. //      range of controls. 
  210. // -----------------------------------------------------------------------
  211.  
  212. void RemoveCategoryTextProps(hwndDlg, nFirstID, nLastID)
  213. HWND    hwndDlg;
  214. int     nFirstID;
  215. int     nLastID;
  216. {
  217.   int           i;
  218.   HWND          hwndCtl;
  219.   HLOCAL        hlocText;
  220.  
  221.   for (i = nFirstID; i <= nLastID; i++)
  222.   {
  223.     hwndCtl = GetDlgItem(hwndDlg, i);
  224.  
  225.     if (hwndCtl)
  226.     {
  227.       hlocText = GetProp(hwndCtl, SZ_TEXTPROP);
  228.  
  229.       if (hlocText)
  230.       {
  231.     RemoveProp(hwndCtl, SZ_TEXTPROP);
  232.     LocalFree(hlocText);
  233.       }
  234.     }
  235.   }
  236. }
  237.