home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource1 / cenvew / wintools.lib < prev    next >
Encoding:
Text File  |  1993-11-08  |  15.3 KB  |  415 lines

  1. // WinTools.lib - Functions for setting the state of of Windows.  This
  2. //                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,struct Rectangle)
  90. //         bool GetWindowRect(string WindowTitle,struct Rectangle)
  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. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  99. // MODIFY: Modifies Rectangle structure to current coordinates
  100. //
  101. //
  102. //***** SetWindowRect(): Set current window coordinates
  103. // SYNTAX: bool SetWindowRect(int WindowHandle,struct Rectangle)
  104. //         bool SetWindowRect(string WindowTitle,struct Rectangle)
  105. // WHERE: WindowHandle: Integer identifier for this window
  106. //        WindowTitle: Partial text title of the window (case-insensitive)
  107. //        Rectangle: new structurw of window with the same member elements as
  108. //                   defined above in GetWindowRect()
  109. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  110. //
  111. //
  112. //***** GetScreenSize(): Get width and height of the windows screen
  113. // SYNTAX: void GetScreenSize(int Width,int Height);
  114. // WHERE: Width: receive screen width
  115. //        Height: receive screen height
  116. // MODIFY: Sets Width and Height to dimensions of screen
  117. //
  118. //
  119. //***** GetSize(): Get current width and height of window
  120. // SYNTAX: bool GetSize(int WindowHandle,int Width,int Height)
  121. //         bool GetSize(string WindowTitle,int Width,int Height)
  122. // WHERE: WindowHandle: Integer identifier for this window
  123. //        WindowTitle: Partial text title of the window (case-insensitive)
  124. //        Width: receive current window width
  125. //        Height: receive current window height
  126. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  127. // MODIFY: Sets Width and Height to current dimensions of window
  128. //
  129. //
  130. //***** SetSize(): Get current width and height of window
  131. // SYNTAX: bool GetSize(int WindowHandle,int Width,int Height)
  132. //         bool GetSize(string WindowTitle,int Width,int Height)
  133. // WHERE: WindowHandle: Integer identifier for this window
  134. //        WindowTitle: Partial text title of the window (case-insensitive)
  135. //        Width: new window width
  136. //        Height: new window height
  137. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  138. //
  139. //
  140. //***** GetPosition(): Get current column and row of window
  141. // SYNTAX: bool GetPosition(int WindowHandle,int LeftCol,int TopRow)
  142. //         bool GetPosition(string WindowTitle,int LeftCol,int TopRow)
  143. // WHERE: WindowHandle: Integer identifier for this window
  144. //        WindowTitle: Partial text title of the window (case-insensitive)
  145. //        LeftCol: receive current column coordinate of upper-left corner of window
  146. //        Height: receive current row coordinate of upper-left corner of window
  147. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  148. // MODIFY: Sets LeftCol and TopRow to current upper-left corner coordinates
  149. //
  150. //
  151. //***** SetPosition(): Get current column and row of window
  152. // SYNTAX: bool SetPosition(int WindowHandle,int LeftCol,int TopRow)
  153. //         bool SetPosition(string WindowTitle,int LeftCol,int TopRow)
  154. // WHERE: WindowHandle: Integer identifier for this window
  155. //        WindowTitle: Partial text title of the window (case-insensitive)
  156. //        LeftCol: new column coordinate of upper-left corner of window
  157. //        Height: new row coordinate of upper-left corner of window
  158. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  159. //
  160. //
  161. //***** ShowWindow(): Set the way the window is shown (or not shown)
  162. // SYNTAX: bool ShowWindow(int WindowHandle,int ShowCommand)
  163. //         bool ShowWindow(string WindowTitle,int ShowCommand)
  164. // WHERE: WindowHandle: Integer identifier for this window
  165. //        WindowTitle: Partial text title of the window (case-insensitive)
  166. //        ShowCommand: Any of the following values:
  167.       #define SW_HIDE            0  // Hide window; make NOT the active window
  168.       #define SW_MINIMIZE        6  // minimize window; make NOT the active window
  169.       #define SW_RESTORE         9  // activate and display this window; restore from minimized
  170.                                     // or maximized to original size and position before
  171.                                     // it was minimized or maximized
  172.       #define SW_RESTORENOACTIVE -9 // Same as restore but active window remains active
  173.       #define SW_SHOW            5  // Activate this window in current size and position
  174.       #define SW_SHOWMAXIMIZED   3  // Activate this window and maximize
  175.       #define SW_SHOWMAXNOACTIVE -3 // Display maximized; active window stays active
  176.       #define SW_SHOWMINIMIZED   2  // Activate this window and display as icon
  177.       #define SW_SHOWMINNOACTIVE 7  // Display as icon; active window stays active
  178.       #define SW_SHOWNA          8  // don't change window state or active window
  179.       #define SW_SHOWNOACTIVATE  4  // display in recent size and place; active window stays active
  180.       #define SW_SHOWNORMAL      1  // same as SW_RESTORE
  181. //
  182. //
  183. //***** GetActiveWindow(): Get the active window
  184. // SYNTAX: int GetActiveWindow()
  185. // RETURN: Return window handle for currently active window
  186. //
  187. //
  188. //***** SetActiveWindow(): Set the currently active window
  189. // SYNTAX: bool SetActiveWindow(int WindowHandle)
  190. //         bool SetActiveWindow(string WindowTitle)
  191. // WHERE: WindowHandle: Integer identifier for this window
  192. //        WindowTitle: Partial text title of the window (case-insensitive)
  193. // RETURN: TRUE if WindowSpec is valid, else FALSE.
  194. //
  195. //
  196.  
  197. #define  _WINTOOLS_LIB  1
  198.  
  199. /*******************************************************************
  200.  *********        END OF DESCRIPTION FOR WINSET.LIB        *********
  201.  *******************************************************************/
  202.  
  203. GetWindowHandle(WindowSpec)
  204. {
  205.    // Test if input is string (WindowTitle) or integer (WindowHandle)
  206.    if ( 0 == DataDimension(WindowSpec) ) {
  207.  
  208.       // Input is WindowHandle.  Verify that this handle is valid
  209.       if ( DynamicLink("USER","ISWINDOW",SWORD16,PASCAL,WindowSpec) )
  210.          return(WindowSpec);
  211.  
  212.    } else {
  213.  
  214.       // Input is WindowTitle, build list of titles and look for this one
  215.       _TitleLen = strlen(WindowSpec);
  216.  
  217.       // Build list of all root window titles
  218.       if ( NULL != (_winList = WindowList()) ) {
  219.  
  220.          // for each window, compare its title against Window
  221.          BLObSize(_buf,200);
  222.          for ( _i = GetArraySpan(_winList); 0 <= _i; _i-- ) {
  223.             // compare the title for this window, if it has one
  224.             if ( DynamicLink("USER","GETWINDOWTEXT",SWORD16,PASCAL,_winList[_i],_buf,199)
  225.               && !memicmp(WindowSpec,_buf,_TitleLen) )
  226.                return(_winList[_i]);
  227.          }
  228.  
  229.       }
  230.    }
  231.    // if didn't return a handle above, then title was not found
  232.    return(0);
  233. }
  234.  
  235.  
  236.  
  237. GetWindowTitle(WindowSpec)
  238. {
  239.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  240.       _ret = NULL;
  241.    else {
  242.       BLObSize(_buf,200);
  243.       if ( !DynamicLink("USER","GETWINDOWTEXT",SWORD16,PASCAL,WindowSpec,_buf,199) )
  244.          _ret = NULL;
  245.       else     // copy to short string and return that
  246.          strcpy(_ret,_buf);
  247.    }
  248.    return(_ret);
  249. }
  250.  
  251.  
  252. SetWindowTitle(WindowSpec,NewTitle)
  253. {
  254.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  255.       return(FALSE);
  256.    DynamicLink("USER","SETWINDOWTEXT",SWORD16,PASCAL,_handle,NewTitle);
  257.    return(TRUE);
  258. }
  259.  
  260.  
  261. IsWindow(WindowSpec)
  262. {
  263.    return( GetWindowHandle(WindowSpec) );
  264. }
  265.  
  266.  
  267. IsMinimized(WindowSpec)
  268. {
  269.    return( (_handle = GetWindowHandle(WindowSpec))
  270.          ? DynamicLink("USER","ISICONIC",SWORD16,PASCAL,_handle)
  271.          : FALSE );
  272. }
  273.  
  274.  
  275. IsMaximized(WindowSpec)
  276. {
  277.    return( (_handle = GetWindowHandle(WindowSpec))
  278.          ? DynamicLink("USER","ISZOOMED",SWORD16,PASCAL,_handle)
  279.          : FALSE );
  280. }
  281.  
  282.  
  283.  
  284. IsVisible(WindowSpec)
  285. {
  286.    return( (_handle = GetWindowHandle(WindowSpec))
  287.          ? DynamicLink("USER","ISWINDOWVISIBLE",SWORD16,PASCAL,_handle)
  288.          : FALSE );
  289. }
  290.  
  291.  
  292. IsEnabled(WindowSpec)
  293. {
  294.    return( (_handle = GetWindowHandle(WindowSpec))
  295.          ? DynamicLink("USER","ISWINDOWENABLED",SWORD16,PASCAL,_handle)
  296.          : FALSE );
  297. }
  298.  
  299.  
  300. GetWindowRect(WindowSpec,Rectangle)
  301. {
  302.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  303.       return(FALSE);
  304.    // set up blob to retrieve four integers
  305.    BLObSize(_rect,4 * 2/*integer size*/);
  306.    DynamicLink("USER","GETWINDOWRECT",SWORD16,PASCAL,_handle,_rect);
  307.    Rectangle.left = BLObGet(_rect,0,SWORD16);
  308.    Rectangle.top = BLObGet(_rect,2,SWORD16);
  309.    Rectangle.right = BLObGet(_rect,4,SWORD16);
  310.    Rectangle.bottom = BLObGet(_rect,6,SWORD16);
  311.    return(TRUE);
  312. }
  313.  
  314.  
  315. SetWindowRect(WindowSpec,Rectangle)
  316. {
  317.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  318.       return(FALSE);
  319.    _width = Rectangle.right - Rectangle.left + 1;
  320.    _height = Rectangle.bottom - Rectangle.top + 1;
  321.    DynamicLink("USER","MOVEWINDOW",SWORD16,PASCAL,
  322.                _handle,Rectangle.left,Rectangle.top,_width,_height,TRUE);
  323.    return(TRUE);
  324. }
  325.  
  326.  
  327. GetScreenSize(Width,Height)
  328. {
  329.    #define SM_CXSCREEN  0
  330.    #define SM_CYSCREEN  1
  331.    Width = DynamicLink("USER","GETSYSTEMMETRICS",SWORD16,PASCAL,SM_CXSCREEN);
  332.    Height = DynamicLink("USER","GETSYSTEMMETRICS",SWORD16,PASCAL,SM_CYSCREEN);
  333. }
  334.  
  335.  
  336. GetSize(WindowSpec,Width,Height)
  337. {
  338.    if ( !GetWindowRect(WindowSpec,_rect) )
  339.       return(FALSE);
  340.    Width = _rect.right - _rect.left + 1;
  341.    Height = _rect.bottom - _rect.top + 1;
  342.    return(TRUE);
  343. }
  344.  
  345.  
  346. SetSize(WindowSpec,Width,Height)
  347. {
  348.    if ( !GetWindowRect(WindowSpec,_rect) )
  349.       return(FALSE);
  350.    _rect.right = _rect.left + Width - 1;
  351.    _rect.bottom = _rect.top + Height - 1;
  352.    SetWindowRect(WindowSpec,_rect);
  353.    return(TRUE);
  354. }
  355.  
  356.  
  357. GetPosition(WindowSpec,LeftCol,TopRow)
  358. {
  359.    if ( !GetWindowRect(WindowSpec,_rect) )
  360.       return(FALSE);
  361.    LeftCol = _rect.left;
  362.    TopRow = _rect.top;
  363.    return(TRUE);
  364. }
  365.  
  366.  
  367. SetPosition(WindowSpec,LeftCol,TopRow)
  368. {
  369.    if ( !GetWindowRect(WindowSpec,_rect) )
  370.       return(FALSE);
  371.    _deltaCol = LeftCol - _rect.left;
  372.    _deltaRow = TopRow - _rect.top;
  373.    _rect.left = LeftCol;
  374.    _rect.right += _deltaCol;
  375.    _rect.top = TopRow;
  376.    _rect.bottom += _deltaRow;
  377.    SetWindowRect(WindowSpec,_rect);
  378.    return(TRUE);
  379. }
  380.  
  381.  
  382. ShowWindow(WindowSpec,CmdShow)
  383. {
  384.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  385.       return(FALSE);
  386.    if ( CmdSHow < 0 ) {
  387.       // Windows does not directly provide these commands without activating
  388.       // the window, and so here will find what window is active, maximize
  389.       // the selected window, then restore the active window
  390.       _active = GetActiveWindow();
  391.       ShowWindow(_handle,-CmdShow);
  392.       SetActiveWindow(_active);
  393.    } else {
  394.       DynamicLink("USER","SHOWWINDOW",SWORD16,PASCAL,_handle,CmdShow);
  395.    }
  396.    return(TRUE);
  397. }
  398.  
  399.  
  400. GetActiveWindow()
  401. {
  402.    return DynamicLink("USER","GETACTIVEWINDOW",SWORD16,PASCAL);
  403. }
  404.  
  405.  
  406. SetActiveWindow(WindowSpec)
  407. {
  408.    if ( !(_handle = GetWindowHandle(WindowSpec)) )
  409.       return(FALSE);
  410.    DynamicLink("USER","SETACTIVEWINDOW",SWORD16,PASCAL,_handle);
  411.    return(TRUE);
  412. }
  413.  
  414.  
  415.