Back
package J_ActiveDesktop;

import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
import System.Runtime.InteropServices.*;

/**
 * Summary description for Form1.
 */
public class Form1 extends System.Windows.Forms.Form
{
	private System.Windows.Forms.Button button1;
	private System.Windows.Forms.Button button2;
	private System.Windows.Forms.PictureBox pictureBox1;
	private System.Windows.Forms.StatusBar statusBar1;
	private System.Windows.Forms.StatusBarPanel statusBarPanel1;
	/**
	 * Required designer variable.
	 */
	private System.ComponentModel.Container components = null;

	public Form1()
	{
		//
		// Required for Windows Form Designer support
		//
		InitializeComponent();

		//
		// TODO: Add any constructor code after InitializeComponent call
		//
	}

	/**
	 * Clean up any resources being used.
	 */
	protected void Dispose(boolean disposing)
	{
		if (disposing)
		{
			if (components != null)
			{
				components.Dispose();
			}
		}
		super.Dispose(disposing);
	}
#region Windows Form Designer generated code
 
	/**
	 * Required method for Designer support - do not modify
	 * the contents of this method with the code editor.
	 */
	private void InitializeComponent()
	{
		this.button1 = new System.Windows.Forms.Button();
		this.button2 = new System.Windows.Forms.Button();
		this.pictureBox1 = new System.Windows.Forms.PictureBox();
		this.statusBar1 = new System.Windows.Forms.StatusBar();
		this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
		((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
		this.SuspendLayout();
		// 
		// button1
		// 
		this.button1.set_Font(new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((ubyte)(System.Byte)(((ubyte)0)))));
		this.button1.set_Location(new System.Drawing.Point(0, 0));
		this.button1.set_Name("button1");
		this.button1.set_Size(new System.Drawing.Size(256, 23));
		this.button1.set_TabIndex(0);
		this.button1.set_Text("Get Current Wallpaper");
		this.button1.add_Click( new System.EventHandler(this.button1_Click) );
		// 
		// button2
		// 
		this.button2.set_Font(new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((ubyte)(System.Byte)(((ubyte)0)))));
		this.button2.set_Location(new System.Drawing.Point(256, 0));
		this.button2.set_Name("button2");
		this.button2.set_Size(new System.Drawing.Size(256, 23));
		this.button2.set_TabIndex(1);
		this.button2.set_Text("Set New Wallpaper");
		this.button2.add_Click( new System.EventHandler(this.button2_Click) );
		// 
		// pictureBox1
		// 
		this.pictureBox1.set_Location(new System.Drawing.Point(0, 24));
		this.pictureBox1.set_Name("pictureBox1");
		this.pictureBox1.set_Size(new System.Drawing.Size(512, 304));
		this.pictureBox1.set_TabIndex(2);
		this.pictureBox1.set_TabStop(false);
		// 
		// statusBar1
		// 
		this.statusBar1.set_Font(new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((ubyte)(System.Byte)(((ubyte)0)))));
		this.statusBar1.set_Location(new System.Drawing.Point(0, 342));
		this.statusBar1.set_Name("statusBar1");
		this.statusBar1.get_Panels().AddRange(new System.Windows.Forms.StatusBarPanel[] 
			{
				this.statusBarPanel1}
			);
		this.statusBar1.set_ShowPanels(true);
		this.statusBar1.set_Size(new System.Drawing.Size(512, 16));
		this.statusBar1.set_SizingGrip(false);
		this.statusBar1.set_TabIndex(3);
		// 
		// statusBarPanel1
		// 
		this.statusBarPanel1.set_AutoSize(System.Windows.Forms.StatusBarPanelAutoSize.Contents);
		this.statusBarPanel1.set_BorderStyle(System.Windows.Forms.StatusBarPanelBorderStyle.None);
		this.statusBarPanel1.set_Width(10);
		// 
		// Form1
		// 
		this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
		this.set_ClientSize(new System.Drawing.Size(512, 358));
		this.get_Controls().Add(this.statusBar1);
		this.get_Controls().Add(this.pictureBox1);
		this.get_Controls().Add(this.button2);
		this.get_Controls().Add(this.button1);
		this.set_Name("Form1");
		this.set_SizeGripStyle(System.Windows.Forms.SizeGripStyle.Hide);
		this.set_Text("IActiveDesktop");
		this.add_Closing( new System.ComponentModel.CancelEventHandler(this.Form1_Closing) );
		this.add_Load( new System.EventHandler(this.Form1_Load) );
		((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
		this.ResumeLayout(false);

	}
	#endregion
	/**
	 * The main entry point for the application.
	 */
	/** @attribute System.STAThread() */
	public static void main(String[] args) 
	{
		Application.Run(new Form1());
	}
    
	private ActiveDesktop desk;
	private IActiveDesktop idesk;
        private _win32 w32 = new _win32();

	private void Form1_Load (Object sender, System.EventArgs e)
	{
		desk = new ActiveDesktop();
		idesk = (IActiveDesktop)desk;
	}
	private void Form1_Closing (Object sender, System.ComponentModel.CancelEventArgs e)
	{
		if(SUCCEEDED(idesk))
		{
			Marshal.ReleaseComObject(idesk);
		}
	}

	private boolean SUCCEEDED(Object obj)
	{
		boolean isobject = false;
		if(obj != null)
		{
			isobject = true;
		}
		return isobject;
	}

	private boolean CreateThumb()
	{
		return false;
	}
	
	private void button1_Click (Object sender, System.EventArgs e)
	{
		if(SUCCEEDED(idesk))
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder(w32.MAX_PATH);
			idesk.GetWallpaper(sb , sb.get_Capacity(),0);
			StatusBarPanel sbPanel = (StatusBarPanel)statusBar1.get_Panels().get_Item(0);
			sbPanel.set_Text(sb.ToString());
			sbPanel.set_ToolTipText(sb.ToString());
			Bitmap bmp =(Bitmap) Bitmap.FromFile(sb.ToString());
			int w = (pictureBox1.get_Width());
			int h = (pictureBox1.get_Height());
			Image.GetThumbnailImageAbort iThumb = new Image.GetThumbnailImageAbort(CreateThumb);
			Image imgThumb = bmp.GetThumbnailImage(w , h , iThumb , System.IntPtr.Zero);
			pictureBox1.set_Image(imgThumb);
		}
		else
		{
			MessageBox.Show("Failed to hook the IActiveDesktop");
		}
	}

