Saturday, July 24, 2010

Fun App: Take a Screenshot of the Desktop as Wallpaper and Delete all Items

First of all: Attention! With the sample code shown here data might get lost, definitely the ordering of the desktop icons will get lost - I do not take any responsibility for any negative aftermaths.

And now what this is all about: Today I want to present to you a little fun application using a lot of the previous posts, which makes it pretty easy to fool your friends with.
The program takes a screenshot of the desktop, then hides the taskbar and removes all icons from the desktop. Then the program sets this screenshot as the wallpaper, so it looks as everything was as normal, but nothing can be clicked anymore.
The files and symbols from the desktop are not deleted but moved in a backup directory. All this can be reversed with the program from the next post.

All tricks I have used in this application I described in previous post, so I hope you excuse that the source code is not that well documented. You can just copy this code in a Windows Forms - Application overwriting the code of the whole form. On the form a timer has to be placed:

    public partial class Form1 : Form
    {

        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);

        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);


        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern Int32 SystemParametersInfo(UInt32 uiAction, UInt32 uiParam, String pvParam, UInt32 fWinIni);
        private static UInt32 SPI_SETDESKWALLPAPER = 20;
        private static UInt32 SPIF_UPDATEINIFILE = 0x1;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            // minimize all windows
            Type typeShell = Type.GetTypeFromProgID("Shell.Application");
            object objShell = Activator.CreateInstance(typeShell);
            typeShell.InvokeMember("MinimizeAll", System.Reflection.BindingFlags.InvokeMethod, null, objShell, null);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int StartX, StartY;
            int Width, Height;

            StartX = 0;
            StartY = 0;
            Width = Screen.PrimaryScreen.Bounds.Width;
            Height = Screen.PrimaryScreen.Bounds.Height;

            Bitmap Screenshot = new Bitmap(Width, Height);

            // wait for all windows to be minimized
            for (int i = 0; i < 1000; i++)
                if (i % 100 == 0)
                    System.Threading.Thread.Sleep(100);

            // take a screenshot from the desktop
            Graphics G = Graphics.FromImage(Screenshot);

            G.CopyFromScreen(StartX, StartY, 0, 0, new Size(Width, Height), CopyPixelOperation.SourceCopy);

            Screenshot.Save("C:\\1.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
            Screenshot.Dispose();

            G.Dispose();

            string filename = "C:\\1.bmp";

            // hide taskbar
            int hwnd = FindWindow("Shell_TrayWnd""");
            ShowWindow(hwnd, 0);

            SystemParametersInfo(20, 0, filename, 0x01 | 0x02);
         
            DirectoryInfo ds = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

            // backup all files and directories
            string BackupDirectory = @"C:\backupdesktopfun\";

            if (!Directory.Exists(BackupDirectory))
                Directory.CreateDirectory(BackupDirectory);

            foreach (FileInfo f in ds.GetFiles())
            {
                if (!File.Exists(BackupDirectory + f.Name))
                    f.MoveTo(BackupDirectory + f.Name);
            }

            foreach (DirectoryInfo d in ds.GetDirectories())
            {
                if (!Directory.Exists(BackupDirectory + d.Name))
                    d.MoveTo(BackupDirectory + d.Name);
            }

            Application.Exit();
        }
    }

No comments:

Post a Comment