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

  1. // WinTools.lib - Functions for setting the state of of Windows.  This
  2. // ver.1          file may be #included into other Cmm source files, or
  3. //                you can cut and paste in the parts that you need.
  4. //
  5. //                Note: Most of these routines require either a window
  6. //                handle or a window title.  You can use either, but
  7. //                using a Window handle is a little quicker.
  8. //
  9. //
  10. //***** GetWindowHandle(): Get the handle for this window
  11. // SYNTAX: int GetWindowHandle(int WindowHandle)
  12. //         int GetWindowHandle(string WindowTitle)
  13. // WHERE: WindowHandle: Integer identifier for this window
  14. //        WindowTitle: Partial text title of the window (case-insensitive)
  15. // RETURN: Returns Window handle for a Windows whose partial title is
  16. //         WindowTitle. Comparison is case-insensitive.  If WindowTitle
  17. //         is not found then returns 0 (NULL).
  18. // NOTE: If WindowHandle is input, then this returns WindowHandle if
  19. //       WindowHandle is valid, else returns 0.  WindowTitle is a partial
  20. //       and case-insensitive match and so "write" would match the
  21. //       window titled "Write - THESIS.WRI".
  22. //
  23. //
  24. //***** GetWindowTitle(): Get full Window Title
  25. // SYNTAX: string GetWindowTitle(int WindowHandle)
  26. //         string GetWindowTitle(string WindowTitle)
  27. // WHERE: WindowHandle: Integer identifier for this window
  28. //        WindowTitle: Partial text title of the window (case-insensitive)
  29. // RETURN: Returns Full title for this window, or NULL if window not
  30. //         found or title not found
  31. //
  32. //
  33. //***** SetWindowTitle(): Set title for window
  34. // SYNTAX: bool SetWindowTitle(int WindowHandle,string NewTitle)
  35. //         bool SetWindowTitle(string WindowTitle,string NewTitle)
  36. // WHERE: WindowHandle: Integer identifier for this window
  37. //        WindowTitle: Partial text title of the window (case-insensitive)
  38. //        NewTitle: New full title for this window
  39. // RETURN: TRUE if successful, else FALSE if WindowSpec is invalid.
  40. //
  41. //
  42. //***** IsWindow(): Is this a valid window title or handle
  43. // SYNTAX: bool IsWindow(int WindowHandle)
  44. //         bool IsWindow(string WindowTitle)
  45. // WHERE: WindowHandle: Integer identifier for this window
  46. //        WindowTitle: Partial text title of the window (case-insensitive)
  47. // RETURN: Returns non-zero if this window exists, else returns zero (FALSE)
  48. //
  49. //
  50. //***** IsMinimized(): Is window minimized (Iconic)
  51. // SYNTAX: bool IsMinimized(int WindowHandle)
  52. //         bool IsMinimized(string WindowTitle)
  53. // WHERE: WindowHandle: Integer identifier for this window
  54. //        WindowTitle: Partial text title of the window (case-insensitive)
  55. // RETURN: non-zero if Window is minimized (iconic) else zero (FALSE)
  56. //         if not minimized or if WindowSpec is invalid
  57. //
  58. //
  59. //***** IsMaximized(): Is window maximized
  60. // SYNTAX: bool IsMinimized(int WindowHandle)
  61. //         bool IsMinimized(string WindowTitle)
  62. // WHERE: WindowHandle: Integer identifier for this window
  63. //        WindowTitle: Partial text title of the window (case-insensitive)
  64. // RETURN: non-zero if Window is maximized else zero (FALSE) if not
  65. //         maximized or if WindowSpec is invalid
  66. //
  67. //
  68. //***** IsVisible(): Is window visible
  69. // SYNTAX: bool IsVisible(int WindowHandle)
  70. //         bool IsVisible(string WindowTitle)
  71. // WHERE: WindowHandle: Integer identifier for this window
  72. //        WindowTitle: Partial text title of the window (case-insensitive)
  73. // RETURN: non-zero if Window is visible else zero (FALSE) if not
  74. //         visible or if WindowSpec is invalid. Visible means that
  75. //         the window exists on the screen even if it is covered by
  76. //         other windows
  77. //
  78. //
  79. //***** IsEnabled(): Is window enabled for input
  80. // SYNTAX: bool IsEnabled(int WindowHandle)
  81. //         bool IsEnabled(string WindowTitle)
  82. // WHERE: WindowHandle: Integer identifier for this window
  83. //        WindowTitle: Partial text title of the window (case-insensitive)
  84. // RETURN: non-zero if Window is enabled for mouse and keyboard input,
  85. //         else zero (FALSE) if not enabled or if WindowSpec is invalid
  86. //
  87. //
  88. //***** GetWindowRect(): Get current window coordinates
  89. // SYNTAX: bool GetWindowRect(int WindowHandle | string WindowTitle,struct Rectangle)
  90. //         bool GetWindowRect(int WindowHandle | string WindowTitle,Left,Top,Right,Bottom)
  91. // WHERE: WindowHandle: Integer identifier for this window
  92. //        WindowTitle: Partial text title of the window (case-insensitive)
  93. //        Rectangle: receive structure of current window with following members
  94. //             .left - coordinate for left edge of window
  95. //             .right - coordinate for right edge of window
  96. //             .top - coordinate for top edge of window
  97. //             .bottom - coordinate for bottom edge of window
  98. //        Left, Top, Right, Bottom - describe four corners of rectangle
  99. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  100. // MODIFY: Modifies Rectangle structure to current coordinates, or if left, right, top, botom
  101. //         then modify these four variables
  102. //
  103. //
  104. //***** SetWindowRect(): Set current window coordinates
  105. // SYNTAX: bool SetWindowRect(int WindowHandle | string WindowTitle,struct Rectangle)
  106. //         bool SetWindowRect(int WindowHandle | string WindowTitle,Left,Top,Right,Bottom)
  107. // WHERE: WindowHandle: Integer identifier for this window
  108. //        WindowTitle: Partial text title of the window (case-insensitive)
  109. //        Rectangle: new structurw of window with the same member elements as
  110. //                   defined above in GetWindowRect()
  111. //        Left, Top, Right, Bottom - describe four corners of rectangle
  112. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  113. //
  114. //
  115. //***** GetClientRect(): Get current window coordinates of client area
  116. // SYNTAX: bool GetClientRect(int WindowHandle,struct Rectangle)
  117. //         bool GetClientRect(string WindowTitle,struct Rectangle)
  118. // WHERE: WindowHandle: Integer identifier for this window
  119. //        WindowTitle: Partial text title of the window (case-insensitive)
  120. //        Rectangle: receive structure of current client area with following members
  121. //             .left - coordinate for left edge of window
  122. //             .right - coordinate for right edge of window
  123. //             .top - coordinate for top edge of window
  124. //             .bottom - coordinate for bottom edge of window
  125. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  126. // MODIFY: Modifies Rectangle structure to current coordinates
  127. //
  128. //
  129. //***** GetScreenSize(): Get width and height of the windows screen
  130. // SYNTAX: void GetScreenSize(int Width,int Height);
  131. // WHERE: Width: receive screen width
  132. //        Height: receive screen height
  133. // MODIFY: Sets Width and Height to dimensions of screen
  134. //
  135. //
  136. //***** GetSize(): Get current width and height of window
  137. // SYNTAX: bool GetSize(int WindowHandle,int Width,int Height)
  138. //         bool GetSize(string WindowTitle,int Width,int Height)
  139. // WHERE: WindowHandle: Integer identifier for this window
  140. //        WindowTitle: Partial text title of the window (case-insensitive)
  141. //        Width: receive current window width
  142. //        Height: receive current window height
  143. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  144. // MODIFY: Sets Width and Height to current dimensions of window
  145. //
  146. //
  147. //***** SetSize(): Get current width and height of window
  148. // SYNTAX: bool SetSize(int WindowHandle,int Width,int Height)
  149. //         bool SetSize(string WindowTitle,int Width,int Height)
  150. // WHERE: WindowHandle: Integer identifier for this window
  151. //        WindowTitle: Partial text title of the window (case-insensitive)
  152. //        Width: new window width
  153. //        Height: new window height
  154. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  155. //
  156. //
  157. //***** GetPosition(): Get current column and row of window
  158. // SYNTAX: bool GetPosition(int WindowHandle,int LeftCol,int TopRow)
  159. //         bool GetPosition(string WindowTitle,int LeftCol,int TopRow)
  160. // WHERE: WindowHandle: Integer identifier for this window
  161. //        WindowTitle: Partial text title of the window (case-insensitive)
  162. //        LeftCol: receive current column coordinate of upper-left corner of window
  163. //        Height: receive current row coordinate of upper-left corner of window
  164. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  165. // MODIFY: Sets LeftCol and TopRow to current upper-left corner coordinates
  166. //
  167. //
  168. //***** SetPosition(): Get current column and row of window
  169. // SYNTAX: bool SetPosition(int WindowHandle,int LeftCol,int TopRow)
  170. //         bool SetPosition(string WindowTitle,int LeftCol,int TopRow)
  171. // WHERE: WindowHandle: Integer identifier for this window
  172. //        WindowTitle: Partial text title of the window (case-insensitive)
  173. //        LeftCol: new column coordinate of upper-left corner of window
  174. //        Height: new row coordinate of upper-left corner of window
  175. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  176. //
  177. //
  178. //***** ShowWindow(): Set the way the window is shown (or not shown)
  179. // SYNTAX: bool ShowWindow(int WindowHandle,int ShowCommand)
  180. //         bool ShowWindow(string WindowTitle,int ShowCommand)
  181. // WHERE: WindowHandle: Integer identifier for this window
  182. //        WindowTitle: Partial text title of the window (case-insensitive)
  183. //        ShowCommand: Any of the following values:
  184.       #define SW_HIDE            0  // Hide window; make NOT the active window
  185.       #define SW_MINIMIZE        6  // minimize window; make NOT the active window
  186.       #define SW_RESTORE         9  // activate and display this window; restore from minimized
  187.                                     // or maximized to original size and position before
  188.                                     // it was minimized or maximized
  189.       #define SW_RESTORENOACTIVE -9 // Same as restore but active window remains active
  190.       #define SW_SHOW            5  // Activate this window in current size and position
  191.       #define SW_SHOWMAXIMIZED   3  // Activate this window and maximize
  192.       #define SW_SHOWMAXNOACTIVE -3 // Display maximized; active window stays active
  193.       #define SW_SHOWMINIMIZED   2  // Activate this window and display as icon
  194.       #define SW_SHOWMINNOACTIVE 7  // Display as icon; active window stays active
  195.       #define SW_SHOWNA          8  // don't change window state or active window
  196.       #define SW_SHOWNOACTIVATE  4  // display in recent size and place; active window stays active
  197.       #define SW_SHOWNORMAL      1  // same as SW_RESTORE
  198. //
  199. //
  200. //***** GetActiveWindow(): Get the active window
  201. // SYNTAX: int GetActiveWindow()
  202. // RETURN: Return window handle for currently active window
  203. //
  204. //
  205. //***** SetActiveWindow(): Set the currently active window
  206. // SYNTAX: bool SetActiveWindow(int WindowHandle)
  207. //         bool SetActiveWindow(string WindowTitle)
  208. // WHERE: WindowHandle: Integer identifier for this window
  209. //        WindowTitle: Partial text title of the window (case-insensitive)
  210. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  211. //
  212. //***** GetFocus(): Get window with the input focus
  213. // SYNTAX: int GetFocus()
  214. // RETURN: Return handle of window that owns keyboard focus
  215. //
  216. //
  217.  
  218. /*******************************************************************
  219.  *********        END OF DESCRIPTION FOR WINSET.LIB        *********
  220.  *******************************************************************/
  221.  
  222. GetWindowHandle(WindowSpec)
  223. {
  224.    // Test if input is string (WindowTitle) or integer (WindowHandle)
  225.    if ( 0 == DataDimension(WindowSpec) ) {
  226.  
  227.       // Input is WindowHandle.  Verify that this handle is valid
  228.       if ( DynamicLink("USER","ISWINDOW",SWORD16,PASCAL,WindowSpec) )
  229.          return(WindowSpec);
  230.  
  231.    } else {
  232.  
  233.       // Input is WindowTitle, build list of titles and look for this one
  234.       _TitleLen = strlen(WindowSpec);
  235.  
  236.       // Build list of all root window titles
  237.       if ( NULL != (_winList = WindowList()) ) {
  238.  
  239.          // for each window, compare its title against Window
  240.          BLObSize(_buf,200);
  241.          for ( _i = GetArraySpan(_winList); 0 <= _i; _i-- ) {
  242.             // compare the title for this window, if it has one
  243.             if ( DynamicLink("USER","GETWINDOWTEXT",SWORD16,PASCAL,_winList[_i],_buf,199)
  244.               && !memicmp(WindowSpec,_buf,_TitleLen) )
  245.                return(_winList[_i]);
  246.          }
  247.  
  248.       }
  249.    }
  250.    // if didn't return a handle above, then title was not found
  251.    return(0);
  252. }
  253.  
  254.  
  255.  
  256. GetWindowTitle(WindowSpec)
  257. {
  258.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  259.       _ret = NULL;
  260.    else {
  261.       BLObSize(_buf,200);
  262.       if ( !DynamicLink("USER","GETWINDOWTEXT",SWORD16,PASCAL,WindowSpec,_buf,199) )
  263.          _ret = NULL;
  264.       else     // copy to short string and return that
  265.          strcpy(_ret,_buf);
  266.    }
  267.    return(_ret);
  268. }
  269.  
  270.  
  271. SetWindowTitle(WindowSpec,NewTitle)
  272. {
  273.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  274.       return(FALSE);
  275.    DynamicLink("USER","SETWINDOWTEXT",SWORD16,PASCAL,_handle,NewTitle);
  276.    return(TRUE);
  277. }
  278.  
  279.  
  280. IsWindow(WindowSpec)
  281. {
  282.    return( GetWindowHandle(WindowSpec) );
  283. }
  284.  
  285.  
  286. IsMinimized(WindowSpec)
  287. {
  288.    return( (_handle = GetWindowHandle(WindowSpec))
  289.          ? DynamicLink("USER","ISICONIC",SWORD16,PASCAL,_handle)
  290.          : FALSE );
  291. }
  292.  
  293.  
  294. IsMaximized(WindowSpec)
  295. {
  296.    return( (_handle = GetWindowHandle(WindowSpec))
  297.          ? DynamicLink("USER","ISZOOMED",SWORD16,PASCAL,_handle)
  298.          : FALSE );
  299. }
  300.  
  301.  
  302.  
  303. IsVisible(WindowSpec)
  304. {
  305.    return( (_handle = GetWindowHandle(WindowSpec))
  306.          ? DynamicLink("USER","ISWINDOWVISIBLE",SWORD16,PASCAL,_handle)
  307.          : FALSE );
  308. }
  309.  
  310.  
  311. IsEnabled(WindowSpec)
  312. {
  313.    return( (_handle = GetWindowHandle(WindowSpec))
  314.          ? DynamicLink("USER","ISWINDOWENABLED",SWORD16,PASCAL,_handle)
  315.          : FALSE );
  316. }
  317.  
  318.  
  319. GetWindowRect(pWindowSpec,pRectangleOrLeft,pTop,pRight,pBottom)
  320. {
  321.    if ( !(lHwnd = GetWindowHandle(pWindowSpec)) )
  322.       return(FALSE);
  323.    // set up blob to retrieve four integers
  324.    BLObSize(lRectBLOb,4 * 2/*integer size*/);
  325.    DynamicLink("USER","GETWINDOWRECT",SWORD16,PASCAL,lHwnd,lRectBLOb);
  326.    lLeft = BLObGet(lRectBLOb,0,SWORD16);
  327.    lTop = BLObGet(lRectBLOb,2,SWORD16);
  328.    lRight = BLObGet(lRectBLOb,4,SWORD16);
  329.    lBottom = BLObGet(lRectBLOb,6,SWORD16);
  330.    if ( 2 == va_arg() )
  331.       pRectangleOrLeft.left = lLeft, pRectangleOrLeft.right = lRight,
  332.       pRectangleOrLeft.top = lTop, pRectangleOrLeft.bottom = lBottom;
  333.    else
  334.       pRectangleOrLeft = lLeft, pRight = lRight, pTop = lTop, pBottom = lBottom;
  335.    return(TRUE);
  336. }
  337.  
  338.  
  339. SetWindowRect(pWindowSpec,pRectangleOrLeft,pTop,pRight,pBottom)
  340. {
  341.    if ( !(lHwnd = GetWindowHandle(pWindowSpec)) )
  342.       return(FALSE);
  343.    if ( 2 == va_arg() )
  344.       lLeft = pRectangleOrleft.left, lRight = pRectangleOrleft.right,
  345.       lTop = pRectangleOrleft.top, lBottom = pRectangleOrleft.bottom;
  346.    else
  347.       lLeft = pRectangleOrleft, lRight = pRight, lTop = pTop, lBottom = pBottom;
  348.    lWidth = lRight - lLeft + 1;
  349.    lHeight = lBottom - lTop + 1;
  350.    DynamicLink("USER","MOVEWINDOW",SWORD16,PASCAL,
  351.                lHwnd,lLeft,lTop,lWidth,lHeight,TRUE);
  352.    return(TRUE);
  353. }
  354.  
  355. GetClientRect(pWindowSpec,pRectangle)
  356. {
  357.    if ( !(_handle = GetWindowHandle(pWindowSpec)) )
  358.       return(FALSE);
  359.    // set up blob to retrieve four integers
  360.    BLObSize(_rect,4 * 2/*integer size*/);
  361.    DynamicLink("USER","GETCLIENTRECT",SWORD16,PASCAL,_handle,_rect);
  362.    pRectangle.left = BLObGet(_rect,0,SWORD16);
  363.    pRectangle.top = BLObGet(_rect,2,SWORD16);
  364.    pRectangle.right = BLObGet(_rect,4,SWORD16);
  365.    pRectangle.bottom = BLObGet(_rect,6,SWORD16);
  366.    return(TRUE);
  367. }
  368.  
  369. GetScreenSize(Width,Height)
  370. {
  371.    #define SM_CXSCREEN  0
  372.    #define SM_CYSCREEN  1
  373.    Width = DynamicLink("USER","GETSYSTEMMETRICS",SWORD16,PASCAL,SM_CXSCREEN);
  374.    Height = DynamicLink("USER","GETSYSTEMMETRICS",SWORD16,PASCAL,SM_CYSCREEN);
  375. }
  376.  
  377.  
  378. GetSize(pWindowSpec,pWidth,pHeight)
  379. {
  380.    if ( !GetWindowRect(pWindowSpec,lRect) )
  381.       return(FALSE);
  382.    pWidth = lRect.right - lRect.left + 1;
  383.    pHeight = lRect.bottom - lRect.top + 1;
  384.    return(TRUE);
  385. }
  386.  
  387.  
  388. SetSize(WindowSpec,Width,Height)
  389. {
  390.    if ( !GetWindowRect(WindowSpec,_rect) )
  391.       return(FALSE);
  392.    _rect.right = _rect.left + Width - 1;
  393.    _rect.bottom = _rect.top + Height - 1;
  394.    SetWindowRect(WindowSpec,_rect);
  395.    return(TRUE);
  396. }
  397.  
  398.  
  399. GetPosition(WindowSpec,LeftCol,TopRow)
  400. {
  401.    if ( !GetWindowRect(WindowSpec,_rect) )
  402.       return(FALSE);
  403.    LeftCol = _rect.left;
  404.    TopRow = _rect.top;
  405.    return(TRUE);
  406. }
  407.  
  408.  
  409. SetPosition(WindowSpec,LeftCol,TopRow)
  410. {
  411.    if ( !GetWindowRect(WindowSpec,_rect) )
  412.       return(FALSE);
  413.    _deltaCol = LeftCol - _rect.left;
  414.    _deltaRow = TopRow - _rect.top;
  415.    _rect.left = LeftCol;
  416.    _rect.right += _deltaCol;
  417.    _rect.top = TopRow;
  418.    _rect.bottom += _deltaRow;
  419.    SetWindowRect(WindowSpec,_rect);
  420.    return(TRUE);
  421. }
  422.  
  423.  
  424. ShowWindow(WindowSpec,CmdShow)
  425. {
  426.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  427.       return(FALSE);
  428.    if ( CmdSHow < 0 ) {
  429.       // Windows does not directly provide these commands without activating
  430.       // the window, and so here will find what window is active, maximize
  431.       // the selected window, then restore the active window
  432.       _active = GetActiveWindow();
  433.       ShowWindow(_handle,-CmdShow);
  434.       SetActiveWindow(_active);
  435.    } else {
  436.       DynamicLink("USER","SHOWWINDOW",SWORD16,PASCAL,_handle,CmdShow);
  437.    }
  438.    return(TRUE);
  439. }
  440.  
  441.  
  442. GetActiveWindow()
  443. {
  444.    return DynamicLink("USER","GETACTIVEWINDOW",SWORD16,PASCAL);
  445. }
  446.  
  447.  
  448. SetActiveWindow(WindowSpec)
  449. {
  450.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  451.       return(FALSE);
  452.    DynamicLink("USER","SETACTIVEWINDOW",SWORD16,PASCAL,_handle);
  453.    return(TRUE);
  454. }
  455.  
  456. GetFocus()
  457. {
  458.    return DynamicLink("USER","GETFOCUS",UWORD16,PASCAL);
  459. }
  460.  
  461.