	private void button2_Click (Object sender, System.EventArgs e)
	{
		if(SUCCEEDED(idesk))
		{
			OpenFileDialog od = new OpenFileDialog();
			od.set_InitialDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures));
			od.set_Filter("Jpeg |*.jpg|Bitmap |*.bmp");
			if(od.ShowDialog()== DialogResult.OK)
			{
				idesk.SetWallpaper(od.get_FileName(),0);
				idesk.ApplyChanges(w32.AD_APPLY_FORCE | w32.AD_APPLY_SAVE | w32.AD_APPLY_REFRESH);
			}
		}
	}


}
 #region ActiveDesktop dummy class to expose the IActiveDesktop interface
 
/** @attribute ComImport() */
/** @attribute Guid("75048700-EF1F-11D0-9888-006097DEACF9") */
public class ActiveDesktop
{
	/**
	 * Dummy to enable access to the IActiveDesktop.
	 */
}
#endregion
 #region IActiveDesktop Interface
 
/** @attribute ComImport() */
/** @attribute Guid("F490EB00-1240-11D1-9888-006097DEACF9") */
/** @attribute InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] */
public interface IActiveDesktop
{
	System.IntPtr ApplyChanges( int dwFlags
		); //HRESULT ApplyChanges(DWORD dwFlags);

	System.IntPtr GetWallpaper(System.Text.StringBuilder pwszWallpaper,
			int cchWallpaper,
			int dwReserved
			); // HRESULT GetWallpaper(LPWSTR pwszWallpaper,UINT cchWallpaper,DWORD dwReserved);

	System.IntPtr SetWallpaper(String pwszWallpaper,
			int dwReserved
			); // HRESULT SetWallpaper(LPCWSTR pwszWallpaper,DWORD dwReserved);

	System.IntPtr GetWallpaperOptions(_win32._tagWALLPAPEROPT pwpo,
			int dwReserved
			); // HRESULT GetWallpaperOptions(LPWALLPAPEROPT pwpo,DWORD dwReserved);

