Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / C#

Window Tabifier

Rate me:
Please Sign up or sign in to vote.
4.91/5 (88 votes)
29 Mar 2008CPOL9 min read 314K   9.9K   294  
A simple application for hosting several Windows in one parent window
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
using FarsiLibrary.Win;

namespace WindowTabifier
{
    public partial class Host : Form
    {
        private List<window> hostedwindows;

        public Host(List<window> windows)
        {
            InitializeComponent();
            hostedwindows = windows;
        }

        private void ProcessWindows(List<window> windows)
        {
            lock (tabs)
            {
                int startindex = tabs.Items.Count - 1;
                for (int i = startindex; i < windows.Count; i++)
                {
                    int count = tabs.Items.Add(new FATabStripItem(windows[i].Title, null));

                    windows[i].SetParent(tabs.Items[count].Handle);
                    windows[i].SetStyle(winapi.GWL_STYLE, (IntPtr)winapi.WS_VISIBLE);
                    windows[i].Move(tabs.Location, tabs.Items[0].Size, true);
                }
            }
        }

        private void Release(window wnd)
        {
            wnd.RestoreParent();
            wnd.SetStyle(winapi.GWL_STYLE, (IntPtr)wnd.PreviousStyle);
            wnd.RestoreLocation();
        }

       
        private void Host_Load(object sender, EventArgs e)
        {
            ProcessWindows(hostedwindows);
        }

        private void Host_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (window wnd in hostedwindows)
            {
                Release(wnd);
            }
        }

        private void Host_Resize(object sender, EventArgs e)
        {
            for (int i = 1; i < tabs.Items.Count; i++)
            {
                hostedwindows[i - 1].Move(tabs.Location, tabs.Items[0].Size, true);
            }

            if (this.WindowState==FormWindowState.Minimized && Properties.Settings.Default.Tray)
            {
                this.Hide();

                hostTrayIcon.Icon = this.Icon;
                hostTrayIcon.Visible = true;
                hostTrayIcon.Text = this.Text;
                hostTrayIcon.ShowBalloonTip(100);
            }
        }


        private void tabs_TabStripItemClosing(TabStripItemClosingEventArgs e)
        {
            if (tabs.SelectedItem==tabs.Items[0])
            {
                e.Cancel = true;
                return;
            }

            ActionForm act = new ActionForm();

            if (act.ShowDialog() == DialogResult.OK)
            {
                int index=tabs.Items.IndexOf(e.Item)-1;

                Release(hostedwindows[index]);

                if (act.CloseWindow)
                {
                    hostedwindows[index].Close();
                }
                hostedwindows.RemoveAt(index);
            }
            else
                e.Cancel = true;
        }

        private void tabs_TabStripItemClosed(object sender, EventArgs e)
        {
            if (tabs.Items.Count == 1)
            {
                this.Close();
            }
        }

        private void tabs_TabStripItemSelectionChanged(TabStripItemChangedEventArgs e)
        {
            if (e.ChangeType != FATabStripItemChangeTypes.Added)
            {
                this.Text = e.Item.Title;

                int index = tabs.Items.IndexOf(e.Item);

                if (e.ChangeType == FATabStripItemChangeTypes.Removed)
                {
                    index -= 1;
                }

                Icon temp = null;

                if (Properties.Settings.Default.WindowIcon)
                {
                    temp = index > 0 ? hostedwindows[index - 1].WindowIcon : null;
                }
                else
                {
                    temp = index > 0 ? hostedwindows[index - 1].ExecutableIcon : null;
                }

                if (index == 0 || temp == null)
                {
                    this.Icon = Properties.Resources.ProgramIcon;
                }
                else
                {
                    this.Icon = temp;
                }
            }
        }

        private void tabs_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyValue>48 && e.KeyValue<58 && tabs.Items.Count>=(e.KeyValue-48))
            {
                tabs.SelectedItem = tabs.Items[e.KeyValue - 49];
            }
        }

        private void tabs_MouseMove(object sender, MouseEventArgs e)
        {
            FATabStripItem c = tabs.GetTabItemByPoint(e.Location);
            if (c != null && Properties.Settings.Default.SelectonHover)
            {
                tabs.SelectedItem = tabs.Items[tabs.Items.IndexOf(c)];
            }
        }

       
        private void dragAndDropFile_FileDropped(object sender, DragAndDropFileControlLibrary.FileDroppedEventArgs e)
        {
            ProcessFiles(e.Filenames);
        }

        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = true;
            ofd.Filter = "All Files|*.txt";

            if (ofd.ShowDialog()==DialogResult.OK)
            {
                ProcessFiles(ofd.FileNames);
            }
        }

        
        private void ProcessFiles(string[] files)
        {
            foreach (string filename in files)
            {
                if (!filename.EndsWith(".lnk"))
                {
                    ParameterizedThreadStart thrparam = new ParameterizedThreadStart(ProcessFile);
                    Thread thr = new Thread(thrparam);
                    thr.Start(filename);
                }
            }

            if (files.Length > 0)
            {
                tm.Enabled = true;
            }
        }

        private void ProcessFile(object filename)
        {
            string path = filename as string;
            if (File.Exists(path))
            {
                Process proc = Process.Start(path);

                if (proc != null)
                {
                    proc.WaitForInputIdle(5000);
                    if (proc.MainWindowHandle != IntPtr.Zero)
                    {
                        lock (hostedwindows)
                        {
                            hostedwindows.Add(new window(proc.MainWindowHandle));
                        }
                    }
                    proc.Dispose();
                }
            }
            else
                if (Directory.Exists(path))
                {
                    int i = 0;
                    Process.Start(path);

                    IntPtr handle = IntPtr.Zero;
                    while (handle==IntPtr.Zero && i<5)
                    {
                        i++;
                        Thread.Sleep(1000);
                        handle = window.FindWindow("CabinetWClass", Path.GetFileName(path));
                    }

                    if (handle != IntPtr.Zero)
                    {
                        lock (hostedwindows)
                        {
                            hostedwindows.Add(new window(handle));
                        }
                    }
                }
        }

       
        private void tm_Tick(object sender, EventArgs e)
        {
            ProcessWindows(hostedwindows);
            tm.Enabled = false;
        }

        private void AddWindowButton_Click(object sender, EventArgs e)
        {
            WindowList wnd = new WindowList();
            if (wnd.ShowDialog() == DialogResult.OK && wnd.SelectedWindows.Count > 0)
            {
                this.hostedwindows.AddRange(wnd.SelectedWindows);
                wnd.Dispose();
                ProcessWindows(hostedwindows);
            }
        }

        
        private void hostTrayIcon_Click(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Maximized;

            hostTrayIcon.Visible = false;
            hostTrayIcon.Icon = null;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Georgia Georgia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions