Click here to Skip to main content
15,896,278 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 316.6K   9.9K   294  
A simple application for hosting several Windows in one parent window
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace WindowTabifier
{
    public class window
    {
        #region private fields
        private IntPtr handle;
        private string title;
        private int style;
        private int previousstyle;
        private IntPtr parent;
        private IntPtr previousparent;
        private winapi.RECT rect;
        private Size size;
        private Point location;

        private static List<window> openwnd;
        #endregion

        #region Properties

        public IntPtr Handle
        {
            get { return handle; }
        }

        public string Title
        {
            get { return title; }
        }

        public int Style
        {
            get { return style; }
        }

        public IntPtr Parent
        {
            get { return parent; }
        }

        public Size Size
        {
            get { return size; }
        }

        public Point Location
        {
            get { return location; }
        }

        public int PreviousStyle
        {
            get { return previousstyle; }
        }

        #endregion

        public window(IntPtr handle)
        {
            this.handle = handle;

            title = gettext();
            parent = getparent();
            style = getstyle();
            winapi.GetWindowRect(handle, out rect);
            size = new Size(rect.Right - rect.Left, rect.Bottom - rect.Top);
            location = new Point(rect.Left, rect.Top);
        }

        public void SetParent(IntPtr ParentHandle)
        {
            previousparent = winapi.SetParent(handle, ParentHandle);
            parent = ParentHandle;
        }

        public void RestoreParent()
        {
            parent = previousparent;
            previousparent = winapi.SetParent(handle, previousparent);
        }

        public void Move(Point Location,Size size, bool repaint)
        {
            winapi.MoveWindow(handle, Location.X, Location.Y, size.Width, size.Height, repaint);
        }

        public void SetStyle(int index, IntPtr value)
        {
            previousstyle = winapi.SetWindowLong(handle, index, value);
            style = value.ToInt32();
        }

        public bool Close()
        {
            bool result = winapi.PostMessage(handle, winapi.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
            
            if (!result)
            {
                result = winapi.PostMessage(handle, winapi.WM_DESTROY, IntPtr.Zero, IntPtr.Zero);
            }

            return result;
        }

        public string GetExecutablePath()
        {
            uint dwProcessId;
            winapi.GetWindowThreadProcessId(handle, out dwProcessId);
            IntPtr hProcess = winapi.OpenProcess(winapi.ProcessAccessFlags.VMRead | winapi.ProcessAccessFlags.QueryInformation, false, dwProcessId);
            StringBuilder path = new StringBuilder(1024);
            winapi.GetModuleFileNameEx(hProcess, IntPtr.Zero, path, 1024);
            winapi.CloseHandle(hProcess);
            return path.ToString();
        }

        public static List<window> GetOpenWindows()
        {
            openwnd = new List<window>();

            winapi.EnumWindowsProc callback = new winapi.EnumWindowsProc(EnumWindows);
            winapi.EnumWindows(callback, 0);

            List<window> result = new List<window>(openwnd);
            openwnd.Clear();

            result.RemoveAt(result.Count - 1);
            return result;
        }

        private static bool EnumWindows(IntPtr hWnd, int lParam)
        {
            if (!winapi.IsWindowVisible(hWnd) || hWnd == winapi.statusbar)
                return true;

            openwnd.Add(new window(hWnd));
            
            return true;
        }

        private string gettext()
        {
            StringBuilder title = new StringBuilder(256);
            winapi.GetWindowText(handle, title, 256);

            return title.ToString();
        }

        private IntPtr getparent()
        {
            return winapi.GetParent(handle);
        }

        private int getstyle()
        {
            return winapi.GetWindowLong(handle, winapi.GWL_STYLE);
        }
    }
}

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