	System.IntPtr SetWallpaperOptions(_win32._tagWALLPAPEROPT pwpo,
			int dwReserved
			); // HRESULT SetWallpaperOptions(LPCWALLPAPEROPT pwpo,DWORD dwReserved);

	System.IntPtr GetPattern(System.Text.StringBuilder pwszPattern,
			int cchPattern,
			int dwReserved
			); //HRESULT GetPattern(LPWSTR pwszPattern,UINT cchPattern,DWORD dwReserved);

	System.IntPtr SetPattern(String pwszPattern,
			int dwReserved
			); // HRESULT SetPattern(LPCWSTR pwszPattern,DWORD dwReserved);

	System.IntPtr GetDesktopItemOptions(_win32._tagCOMPONENTSOPT pco,
			int dwReserved
			); // HRESULT GetDesktopItemOptions(LPCOMPONENTSOPT pco,DWORD dwReserved);

	System.IntPtr SetDesktopItemOptions( _win32._tagCOMPONENTSOPT pcomp,
			int dwReserved
			); // HRESULT SetDesktopItemOptions(LPCCOMPONENTSOPT pcomp,DWORD dwReserved);

	System.IntPtr AddDesktopItem( _win32._tagCOMPONENT pcomp,
			int dwReserved
			); // HRESULT AddDesktopItem(LPCOMPONENT pcomp,DWORD dwReserved);

	System.IntPtr AddDesktopItemWithUI( System.IntPtr hwnd,
		 _win32._tagCOMPONENT pcomp,
			int dwFlags
			); // HRESULT AddDesktopItemWithUI(HWND hwnd,LPCOMPONENT pcomp,DWORD dwFlags);

	System.IntPtr ModifyDesktopItem( _win32._tagCOMPONENT pcomp,
			int dwFlags
			); // HRESULT ModifyDesktopItem(LPCCOMPONENT pcomp,DWORD dwFlags);

	System.IntPtr RemoveDesktopItem( _win32._tagCOMPONENT pcomp,
			int dwReserved
			); // HRESULT RemoveDesktopItem(LPCCOMPONENT pcomp,DWORD dwReserved);

	System.IntPtr GetDesktopItemCount(int lpiCount,
			int dwReserved
			); // HRESULT GetDesktopItemCount(LPINT lpiCount,DWORD dwReserved);

	System.IntPtr GetDesktopItem( int nComponent,
		_win32._tagCOMPONENT pcomp,
			int dwReserved
			); // HRESULT GetDesktopItem(int nComponent,LPCOMPONENT pcomp,DWORD dwReserved);

	System.IntPtr GetDesktopItemByID( System.IntPtr dwID,
		_win32._tagCOMPONENT pcomp,
			int dwReserved
			); // HRESULT GetDesktopItemByID(DWORD dwID,LPCOMPONENT pcomp,DWORD dwReserved);

	System.IntPtr GenerateDesktopItemHtml(String pwszFileName,
			_win32._tagCOMPPOS pcomp,
			int dwReserved
			); // HRESULT GenerateDesktopItemHtml(LPCWSTR pwszFileName,LPCOMPONENT pcomp,DWORD dwReserved);

	System.IntPtr AddUrl( System.IntPtr hwnd,
	String pszSource,
		    _win32._tagCOMPONENT pcomp,
			int dwFlags
			); // HRESULT AddUrl(HWND hwnd,LPCWSTR pszSource,LPCOMPONENT pcomp,DWORD dwFlags);

	System.IntPtr GetDesktopItemBySource(String pszSource,
			_win32._tagCOMPONENT pcomp,
	        int dwReserved
		    ); // HRESULT GetDesktopItemBySource(LPCWSTR pszSource,LPCOMPONENT pcomp,DWORD dwReserved);
}
#endregion
 #region win32 constants / structures
 
