欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#使用热键实现程序窗口隐藏示例

程序员文章站 2024-02-25 22:09:33
复制代码 代码如下:using system; using system.text; using system.collections; using system.runt...

复制代码 代码如下:

using system;
using system.text;
using system.collections;
using system.runtime.interopservices;

namespace windowhider
{
    /// <summary>
    /// object used to control a windows form.
    /// </summary>
    public class window
    {
        /// <summary>
        /// win32 api imports
        /// </summary>
        [dllimport("user32.dll")] private static extern 
            bool showwindowasync(intptr hwnd, int ncmdshow);
        [dllimport("user32.dll")] private static extern 
            bool setforegroundwindow(intptr hwnd);
        [dllimport("user32.dll")] private static extern 
            bool isiconic(intptr hwnd);
        [dllimport("user32.dll")] private static extern 
            bool iszoomed(intptr hwnd);
        [dllimport("user32.dll")] private static extern 
            intptr getforegroundwindow();
        [dllimport("user32.dll")] private static extern 
            intptr getwindowthreadprocessid(intptr hwnd, intptr processid);
        [dllimport("user32.dll")] private static extern 
            intptr attachthreadinput(intptr idattach, intptr idattachto, int fattach);

        /// <summary>
        /// win32 api constants for showwindowasync()
        /// </summary>
        private const int sw_hide = 0;
        private const int sw_shownormal = 1;
        private const int sw_showminimized = 2;
        private const int sw_showmaximized = 3;
        private const int sw_shownoactivate = 4;
        private const int sw_restore = 9;
        private const int sw_showdefault = 10;

        /// <summary>
        /// private fields
        /// </summary>
        private intptr m_hwnd;
        private string m_title;
        private bool m_visible = true;
        private string m_process;
        private bool m_wasmax = false;

        /// <summary>
        /// window object's public properties
        /// </summary>
        public intptr hwnd
        {
            get{return m_hwnd;}
        }
        public string title
        {
            get{return m_title;}
        }
        public string process
        {
            get{return m_process;}
        }

        /// <summary>
        /// sets this window object's visibility
        /// </summary>
        public bool visible
        {
            get{return m_visible;}
            set
            {
                //show the window
                if(value == true)
                {
                    if(m_wasmax)
                    {
                        if(showwindowasync(m_hwnd,sw_showmaximized))
                            m_visible = true;
                    }
                    else
                    {
                        if(showwindowasync(m_hwnd,sw_shownormal))
                            m_visible = true;
                    }
                }
                //hide the window
                if(value == false)
                {
                    m_wasmax = iszoomed(m_hwnd);
                    if(showwindowasync(m_hwnd,sw_hide))
                        m_visible = false;
                }
            }
        }

        /// <summary>
        /// constructs a window object
        /// </summary>
        /// <param name="title">title caption</param>
        /// <param name="hwnd">handle</param>
        /// <param name="process">owning process</param>
        public window(string title, intptr hwnd, string process)
        {
            m_title = title;
            m_hwnd = hwnd;
            m_process = process;
        }

        //override tostring() 
        public override string tostring()
        {
            //return the title if it has one, if not return the process name
            if (m_title.length > 0)
            {
                return m_title;
            }
            else
            {
                return m_process;
            }
        }

        /// <summary>
        /// sets focus to this window object
        /// </summary>
        public void activate()
        {
            if(m_hwnd == getforegroundwindow())
                return;

            intptr threadid1 = getwindowthreadprocessid(getforegroundwindow(),
                                                        intptr.zero);
            intptr threadid2 = getwindowthreadprocessid(m_hwnd,intptr.zero);

            if (threadid1 != threadid2)
            {
                attachthreadinput(threadid1,threadid2,1);
                setforegroundwindow(m_hwnd);
                attachthreadinput(threadid1,threadid2,0);
            }
            else
            {
                setforegroundwindow(m_hwnd);
            }

            if (isiconic(m_hwnd))
            {
                showwindowasync(m_hwnd,sw_restore);
            }
            else
            {
                showwindowasync(m_hwnd,sw_shownormal);
            }
        }
    }

    /// <summary>
    /// collection used to enumerate window objects
    /// </summary>
    public class windows : ienumerable, ienumerator
    {
        /// <summary>
        /// win32 api imports
        /// </summary>
        [dllimport("user32.dll")] private static extern 
              int getwindowtext(int hwnd, stringbuilder title, int size);
        [dllimport("user32.dll")] private static extern 
              int getwindowmodulefilename(int hwnd, stringbuilder title, int size);
        [dllimport("user32.dll")] private static extern 
              int enumwindows(enumwindowsproc ewp, int lparam); 
        [dllimport("user32.dll")] private static extern 
              bool iswindowvisible(int hwnd);

        //delegate used for enumwindows() callback function
        public delegate bool enumwindowsproc(int hwnd, int lparam);

        private int m_position = -1; // holds current index of wndarray, 
                                     // necessary for ienumerable

        arraylist wndarray = new arraylist(); //array of windows

        //object's private fields
        private bool m_invisible = false;
        private bool m_notitle = false;

        /// <summary>
        /// collection constructor with additional options
        /// </summary>
        /// <param name="invisible">include invisible windows</param>
        /// <param name="untitled">include untitled windows</param>
        public windows(bool invisible, bool untitled)
        {
            m_invisible = invisible;
            m_notitle = untitled;

            //declare a callback delegate for enumwindows() api call
            enumwindowsproc ewp = new enumwindowsproc(evalwindow);
            //enumerate all windows
            enumwindows(ewp, 0);
        }
        /// <summary>
        /// collection constructor
        /// </summary>
        public windows()
        {
            //declare a callback delegate for enumwindows() api call
            enumwindowsproc ewp = new enumwindowsproc(evalwindow);
            //enumerate all windows
            enumwindows(ewp, 0);
        }
        //enumwindows callback function
        private bool evalwindow(int hwnd, int lparam)
        {
            if (m_invisible == false && !iswindowvisible(hwnd))
                return(true);

            stringbuilder title = new stringbuilder(256);
            stringbuilder module = new stringbuilder(256);

            getwindowmodulefilename(hwnd, module, 256);
            getwindowtext(hwnd, title, 256);

            if (m_notitle == false && title.length == 0)
                return(true);

            wndarray.add(new window(title.tostring(), (intptr)hwnd, 
                                    module.tostring()));

            return(true);
        }

        //implement ienumerable
        public ienumerator getenumerator()
        {
            return (ienumerator)this;
        }
        //implement ienumerator
        public bool movenext()
        {
            m_position++;
            if (m_position < wndarray.count)
{
return true;
}
else
{
return false;
}
}
public void reset()
{
m_position = -1;
}
public object current
{
get
{
return wndarray[m_position];
}
}
}
}