home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 01e / msjall.zip / MSJV2-3.ZIP / TILER.ALL < prev    next >
Text File  |  1987-10-30  |  12KB  |  432 lines

  1. Microsoft Systems Journal
  2. Volume 2; Issue 3; July, 1987
  3.  
  4. Code Listings For:
  5.  
  6.     TILER
  7.     pp. 19-36
  8.  
  9. Author(s): Michael Geary
  10. Title:     Microsoft Windows 2.0: Enhancements Offer Developers More Control
  11.  
  12.  
  13.  
  14.  
  15. Figure 7.
  16. =========
  17.  
  18. Caption: Code Listing for TILER.C
  19.  
  20.  
  21.  
  22. TILER.C
  23.  
  24. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *\
  25.  *  Tiler.c                                                        *
  26.  *  Windows 2.0 Tiling Utility                                     *
  27.  *  Written by Michael Geary                                       *
  28. \* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  29.  
  30. #include <windows.h>
  31.  
  32. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33.  
  34. #define MAXINT      32767
  35.  
  36. /* Menu command definitions */
  37.  
  38. #define CMD_TILECOLS    1
  39. #define CMD_TILEROWS    2
  40.  
  41.  
  42. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  43.  
  44. typedef struct {
  45.     HWND    hWnd;
  46.     RECT    rect;
  47. } WINDOW;
  48.  
  49. WINDOW      Window[4];        /* Window info for each tiled window */
  50. int         nWindows;         /* How many windows we will tile */
  51.  
  52. HANDLE      hInstance;        /* Our instance handle */
  53. int         hWndTiler;        /* hWnd of our icon */
  54. RECT        TileRect;         /* Overall tiling rectangle */
  55.  
  56. char        szClass[] = "Tiler!";         /* Our window class name */
  57. char        szTitle[] = "Tiler";          /* Our window title */
  58. char        szTileCols[] = "&Tile Columns";
  59. char        szTileRows[] = "Tile &Rows";
  60.  
  61. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  62.  
  63.  
  64. /*  Declare full templates for all our functions.  This gives us 
  65.  *  strong type checking on our functions.
  66.  */
  67.  
  68. void                CalcWindowRects( BOOL );
  69. BOOL                Initialize( void );
  70. BOOL                IsTileable( HWND );
  71. long    FAR PASCAL  TilerWndProc( HWND, unsigned, WORD, LONG );
  72. void                TileWindows( BOOL );
  73. int         PASCAL  WinMain( HANDLE, HANDLE, LPSTR, int );
  74.  
  75. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  76.  
  77.  
  78. /*  Calculate window rectangles for the four topmost tileable windows
  79.  *  and set up the Window array for them.  This is a simple-minded
  80.  *  tiling algorithm that simply divides the tiling area into equal
  81.  *  rows and columns.
  82.  */
  83.  
  84. void CalcWindowRects( bColumns )
  85.     BOOL        bColumns;
  86. {
  87.     HWND        hWnd;
  88.     int         n;
  89.  
  90.     n = 0;
  91.     for(
  92.         hWnd = GetWindow( hWndTiler, GW_HWNDFIRST );
  93.         hWnd;
  94.         hWnd = GetWindow( hWnd, GW_HWNDNEXT )
  95.     ) {
  96.         if( ! IsTileable( hWnd ) )  continue;
  97.  
  98.         Window[n].hWnd = hWnd;
  99.         CopyRect( &Window[n].rect, &TileRect );
  100.  
  101.         switch( n ) {
  102.             case 0:
  103.                 break;
  104.             case 1:
  105.                 if( bColumns ) {
  106.                     Window[0].rect.right = TileRect.right / 2;
  107.                     Window[1].rect.left = Window[0].rect.right - 1;
  108.                 } else {
  109.                     Window[0].rect.bottom = TileRect.bottom / 2;
  110.                     Window[1].rect.top = Window[0].rect.bottom - 1;
  111.                 }
  112.                 break;
  113.             case 2:
  114.                 if( bColumns ) {
  115.                     Window[2].rect.left = Window[1].rect.left;
  116.                     Window[1].rect.bottom = TileRect.bottom / 2;
  117.                     Window[2].rect.top = Window[1].rect.bottom - 1;
  118.                 } else {
  119.                     Window[2].rect.top = Window[1].rect.top;
  120.                     Window[1].rect.right = TileRect.right / 2;
  121.                     Window[2].rect.left = Window[1].rect.right - 1;
  122.                 }
  123.                 break;
  124.             case 3:
  125.                 if( bColumns ) {
  126.                     Window[3].rect.right = Window[0].rect.right;
  127.                     Window[0].rect.bottom = TileRect.bottom / 2;
  128.                     Window[3].rect.top = Window[0].rect.bottom - 1;
  129.                 } else {
  130.                     Window[3].rect.bottom = Window[0].rect.bottom;
  131.                     Window[0].rect.right = TileRect.right / 2;
  132.                     Window[3].rect.left = Window[0].rect.right - 1;
  133.                 }
  134.                 break;
  135.         }
  136.         if( ++n == 4 )  break;
  137.     }
  138.     nWindows = n;
  139. }
  140.  
  141. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  142.  
  143.  
  144. /*  Initialize TILER.  Assumes a single instance.
  145.  *  Returns TRUE if initialization succeeded, FALSE if failed.
  146.  */
  147.  
  148. BOOL Initialize()
  149. {
  150.     WNDCLASS    Class;        /* Class structure for RegisterClass */
  151.     HMENU       hMenu;        /* Menu handle of system menu */
  152.  
  153.     /* Register our window class */
  154.     Class.style          = 0;
  155.     Class.cbClsExtra     = 0;
  156.     Class.cbWndExtra     = 0;
  157.     Class.lpfnWndProc    = TilerWndProc;
  158.     Class.hInstance      = hInstance;
  159.     Class.hIcon          = LoadIcon( hInstance, szClass );
  160.     Class.hCursor        = LoadCursor( NULL, IDC_ARROW );
  161.     Class.hbrBackground  = COLOR_WINDOW + 1;
  162.     Class.lpszMenuName   = NULL;
  163.     Class.lpszClassName  = szClass;
  164.  
  165.     if( ! RegisterClass( &Class ) )  return FALSE;
  166.  
  167.     /* Create our window but don't iconize it yet */
  168.     hWndTiler = CreateWindow(
  169.         szClass, szTitle,
  170.         WS_OVERLAPPED | WS_SYSMENU,
  171.         CW_USEDEFAULT, 0,
  172.         CW_USEDEFAULT, 0,
  173.         NULL, NULL, hInstance, NULL
  174.     );
  175.     if( ! hWndTiler )  return FALSE;
  176.  
  177.     /* Since we took the default size, the bottom of our window is
  178.      * the base Y coordinate for tiling */
  179.     GetWindowRect( hWndTiler, &TileRect );
  180.     TileRect.top = TileRect.left = -1;
  181.     TileRect.right = GetSystemMetrics( SM_CXSCREEN ) + 1;
  182.  
  183.     /* Add our menu items to the System (Control) menu */
  184.     hMenu = GetSystemMenu( hWndTiler, FALSE);
  185.     ChangeMenu( hMenu, 0, NULL, MAXINT, MF_APPEND | MF_SEPARATOR );
  186.     ChangeMenu( hMenu, 0, szTileCols, CMD_TILECOLS, 
  187.                 MF_APPEND | MF_STRING );
  188.     ChangeMenu( hMenu, 0, szTileRows, CMD_TILEROWS, 
  189.                 MF_APPEND | MF_STRING );
  190.  
  191.     /* Now display our window as an icon */
  192.     ShowWindow( hWndTiler, SW_SHOWMINIMIZED );
  193.  
  194.     return TRUE;
  195. }
  196.  
  197. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  198.  
  199.  
  200. /*  Tells whether a window can be tiled, returns TRUE if so.
  201.  *  We will tile only top level, resizable windows that are not
  202.  *  minimized and not maximized.
  203.  */
  204.  
  205. BOOL IsTileable( hWnd )
  206.     HWND        hWnd;
  207. {
  208.     DWORD       dwStyle;
  209.  
  210.     dwStyle = GetWindowLong( hWnd, GWL_STYLE );
  211.     return(
  212.         ! ( dwStyle & ( WS_POPUP | WS_MINIMIZE | WS_MAXIMIZE ) )  &&
  213.           ( dwStyle & WS_SIZEBOX )  &&
  214.           ( dwStyle & WS_VISIBLE )
  215.     );
  216. }
  217.  
  218. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  219.  
  220.  
  221. /*  Tiler's window function.
  222.  */
  223.  
  224. long FAR PASCAL TilerWndProc( hWnd, wMsg, wParam, lParam )
  225.     HWND        hWnd;            /* Window handle */
  226.     unsigned    wMsg;            /* Message number */
  227.     WORD        wParam;          /* Word parameter for the message */
  228.     LONG        lParam;          /* Long parameter for the message */
  229. {
  230.     RECT        rect;            /* A rectangle */
  231.  
  232.     switch( wMsg ) {
  233.  
  234.     /* Destroy-window message - time to quit the application */
  235.         case WM_DESTROY:
  236.             PostQuitMessage( 0 );
  237.             return 0L;
  238.  
  239.     /* Query open icon message - don't allow icon to be opened! */
  240.         case WM_QUERYOPEN:
  241.             return 0L;
  242.  
  243.     /* System menu command message - process the command if ours */
  244.         case WM_SYSCOMMAND:
  245.             switch( wParam ) {
  246.                 case CMD_TILECOLS:
  247.                     TileWindows( TRUE );
  248.                     return 0L;
  249.                 case CMD_TILEROWS:
  250.                     TileWindows( FALSE );
  251.                     return 0L;
  252.                 default:
  253.                     break;
  254.             }
  255.             break;
  256.  
  257.     /* For all other messages, we pass them on to DefWindowProc */
  258.         default:
  259.             break;
  260.     }
  261.     return DefWindowProc( hWnd, wMsg, wParam, lParam );
  262. }
  263.  
  264. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  265.  
  266. /*  This function actually tiles the windows.  First, it calls
  267.  *  CalcWindowRects to determine the window positions.  Then, it 
  268.  *  loops through the windows and moves them into place.
  269.  */
  270.  
  271. void TileWindows( bColumns )
  272.     BOOL        bColumns;     /* TRUE = tile columns; FALSE = rows */
  273. {
  274.     int         n;
  275.     HWND        hWnd = NULL;
  276.  
  277.     CalcWindowRects( bColumns );  /* Assign window rectangles */
  278.  
  279.     if( nWindows == 0 ) {
  280.         MessageBox(
  281.             hWndTiler,
  282.             "There are no windows that can be tiled.",
  283.             szTitle,
  284.             MB_OK | MB_ICONEXCLAMATION
  285.         );
  286.         return;
  287.     }
  288.  
  289.     /* Move, size, and reorder windows */
  290.     for( n = 0;  n < nWindows;  ++n ) { 
  291.         SetWindowPos(
  292.             Window[n].hWnd,
  293.             hWnd,
  294.             Window[n].rect.left,
  295.             Window[n].rect.top,
  296.             Window[n].rect.right  - Window[n].rect.left,
  297.             Window[n].rect.bottom - Window[n].rect.top,
  298.             SWP_NOACTIVATE
  299.         );
  300.         hWnd = Window[n].hWnd;
  301.         if( GetClassWord( hWnd, GCW_STYLE ) & 
  302.                         ( CS_HREDRAW | CS_VREDRAW ) )
  303.             /* Make sure it's redrawn */
  304.             InvalidateRect( hWnd, NULL, TRUE );
  305.     }
  306. }
  307.  
  308. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  309.  
  310. /*  Application main program. */
  311.  
  312. int PASCAL WinMain( hInst, hPrevInst, lpszCmdLine, nCmdShow )
  313.  
  314.     /* Our instance handle */
  315.     HANDLE      hInst; 
  316.     /* Previous instance of this application*/
  317.     HANDLE      hPrevInst; 
  318.     /* Pointer to any command line params*/
  319.     LPSTR       lpszCmdLine;
  320.     /* Parameter to use for first ShowWindow */
  321.     int         nCmdShow; 
  322. {
  323.     MSG         msg;                /* Message structure */
  324.  
  325.     /* Allow only a single instance */
  326.     if( hPrevInst ) {
  327.         MessageBeep( 0 );
  328.         return 0;
  329.     }
  330.  
  331.     /* Save our instance handle in static variable */
  332.     hInstance = hInst;
  333.  
  334.     /* Initialize application, quit if any errors */
  335.     if( ! Initialize() )  return FALSE;
  336.  
  337.     /* Main message processing loop */
  338.     while( GetMessage( &msg, NULL, 0, 0 ) ) {
  339.         DispatchMessage( &msg );
  340.     }
  341.  
  342.     return msg.wParam;
  343. }
  344.  
  345. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  346.  
  347. =============================[ End of Listing]================================
  348.  
  349.  
  350.  
  351.  
  352. Figure 5.
  353. =========
  354.  
  355.  
  356. Caption: DEF File for TILER.EXE
  357.  
  358.  
  359.  
  360. TILER.DEF
  361.  
  362.  
  363. NAME    Tiler
  364.  
  365. DESCRIPTION 'Windows Tiler by Michael Geary'
  366.  
  367. STUB    'WINSTUB.EXE'
  368.  
  369. CODE    MOVEABLE
  370. DATA    MOVEABLE MULTIPLE
  371.  
  372. HEAPSIZE    1024
  373.  
  374. STACKSIZE   4096
  375.  
  376. EXPORTS
  377.     TilerWndProc    @1
  378.  
  379. =============================[ End of Listing]================================
  380.  
  381.  
  382.  
  383.  
  384. Figure 6.
  385. =========
  386.  
  387.  
  388. Caption: RC File for TILER.EXE
  389.  
  390.  
  391.  
  392.  
  393. TILER.RC
  394.  
  395. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  396.  
  397. /* Tiler.RC - resource file for TILER.EXE */
  398.  
  399. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  400.  
  401. #include <style.h>
  402.  
  403. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  404.  
  405. Tiler!  ICON    tiler.ico
  406.  
  407. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  408.  
  409. =============================[ End of Listing]================================
  410.  
  411.  
  412.  
  413.  
  414. Figure 4.
  415. =========
  416.  
  417. Caption: Make File for TILER.EXE
  418.  
  419.  
  420.  
  421.  
  422. tiler.obj:  tiler.c
  423.     msc -AS -Gcsw -Ox -u -W3 -Zdp $*;
  424.  
  425. tiler.res:  tiler.rc  tiler.ico
  426.     rc -r tiler.rc
  427.  
  428. tiler.exe:  tiler.obj  tiler.res  tiler.def
  429.     link4 tiler, tiler/align:16, tiler/map/line, slibw, tiler.def
  430.     rc tiler.res
  431.     mapsym tiler
  432. =============================[ End of Listing]================================