home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Micrsoft / SAMPLES / VJ6SAMPL.EXE / DeskMgr / DeskMgr.java < prev    next >
Encoding:
Java Source  |  1998-02-24  |  12.0 KB  |  343 lines

  1.  
  2. /*  Copyright (C) Microsoft Corporation, 1996-1998.  All rights reserved.
  3.  
  4.   This source code is intended only as a supplement to Microsoft
  5.   Visual J++ 6.0.  See this product's on-line documentation for detailed   
  6.   information regarding Microsoft code samples.
  7.  
  8. */
  9.  
  10. import wfc.app.*;
  11. import wfc.core.*;
  12. import wfc.ui.*;
  13. import wfc.win32.*;
  14.  
  15. /**
  16.  * DeskMgr is a Desktop Icon Manager that allows you to change the otherwise
  17.  * unchangeable aspects of the icons on your Win95/NT4 desktop.  Knowing that
  18.  * the desktop is, in fact, a Win32 ListView, you can set styles and colors
  19.  * that aren't exposed in the normal desktop properties settings.
  20.  *
  21.  * DeskMgr creates an icon in the system tray using the TrayIcon control class,
  22.  * then allows you to change your desktop icon appearance by selecting items
  23.  * from a popup menu that appears when you right-click the tray icon.  DeskMgr
  24.  * also allows you set appearances automatically on startup by specifying
  25.  * keywords on the command line.
  26.  */
  27. public class DeskMgr extends Form
  28. {
  29.    /**
  30.     * The main entry point for the application.
  31.     *
  32.     * @param <args> Array of parameters passed to the application
  33.     * via the command line.
  34.     */
  35.    public static void main(String args[])
  36.    {
  37.       DeskMgr dm = new DeskMgr();
  38.       boolean doExit = false;
  39.  
  40.       // parse the command line parameters, and invoke code appropriate
  41.       // to the options indicated by the user.
  42.       for (int i = 0; i < args.length; i++)
  43.       {
  44.          if (args[ i ].equalsIgnoreCase( "transparent" ))
  45.             dm.setDesktopListviewTransparent( true );
  46.          else if (args[ i ].equalsIgnoreCase( "opaque" ))
  47.             dm.setDesktopListviewTransparent( false );
  48.          else if (args[ i ].equalsIgnoreCase( "details" ) ||
  49.                   args[ i ].equalsIgnoreCase( "report" ))
  50.             dm.setDesktopListviewStyle( comctl32.LVS_REPORT );
  51.          else if (args[ i ].equalsIgnoreCase( "small" ) ||
  52.                   args[ i ].equalsIgnoreCase( "smallicons" ))
  53.             dm.setDesktopListviewStyle( comctl32.LVS_SMALLICON );
  54.          else if (args[ i ].equalsIgnoreCase( "icon" ) ||
  55.                   args[ i ].equalsIgnoreCase( "largeicon" ) ||
  56.                   args[ i ].equalsIgnoreCase( "large" ))
  57.             dm.setDesktopListviewStyle( comctl32.LVS_ICON );
  58.          else if (args[ i ].equalsIgnoreCase( "exit" ))
  59.             doExit = true;
  60.       }
  61.  
  62.       // if the exit flag was not set...
  63.       if (!doExit)
  64.       {
  65.          // This causes the form to be created, but not shown (it remains
  66.          // hidden).  We want this because the main window hosts the context
  67.          // menu and tray icon controls, but is really just the about box
  68.          // and should normally be hidden.
  69.          dm.getHandle();
  70.  
  71.          // run() with no parameters just enters the msg pump without showing
  72.          // a form window.
  73.          Application.run();
  74.       }
  75.    }
  76.  
  77.    /**
  78.     * The simple ctor, calls the WFC initForm() method (required for the designer).
  79.     */
  80.    public DeskMgr()
  81.    {
  82.       initForm();
  83.    }
  84.  
  85.    /**
  86.     * This method checks/unchecks the menu items according to the item given.
  87.     * When one is checked, the others must be unchecked.
  88.     */
  89.    void checkMenuThingies( MenuItem sender )
  90.    {
  91.       showLarge.setChecked( false );
  92.       showSmall.setChecked( false );
  93.       showList.setChecked( false );
  94.       sender.setChecked( true );
  95.    }
  96.  
  97.    /**
  98.     * This function asserts the new listview style on the desktop listview.
  99.     */
  100.    void setDesktopListviewStyle( int style )
  101.    {
  102.       // fetch the listview control on the desktop
  103.       int   hlv = getDesktopListview();
  104.  
  105.       // create an array for old/new styles.  see WM_STYLECHANGED below...
  106.       int   styles[] = { 0, 0 };
  107.  
  108.       // get the current style
  109.       styles[ 0 ] = Windows.GetWindowLong( hlv, Windows.GWL_STYLE );
  110.  
  111.       // clear the style bits we're interested in
  112.       styles[ 1 ] = 0xfffffffc & styles[ 0 ];
  113.       // set the new style
  114.       styles[ 1 ] |= style;
  115.  
  116.       // and set the style with Win32
  117.       Windows.SetWindowLong( hlv, Windows.GWL_STYLE, styles[ 1 ] );
  118.  
  119.       // on Win95, user32 doesn't detect style changes properly, so we
  120.       // have to nudge it a little.  that's why we needed an array of old/new.
  121.       Windows.SendMessage( hlv, Windows.WM_STYLECHANGED, Windows.GWL_STYLE, styles );
  122.    }
  123.  
  124.    /**
  125.     * This method sets the background color for the desktop listview.
  126.     */
  127.    void setDesktopListviewTransparent( boolean transparent )
  128.    {
  129.       int   hlv;
  130.  
  131.       // fetch the listview control on the desktop
  132.       hlv = getDesktopListview();
  133.  
  134.       // set the text color.  Interestingly, if the color is 0xffffffff,
  135.       // the listview interprets that to mean transparent -- so that's
  136.       // what we do if the parameter indicates so.  Otherwise, we
  137.       // just use the default desktop color (see Win32 API
  138.       // GetSysColor( COLOR_DESKTOP )).
  139.       Windows.SendMessage( hlv, comctl32.LVM_SETTEXTBKCOLOR, 0,
  140.                            transparent ? 0xffffffff : Color.DESKTOP.getRGB()
  141.                          );
  142.  
  143.       // force a repaint of the listview
  144.       Windows.InvalidateRect( hlv, null, true );
  145.    }
  146.  
  147.    /**
  148.     * This method traverses up from the desktop and gets the listview handle.
  149.     *
  150.     * TODO: convert this to use fromHandle() and return a Control object.
  151.     */
  152.    int getDesktopListview()
  153.    {
  154.       int   hwnd;
  155.  
  156.       // start with the desktop window
  157.       hwnd = Windows.GetDesktopWindow();
  158.  
  159.       // look among it's children for the shell, class "Progman"
  160.       hwnd = com.ms.win32.User32.FindWindowEx( hwnd, 0, "Progman", "Program Manager" );
  161.       // then, look among progman's children for the shell default view
  162.       hwnd = com.ms.win32.User32.FindWindowEx( hwnd, 0, "SHELLDLL_DefView", null );
  163.       // then, get the listview that's in the shell default view
  164.       hwnd = com.ms.win32.User32.FindWindowEx( hwnd, 0, "SysListView32", null );
  165.  
  166.       return hwnd;
  167.    }
  168.  
  169.    /**
  170.     * Handle the Show Transparent menu option by toggling the menu check state
  171.     * and setting the transparency of the desktop listview.
  172.     */
  173.    private void showTransparent_click(Object sender, Event e)
  174.    {
  175.       showTransparent.setChecked( !showTransparent.getChecked() );
  176.       setDesktopListviewTransparent( showTransparent.getChecked() );
  177.    }
  178.  
  179.    /**
  180.     * Handle the Show List menu option by setting detailed view on the listview.
  181.     */
  182.    private void showList_click(Object sender, Event e)
  183.    {
  184.       checkMenuThingies( showList  );
  185.       setDesktopListviewStyle( comctl32.LVS_REPORT );
  186.    }
  187.  
  188.    /**
  189.     * Handle the Show Small menu option by setting small icon view on the listview.
  190.     */
  191.    private void showSmall_click(Object sender, Event e)
  192.    {
  193.       checkMenuThingies( showSmall  );
  194.       setDesktopListviewStyle( comctl32.LVS_SMALLICON );
  195.    }
  196.  
  197.    /**
  198.     * Handle the Show Large menu option by setting large icon view on the listview.
  199.     */
  200.    private void showLarge_click(Object sender, Event e)
  201.    {
  202.       checkMenuThingies( showLarge  );
  203.       setDesktopListviewStyle( comctl32.LVS_ICON );
  204.    }
  205.  
  206.    /**
  207.     * Handle the mouse down event on the icon, to act on double click and do the About.
  208.     */
  209.    private void deskmgrIcon_MouseDown(Object sender, MouseEvent e)
  210.    {
  211.       // handle the Doubleclick, and show the About dialog
  212.       if (e.clicks == 2 && e.button == Control.MB_LEFT)
  213.          aboutDeskMgr_click( sender, Event.EMPTY );
  214.    }
  215.  
  216.    /**
  217.     * Handle the OK button (called for Enter, Cancel, or pressing OK) by just
  218.     * hiding the form.
  219.     */
  220.    private void ok_click(Object sender, Event e)
  221.    {
  222.       // re-enable the icon
  223.       deskmgrIcon.setEnabled( true );
  224.       hide();
  225.    }
  226.  
  227.    /**
  228.     * Handle the form's closing to just hide the form when the user presses
  229.     * the close box, presses Alt-F4, or selects Close from the sys menu.
  230.     */
  231.    private void DeskMgr_closing(Object sender, CancelEvent e)
  232.    {
  233.       // Simulate the OK button
  234.       ok_click( null, null );
  235.  
  236.       // Tell WFC to cancel the event, ignoring the close
  237.       e.cancel = true;
  238.    }
  239.  
  240.    /**
  241.     * Handle the Exit menu item, and shut down the WFC app.
  242.     */
  243.    private void exitDeskMgr_click(Object sender, Event e)
  244.    {
  245.       Application.exit();
  246.    }
  247.  
  248.    /**
  249.     * Handle the About menu item, and just show the main form.
  250.     */
  251.    private void aboutDeskMgr_click(Object sender, Event e)
  252.    {
  253.       // On about, just show the main form (it's an about box).
  254.       show();
  255.  
  256.       // Disable the tray icon to prevent reentrancy.
  257.       deskmgrIcon.setEnabled( false );
  258.    }
  259.  
  260.    /**
  261.     * NOTE: The following code is required by the Visual J++ form
  262.     * designer.  It can be modified using the form editor.  Do not
  263.     * modify it using the code editor.
  264.     */
  265.    Container components = new Container();
  266.    Button ok = new Button();
  267.    Label lblDescription = new Label();
  268.    TrayIcon deskmgrIcon = new TrayIcon();
  269.    ContextMenu deskmgrMenu = new ContextMenu();
  270.    MenuItem showSmall = new MenuItem();
  271.    MenuItem showLarge = new MenuItem();
  272.    MenuItem showList = new MenuItem();
  273.    MenuItem separator1 = new MenuItem();
  274.    MenuItem showTransparent = new MenuItem();
  275.    MenuItem separator2 = new MenuItem();
  276.    MenuItem aboutDeskMgr = new MenuItem();
  277.    MenuItem exitDeskMgr = new MenuItem();
  278.  
  279.    private void initForm()
  280.    {
  281.       ResourceManager resources = new ResourceManager(this, "DeskMgr");
  282.       ok.setLocation(new Point(270, 90));
  283.       ok.setSize(new Point(70, 20));
  284.       ok.setTabIndex(0);
  285.       ok.setTabStop(true);
  286.       ok.setText("OK");
  287.       ok.addOnClick(new EventHandler(this.ok_click));
  288.       lblDescription.setLocation(new Point(10, 10));
  289.       lblDescription.setSize(new Point(330, 70));
  290.       lblDescription.setText("WFC Desktop Icon Manager");
  291.       lblDescription.setTabIndex(2);
  292.       showSmall.setText("&Small Icons");
  293.       showSmall.addOnClick(new EventHandler(this.showSmall_click));
  294.       showLarge.setText("&Large Icons");
  295.       showLarge.addOnClick(new EventHandler(this.showLarge_click));
  296.       showList.setText("&Details");
  297.       showList.addOnClick(new EventHandler(this.showList_click));
  298.       separator1.setText("-");
  299.       showTransparent.setText("&Transparent Labels");
  300.       showTransparent.addOnClick(new EventHandler(this.showTransparent_click));
  301.       separator2.setText("-");
  302.       aboutDeskMgr.setText("&About DeskMgr...");
  303.       aboutDeskMgr.addOnClick(new EventHandler(this.aboutDeskMgr_click));
  304.       exitDeskMgr.setText("&Exit");
  305.       exitDeskMgr.addOnClick(new EventHandler(this.exitDeskMgr_click));
  306.       deskmgrMenu.setMenuItems(new MenuItem[] {
  307.          showSmall,
  308.          showLarge,
  309.          showList,
  310.          separator1,
  311.          showTransparent,
  312.          separator2,
  313.          aboutDeskMgr,
  314.          exitDeskMgr});
  315.       /* @designTimeOnly deskmgrMenu.setLocation(new Point(110, 90)); */
  316.       this.setBackColor(Color.CONTROL);
  317.       this.setContextMenu(deskmgrMenu);
  318.       this.setLocation(new Point(0, 0));
  319.       this.setSize(new Point(358, 166));
  320.       this.setTabIndex(-1);
  321.       this.setTabStop(true);
  322.       this.setText("About Desktop Icon Manager");
  323.       this.setAcceptButton(ok);
  324.       this.setAutoScaleBaseSize(13);
  325.       this.setBorderStyle(FormBorderStyle.FIXED_3D);
  326.       this.setCancelButton(ok);
  327.       this.setClientSize(new Point(352, 121));
  328.       this.setMaxButton(false);
  329.       this.setMinButton(false);
  330.       this.addOnClosing(new CancelEventHandler(this.DeskMgr_closing));
  331.       deskmgrIcon.setContextMenu(deskmgrMenu);
  332.       deskmgrIcon.setEnabled(true);
  333.       deskmgrIcon.setIcon((Icon)resources.getObject("deskmgrIcon_icon"));
  334.       deskmgrIcon.setText("WFC Desktop Icon Manager");
  335.       deskmgrIcon.setVisible(true);
  336.       deskmgrIcon.addOnMouseDown(new MouseEventHandler(this.deskmgrIcon_MouseDown));
  337.       /* @designTimeOnly deskmgrIcon.addOnLocation(new Point(0, 90)); */
  338.       this.setNewControls(new Control[] {
  339.          lblDescription,
  340.          ok});
  341.    }
  342. }
  343.