public class _win32
{
	public final int AD_APPLY_BUFFERED_REFRESH = 0x10;
	public final int AD_APPLY_DYNAMICREFRESH = 0x20;
	public final int AD_APPLY_FORCE = 0x8;
	public final int AD_APPLY_HTMLGEN = 0x2;
	public final int AD_APPLY_REFRESH = 0x4;
	public final int AD_APPLY_SAVE = 0x1;
	public final int AD_APPLY_ALL = AD_APPLY_SAVE | AD_APPLY_HTMLGEN | AD_APPLY_REFRESH;
	public final int COMP_ELEM_CHECKED = 0x2;
	public final int COMP_ELEM_CURITEMSTATE = 0x4000;
	public final int COMP_ELEM_DIRTY = 0x4;
	public final int COMP_ELEM_FRIENDLYNAME = 0x400;
	public final int COMP_ELEM_NOSCROLL = 0x8;
	public final int COMP_ELEM_ORIGINAL_CSI = 0x1000;
	public final int COMP_ELEM_POS_LEFT = 0x10;
	public final int COMP_ELEM_POS_TOP = 0x20;
	public final int COMP_ELEM_POS_ZINDEX = 0x100;
	public final int COMP_ELEM_RESTORED_CSI = 0x2000;
	public final int COMP_ELEM_SIZE_HEIGHT = 0x80;
	public final int COMP_ELEM_SIZE_WIDTH = 0x40;
	public final int COMP_ELEM_SOURCE = 0x200;
	public final int COMP_ELEM_TYPE = 0x1;
	public final int COMP_ELEM_SUBSCRIBEDURL = 0x800;
	public final int COMP_TYPE_CONTROL = 3;
	public final int COMP_TYPE_HTMLDOC = 0;
	public final int COMP_TYPE_PICTURE = 1;
	public final int COMP_TYPE_WEBSITE = 2;
	public final int COMPONENT_DEFAULT_LEFT = (0xFFFF);
	public final int COMPONENT_DEFAULT_TOP = (0xFFFF);
	public final int COMP_ELEM_ALL = COMP_ELEM_TYPE | COMP_ELEM_CHECKED | COMP_ELEM_DIRTY | COMP_ELEM_NOSCROLL | COMP_ELEM_POS_LEFT | COMP_ELEM_SIZE_WIDTH | COMP_ELEM_SIZE_HEIGHT | COMP_ELEM_POS_ZINDEX | COMP_ELEM_SOURCE | COMP_ELEM_FRIENDLYNAME | COMP_ELEM_POS_TOP | COMP_ELEM_SUBSCRIBEDURL | COMP_ELEM_ORIGINAL_CSI | COMP_ELEM_RESTORED_CSI | COMP_ELEM_CURITEMSTATE;
	public final int IS_FULLSCREEN = 0x2;
	public final int IS_NORMAL = 0x1;
	public final int IS_SPLIT = 0x4;
	public final int MAX_PATH = 260;
	public final int WPSTYLE_CENTER = 0;
	public final int WPSTYLE_MAX = 3;
	public final int WPSTYLE_STRETCH = 2;
	public final int WPSTYLE_TILE = 1;

	public class _tagCOMPSTATEINFO 
	{
		public int dwSize;
		public int iLeft;
		public int iTop;
		public int dwWidth;
		public int dwHeight;
		public int dwItemState;
	}

	public class _tagCOMPPOS 
	{
		public int dwSize;
		public int iLeft;
		public int iTop;
		public int dwWidth;
		public int dwHeight;
		public int izIndex;
		public boolean fCanResize;
		public boolean fCanResizeX;
		public boolean fCanResizeY;
		public int iPreferredLeftPercent;
		public int iPreferredTopPercent;
	}

	public class _tagCOMPONENT 
	{
		public int dwSize;
		public int dwID;
		public int iComponentType;
		public boolean fChecked;
		public boolean fDirty;
		public boolean fNoScroll;
		public _tagCOMPPOS cpPos;
		public String wszFriendlyName;
		public String wszSource;
		public String wszSubscribedURL;
		public int dwCurItemState;
		public _tagCOMPSTATEINFO csiOriginal;
		public _tagCOMPSTATEINFO csiRestored;
	}

	public class _tagCOMPONENTSOPT 
	{
		public int dwSize;
		public boolean fEnableComponents;
		public boolean fActiveDesktop;
	}

	public class _tagWALLPAPEROPT 
	{
		public int dwSize;
		public int dwStyle;
	}
}
#endregion