欢迎光临
我们一直在努力

在C#中使用热键隐含窗口-.NET教程,C#语言

建站超值云服务器,限时71元/月

我们曾经想过能够在我们的计算机上将窗口隐蔽起来,不想被从身边走过的老板看见。尝试便捷的windows隐藏并定义热键来控制它们。下面我们将演示如何通过热键,我们将会用到dllimports of win32 api、callbacks/delegates,定制事件与事件的句柄。

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 objects 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 objects 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

//objects 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];

}

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在C#中使用热键隐含窗口-.NET教程,C#语言
分享到: 更多 (0)