大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> Window Hiding with C#(ZT)

Window Hiding with C#(ZT)

时间: 2021-07-31 作者:daque

introduction

ever wanted to completely hide all the crap on your machine that you're not supposed to be looking at when your boss walks by? try this handy window hider utility and all you will have to do is press a customizable hotkey.this source project will demonstrate the implementation of hot keys, enumerable collections, enums, binary serialization, dllimports of win32 api, window enumeration, callbacks/delegates, custom events and event handlers, and more. it's quite a bit of code to look through but some of you may find it interesting. the guts of the application is based in the window and windows classes that enumerate and hide the open windows your choose: 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

window hiding with c#(zt)图1

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

}

}

}

}

 

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.