Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Applications » Downloads
 
Add your own
alternative version

Window Tabifier

, 29 Mar 2008 CPOL
A simple application for hosting several Windows in one parent window
windowtabifier_demo.zip
WindowTabifier_demo
About.dll
DragAndDropFileComponent.dll
TabStrip.dll
Window Tabifier.exe
windowtabifier_src.zip
Window Tabifier
Window Tabifier
bin
DLL's
About.dll
DragAndDropFileComponent.dll
TabStrip.dll
documents-32x32.png
folder-documents.ico
Properties
Settings.settings
Thumbs.db
Window Tabifier.csproj.user
Window.cd
Windows-Desktop-32x32.png
windowtabifier_srcvs2005.zip
WindowTabifier_srcVS2005
Window Tabifier
bin
DLLs
About.dll
DragAndDropFileComponent.dll
TabStrip.dll
documents-32x32.png
Properties
Settings.settings
Resources
folder-documents.ico
Thumbs.db
Thumbs.db
Window Tabifier.csproj.user
window.cd
Windows-Desktop-32x32.png
windowtabifier_srcvs2008.zip
WindowTabifier_srcVS2008
Window Tabifier
bin
DLLs
About.dll
DragAndDropFileComponent.dll
TabStrip.dll
documents-32x32.png
folder-documents.ico
Properties
Settings.settings
Thumbs.db
Window Tabifier.csproj.user
window.cd
Windows-Desktop-32x32.png
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)

Share

About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided
Follow on   Google+

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 29 Mar 2008
Article Copyright 2008 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid