home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-24 | 12.0 KB | 343 lines |
-
- /* Copyright (C) Microsoft Corporation, 1996-1998. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Visual J++ 6.0. See this product's on-line documentation for detailed
- information regarding Microsoft code samples.
-
- */
-
- import wfc.app.*;
- import wfc.core.*;
- import wfc.ui.*;
- import wfc.win32.*;
-
- /**
- * DeskMgr is a Desktop Icon Manager that allows you to change the otherwise
- * unchangeable aspects of the icons on your Win95/NT4 desktop. Knowing that
- * the desktop is, in fact, a Win32 ListView, you can set styles and colors
- * that aren't exposed in the normal desktop properties settings.
- *
- * DeskMgr creates an icon in the system tray using the TrayIcon control class,
- * then allows you to change your desktop icon appearance by selecting items
- * from a popup menu that appears when you right-click the tray icon. DeskMgr
- * also allows you set appearances automatically on startup by specifying
- * keywords on the command line.
- */
- public class DeskMgr extends Form
- {
- /**
- * The main entry point for the application.
- *
- * @param <args> Array of parameters passed to the application
- * via the command line.
- */
- public static void main(String args[])
- {
- DeskMgr dm = new DeskMgr();
- boolean doExit = false;
-
- // parse the command line parameters, and invoke code appropriate
- // to the options indicated by the user.
- for (int i = 0; i < args.length; i++)
- {
- if (args[ i ].equalsIgnoreCase( "transparent" ))
- dm.setDesktopListviewTransparent( true );
- else if (args[ i ].equalsIgnoreCase( "opaque" ))
- dm.setDesktopListviewTransparent( false );
- else if (args[ i ].equalsIgnoreCase( "details" ) ||
- args[ i ].equalsIgnoreCase( "report" ))
- dm.setDesktopListviewStyle( comctl32.LVS_REPORT );
- else if (args[ i ].equalsIgnoreCase( "small" ) ||
- args[ i ].equalsIgnoreCase( "smallicons" ))
- dm.setDesktopListviewStyle( comctl32.LVS_SMALLICON );
- else if (args[ i ].equalsIgnoreCase( "icon" ) ||
- args[ i ].equalsIgnoreCase( "largeicon" ) ||
- args[ i ].equalsIgnoreCase( "large" ))
- dm.setDesktopListviewStyle( comctl32.LVS_ICON );
- else if (args[ i ].equalsIgnoreCase( "exit" ))
- doExit = true;
- }
-
- // if the exit flag was not set...
- if (!doExit)
- {
- // This causes the form to be created, but not shown (it remains
- // hidden). We want this because the main window hosts the context
- // menu and tray icon controls, but is really just the about box
- // and should normally be hidden.
- dm.getHandle();
-
- // run() with no parameters just enters the msg pump without showing
- // a form window.
- Application.run();
- }
- }
-
- /**
- * The simple ctor, calls the WFC initForm() method (required for the designer).
- */
- public DeskMgr()
- {
- initForm();
- }
-
- /**
- * This method checks/unchecks the menu items according to the item given.
- * When one is checked, the others must be unchecked.
- */
- void checkMenuThingies( MenuItem sender )
- {
- showLarge.setChecked( false );
- showSmall.setChecked( false );
- showList.setChecked( false );
- sender.setChecked( true );
- }
-
- /**
- * This function asserts the new listview style on the desktop listview.
- */
- void setDesktopListviewStyle( int style )
- {
- // fetch the listview control on the desktop
- int hlv = getDesktopListview();
-
- // create an array for old/new styles. see WM_STYLECHANGED below...
- int styles[] = { 0, 0 };
-
- // get the current style
- styles[ 0 ] = Windows.GetWindowLong( hlv, Windows.GWL_STYLE );
-
- // clear the style bits we're interested in
- styles[ 1 ] = 0xfffffffc & styles[ 0 ];
- // set the new style
- styles[ 1 ] |= style;
-
- // and set the style with Win32
- Windows.SetWindowLong( hlv, Windows.GWL_STYLE, styles[ 1 ] );
-
- // on Win95, user32 doesn't detect style changes properly, so we
- // have to nudge it a little. that's why we needed an array of old/new.
- Windows.SendMessage( hlv, Windows.WM_STYLECHANGED, Windows.GWL_STYLE, styles );
- }
-
- /**
- * This method sets the background color for the desktop listview.
- */
- void setDesktopListviewTransparent( boolean transparent )
- {
- int hlv;
-
- // fetch the listview control on the desktop
- hlv = getDesktopListview();
-
- // set the text color. Interestingly, if the color is 0xffffffff,
- // the listview interprets that to mean transparent -- so that's
- // what we do if the parameter indicates so. Otherwise, we
- // just use the default desktop color (see Win32 API
- // GetSysColor( COLOR_DESKTOP )).
- Windows.SendMessage( hlv, comctl32.LVM_SETTEXTBKCOLOR, 0,
- transparent ? 0xffffffff : Color.DESKTOP.getRGB()
- );
-
- // force a repaint of the listview
- Windows.InvalidateRect( hlv, null, true );
- }
-
- /**
- * This method traverses up from the desktop and gets the listview handle.
- *
- * TODO: convert this to use fromHandle() and return a Control object.
- */
- int getDesktopListview()
- {
- int hwnd;
-
- // start with the desktop window
- hwnd = Windows.GetDesktopWindow();
-
- // look among it's children for the shell, class "Progman"
- hwnd = com.ms.win32.User32.FindWindowEx( hwnd, 0, "Progman", "Program Manager" );
- // then, look among progman's children for the shell default view
- hwnd = com.ms.win32.User32.FindWindowEx( hwnd, 0, "SHELLDLL_DefView", null );
- // then, get the listview that's in the shell default view
- hwnd = com.ms.win32.User32.FindWindowEx( hwnd, 0, "SysListView32", null );
-
- return hwnd;
- }
-
- /**
- * Handle the Show Transparent menu option by toggling the menu check state
- * and setting the transparency of the desktop listview.
- */
- private void showTransparent_click(Object sender, Event e)
- {
- showTransparent.setChecked( !showTransparent.getChecked() );
- setDesktopListviewTransparent( showTransparent.getChecked() );
- }
-
- /**
- * Handle the Show List menu option by setting detailed view on the listview.
- */
- private void showList_click(Object sender, Event e)
- {
- checkMenuThingies( showList );
- setDesktopListviewStyle( comctl32.LVS_REPORT );
- }
-
- /**
- * Handle the Show Small menu option by setting small icon view on the listview.
- */
- private void showSmall_click(Object sender, Event e)
- {
- checkMenuThingies( showSmall );
- setDesktopListviewStyle( comctl32.LVS_SMALLICON );
- }
-
- /**
- * Handle the Show Large menu option by setting large icon view on the listview.
- */
- private void showLarge_click(Object sender, Event e)
- {
- checkMenuThingies( showLarge );
- setDesktopListviewStyle( comctl32.LVS_ICON );
- }
-
- /**
- * Handle the mouse down event on the icon, to act on double click and do the About.
- */
- private void deskmgrIcon_MouseDown(Object sender, MouseEvent e)
- {
- // handle the Doubleclick, and show the About dialog
- if (e.clicks == 2 && e.button == Control.MB_LEFT)
- aboutDeskMgr_click( sender, Event.EMPTY );
- }
-
- /**
- * Handle the OK button (called for Enter, Cancel, or pressing OK) by just
- * hiding the form.
- */
- private void ok_click(Object sender, Event e)
- {
- // re-enable the icon
- deskmgrIcon.setEnabled( true );
- hide();
- }
-
- /**
- * Handle the form's closing to just hide the form when the user presses
- * the close box, presses Alt-F4, or selects Close from the sys menu.
- */
- private void DeskMgr_closing(Object sender, CancelEvent e)
- {
- // Simulate the OK button
- ok_click( null, null );
-
- // Tell WFC to cancel the event, ignoring the close
- e.cancel = true;
- }
-
- /**
- * Handle the Exit menu item, and shut down the WFC app.
- */
- private void exitDeskMgr_click(Object sender, Event e)
- {
- Application.exit();
- }
-
- /**
- * Handle the About menu item, and just show the main form.
- */
- private void aboutDeskMgr_click(Object sender, Event e)
- {
- // On about, just show the main form (it's an about box).
- show();
-
- // Disable the tray icon to prevent reentrancy.
- deskmgrIcon.setEnabled( false );
- }
-
- /**
- * NOTE: The following code is required by the Visual J++ form
- * designer. It can be modified using the form editor. Do not
- * modify it using the code editor.
- */
- Container components = new Container();
- Button ok = new Button();
- Label lblDescription = new Label();
- TrayIcon deskmgrIcon = new TrayIcon();
- ContextMenu deskmgrMenu = new ContextMenu();
- MenuItem showSmall = new MenuItem();
- MenuItem showLarge = new MenuItem();
- MenuItem showList = new MenuItem();
- MenuItem separator1 = new MenuItem();
- MenuItem showTransparent = new MenuItem();
- MenuItem separator2 = new MenuItem();
- MenuItem aboutDeskMgr = new MenuItem();
- MenuItem exitDeskMgr = new MenuItem();
-
- private void initForm()
- {
- ResourceManager resources = new ResourceManager(this, "DeskMgr");
- ok.setLocation(new Point(270, 90));
- ok.setSize(new Point(70, 20));
- ok.setTabIndex(0);
- ok.setTabStop(true);
- ok.setText("OK");
- ok.addOnClick(new EventHandler(this.ok_click));
- lblDescription.setLocation(new Point(10, 10));
- lblDescription.setSize(new Point(330, 70));
- lblDescription.setText("WFC Desktop Icon Manager");
- lblDescription.setTabIndex(2);
- showSmall.setText("&Small Icons");
- showSmall.addOnClick(new EventHandler(this.showSmall_click));
- showLarge.setText("&Large Icons");
- showLarge.addOnClick(new EventHandler(this.showLarge_click));
- showList.setText("&Details");
- showList.addOnClick(new EventHandler(this.showList_click));
- separator1.setText("-");
- showTransparent.setText("&Transparent Labels");
- showTransparent.addOnClick(new EventHandler(this.showTransparent_click));
- separator2.setText("-");
- aboutDeskMgr.setText("&About DeskMgr...");
- aboutDeskMgr.addOnClick(new EventHandler(this.aboutDeskMgr_click));
- exitDeskMgr.setText("&Exit");
- exitDeskMgr.addOnClick(new EventHandler(this.exitDeskMgr_click));
- deskmgrMenu.setMenuItems(new MenuItem[] {
- showSmall,
- showLarge,
- showList,
- separator1,
- showTransparent,
- separator2,
- aboutDeskMgr,
- exitDeskMgr});
- /* @designTimeOnly deskmgrMenu.setLocation(new Point(110, 90)); */
- this.setBackColor(Color.CONTROL);
- this.setContextMenu(deskmgrMenu);
- this.setLocation(new Point(0, 0));
- this.setSize(new Point(358, 166));
- this.setTabIndex(-1);
- this.setTabStop(true);
- this.setText("About Desktop Icon Manager");
- this.setAcceptButton(ok);
- this.setAutoScaleBaseSize(13);
- this.setBorderStyle(FormBorderStyle.FIXED_3D);
- this.setCancelButton(ok);
- this.setClientSize(new Point(352, 121));
- this.setMaxButton(false);
- this.setMinButton(false);
- this.addOnClosing(new CancelEventHandler(this.DeskMgr_closing));
- deskmgrIcon.setContextMenu(deskmgrMenu);
- deskmgrIcon.setEnabled(true);
- deskmgrIcon.setIcon((Icon)resources.getObject("deskmgrIcon_icon"));
- deskmgrIcon.setText("WFC Desktop Icon Manager");
- deskmgrIcon.setVisible(true);
- deskmgrIcon.addOnMouseDown(new MouseEventHandler(this.deskmgrIcon_MouseDown));
- /* @designTimeOnly deskmgrIcon.addOnLocation(new Point(0, 90)); */
- this.setNewControls(new Control[] {
- lblDescription,
- ok});
- }
- }
-