home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CENVIW9.ZIP / DLGCTRL.LIB < prev    next >
Text File  |  1994-03-08  |  9KB  |  241 lines

  1. // DlgCtrl.lib - DIALOG CONTROL LIBRARY
  2. // ver.1
  3. //
  4. // These are useful utilities for working with the values and settings
  5. // of various windows within a window.  These do not necessarily have
  6. // to be dialog boxes, but this will frequently be the case.
  7. //
  8. //
  9. //**** GetSubWindowHandle(): get window within window
  10. // SYNTAX: int GetSubWindowHandle(WindowSpec,int ControlID | string SubWindowText
  11. //                                [,int SiblingIndex | string ClassName])
  12. //         int GetSubWindowHandle(WindowSpec,SubWindowSpec[,int SiblingIndex])
  13. // WHERE: WindowSpec: is a window handle or window title, as in GetWindowHandle() of
  14. //                    WinTools.lib. If 0 or NULL then use current active window.
  15. //        ControlID: Numeric identifier of child window; if 0 then first child
  16. //        SubWindowText: Text associated with child window.  Many sub windows have
  17. //                       '&' in text for hotkeys, and so & will be ignored in match, so
  18. //                       so you can specifiy "help" to match "&Help"
  19. //        SiblingIndex: If specified then will return handle of sibling window this
  20. //                      index from ControlID or SubWindowText. e.g., +1 would get next
  21. //                      sibling and -1 would get previous sibling; 0 = no change
  22. //        ClassName: If specified then will return handle of the next sibling
  23. //                   that is of class ClassName
  24. // RETURN: Returns handle of window found; 0 if not found
  25. // EXAMPLES: The following methods could be use in the FONT dialog box of
  26. //           Paintbrush to return the handle of the "Font Style" combo box
  27. //              GetSubWindowHandle("Font","Font Style",1);
  28. //              GetSubWindowHandle(0,"Font Style",1); // active window
  29. //              GetSubWindowHandle("Font","Font Style","ComboBox");
  30. //              GetSubWindowHandle("Font",0x0471);
  31. //
  32. //
  33. //**** GetWindowText(): Get text association with window
  34. // SYNTAX: string GetWindowText(int WindowHandle)
  35. // WHERE: WindowHandle: any window handle, such as returned from GetSubWindowHandle()
  36. // RETURN: string for text, else NULL if error
  37. // NOTE: Use this function to get edit text, listbox text, or combobox text
  38. //
  39. //
  40. //**** SetWindowText(): Set text association with window
  41. // SYNTAX: string SetWindowText(int WindowHandle,string Text)
  42. // WHERE: WindowHandle: any window handle, such as returned from GetSubWindowHandle()
  43. //        Text: string value to set in edit field, combox, etc...
  44. // RETURN: False if error, else TRUE
  45. // NOTE: Use this function to set edit text, listbox text, or combobox text
  46. //
  47. //
  48. //**** ButtonClick(): click a button window
  49. // SYNTAX: int ButtonClick(int ButtonWindowHandle)
  50. // WHERE: ButtonWindowHandle: handle for button window as from GetSubWindowHandle()
  51. // RETURN: Returns result of sending BN_CLICKED message
  52. //
  53. //
  54. //**** ButtonGetCheck(): Is button checked?
  55. // SYNTAX: bool ButtonGetCheck(int ButtonWindowHandle)
  56. // WHERE: ButtonWindowHandle: handle for button window as from GetSubWindowHandle()
  57. // RETURN: Return boolean true if button is checked, else false
  58. //
  59. //
  60. //**** ButtonSetCheck(): Set button check state on
  61. // SYNTAX: void ButtonSetCheck(int ButtonWindowHandle,int State)
  62. // WHERE: ButtonWindowHandle: handle for button window as from GetSubWindowHandle()
  63. //        State: For regular check buttons, this is TRUE or FALSE for checked or not,
  64. //               but three-state buttons would be one of the following
  65.             #define BTN_NO_CHECK   0
  66.             #define BTN_CHECK      1
  67.             #define BTN_GRAYED     2   // for three-state buttons
  68. //
  69. //
  70. //**** ComboBoxList(): Return all of the possible selections in a combobox list
  71. // SYNTAX: string[] ComboBoxList(int ComboBoxHandle[,ItemCount])
  72. // WHERE: ComboBoxHandle: handle for ComboBox as from GetSubWindowHandle()
  73. //        ItemCount: Return number of item returned in list
  74. // RETURN: NULL if not items, else return
  75. // MODIFY: Set ItemCount (if supplied) to number of item in returned list
  76. //
  77. //
  78. //**** ComboBoxSelectString(): Select first match for string
  79. // SYNTAX: int ComboBoxSelectString(int ComboBoxHandle,string Text)
  80. // WHERE: ComboBoxHandle: handle for ComboBox as from GetSubWindowHandle()
  81. //        Text: string value to set in edit field, combox, etc...
  82. // RETURN: Return index of new selected item, else CB_ERR if no match
  83. //
  84. //
  85. //**** EditGetLineCount(): Get line of text in a multi-line edit control
  86. // SYNTAX: int EditGetLineCount(int EditHandle)
  87. // WHERE: EditHandle: Handle for multi-line edit box
  88. // RETURN:  number of lines in edit control
  89. //
  90. //**** EditGetLine(): Get text from a line of a multi-line edit control
  91. // SYNTAX: string EditGetLine(int EditHandle,int LineNumber)
  92. // WHERE: EditHandle: Handle for multi-line edit box
  93. //        LineNumber: line to read, indexed starting at 0
  94. // RETURN: string found, not NULL
  95. //
  96.  
  97.  
  98. #include <Window.lib>
  99. #include <WinTools.lib>
  100. #include <Message.lib>
  101.  
  102. GetSubWindowHandle(pWinSpec,pIdOrText,pIndexOrClass)
  103. {
  104.    // get handle of parent window
  105.    if ( !(lParentHwnd = pWinSpec ? GetWindowHandle(pWinSpec) : GetActiveWindow()) )
  106.       lRetHwnd = 0;
  107.    else {
  108.       // find window matching ID or Text
  109.       if ( !pIdOrText ) {
  110.          // no id or text, so return first child
  111.          lRetHwnd = GetWindow(lParentHwnd,GW_CHILD);
  112.       } else if ( 0 == DataDimension(pIdOrText) ) {
  113.          // get child window with this ID
  114.          lRetHwnd = GetDlgItem(lParentHwnd,pIdOrText);
  115.       } else {
  116.          lCompareLen = strlen(pIdOrText);
  117.          // window is title, and so must look through all children to match
  118.          for ( lRetHwnd = GetWindow(lParentHwnd,GW_CHILD); lRetHwnd;
  119.                lRetHwnd = GetWindow(lRetHwnd,GW_HWNDNEXT) ) {
  120.             if ( (lWindowText = GetWindowTitle(lRetHwnd)) && lWindowText[0] ) {
  121.                // remove all '&' characters
  122.                lFindAmper = lWindowText;
  123.                while ( lFindAmper = strchr(lFindAmper,'&') )
  124.                   strcpy(lFindAmper,lFindAmper+1);
  125.                // compare new found string against our source
  126.                if ( !strnicmp(lWindowText,pIdOrText,lCompareLen) )
  127.                   break;
  128.             }
  129.          }
  130.       }
  131.    }
  132.  
  133.    if ( lRetHwnd  &&  2 < va_arg() ) {
  134.       if ( 0 == DataDimension(pIndexOrClass) ) {
  135.          // move this sibling direction
  136.          if ( (lIndex = pIndexOrClass) < 0 ) {
  137.             for ( ; lIndex && lRetHwnd; lIndex++ )
  138.                lRetHwnd = GetWindow(lRetHwnd,GW_HWNDPREV);
  139.          } else {
  140.             for ( ; lIndex && lRetHwnd; lIndex-- )
  141.                lRetHwnd = GetWindow(lRetHwnd,GW_HWNDNEXT);
  142.          }
  143.       } else {
  144.          // find first next window that is of class pIndexOrClass
  145.          while ( stricmp(pIndexOrClass,GetClassName(lRetHwnd))
  146.               && (lRetHwnd = GetWindow(lRetHwnd,GW_HWNDNEXT)) ) ;
  147.       }
  148.    }
  149.  
  150.    return lRetHwnd;
  151. }
  152.  
  153. GetWindowText(pHwnd)
  154. {
  155.    lText[0] = lText[500] = '\0';
  156.    if ( (lTextLen = SendMessage(pHwnd,WM_GETTEXT,499,lText)) < 0 )
  157.       return NULL;
  158.    lText[lTextLen] = 0;
  159.    return lText;
  160. }
  161.  
  162. SetWindowText(pHwnd,pText)
  163. {
  164.    return ( 0 <= SendMessage(pHwnd,WM_SETTEXT,0,pText) );
  165. }
  166.  
  167. ButtonClick(pHwnd)
  168. {
  169.    lParentHwnd = GetParent(pHwnd);
  170.    lCtrlID = GetDlgCtrlID(pHwnd);
  171.    return SendMessage(lParentHwnd,WM_COMMAND,lCtrlID,pHwnd,BN_CLICKED);
  172. }
  173.  
  174. ButtonGetCheck(pHwnd)
  175. {
  176.    return SendMessage(pHwnd,BM_GETCHECK,0,0);
  177. }
  178.  
  179. ButtonSetCheck(pHwnd,pState)
  180. {
  181.    SendMessage(pHwnd,BM_SETCHECK,pState,0);
  182. }
  183.  
  184. ComboBoxList(pHwnd,pItemCount)
  185. {
  186.    if ( lCount = SendMessage(pHwnd,CB_GETCOUNT,0,0) ) {
  187.       for ( lIdx = 0; lIdx < lCount; lIdx++ ) {
  188.          lText[0] = lText[500] = '\0';
  189.          lText[SendMessage(pHwnd,CB_GETLBTEXT,lIdx,lText)] = 0;
  190.          strcpy(lList[lIdx],lText);
  191.       }
  192.    }
  193.    if ( 1 < va_arg() ) pItemCount = lCount;
  194.    return lCount ? lList : NULL;
  195. }
  196.  
  197. ComboBoxSelectString(pHwnd,pText)
  198. {
  199.    return SendMessage(pHwnd,CB_SELECTSTRING,-1,pText);
  200. }
  201.  
  202. EditGetLineCount(pHwnd)
  203. {
  204.    return SendMessage(pHwnd,EM_GETLINECOUNT,0,0);
  205. }
  206.  
  207. EditGetLine(pHwnd,pLine)
  208. {
  209.    BLObSize(lTextBLOb,400);
  210.    BLObPut(lTextBLOb,0,380,UWORD16);
  211.    if ( lTextLen = SendMessage(pHwnd,EM_GETLINE,pLine,lTextBLOb) )
  212.       lText = BLObGet(lTextBLOb,0,lTextLen);
  213.    lText[lTextLen] = \'0';
  214.    return lText;
  215. }
  216.  
  217. /************ Useful functions ********/
  218.  
  219. GetDlgItem(pHwnd,pCtrlID)
  220. {
  221.    return DynamicLink("USER","GETDLGITEM",UWORD16,PASCAL,pHwnd,pCtrlID);
  222. }
  223.  
  224. GetDlgCtrlID(pHwnd)
  225. {
  226.    return DynamicLink("USER","GETDLGCTRLID",UWORD16,PASCAL,pHwnd);
  227. }
  228.  
  229. GetClassName(pHwnd)
  230. {
  231.    lClassName[400] = '\0';
  232.    lClassName[DynamicLink("USER","GETCLASSNAME",SWORD16,PASCAL,pHwnd,lClassName,399)] = 0;
  233.    return lClassName;
  234. }
  235.  
  236. GetParent(pHwnd)
  237. {
  238.    return DynamicLink("USER","GETPARENT",UWORD16,PASCAL,pHwnd);
  239. }
  240.  
  241.