分享
 
 
 

在C#中使用热键隐含窗口

王朝c#·作者佚名  2008-05-30
窄屏简体版  字體: |||超大  

尝试便捷的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 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];

}

}

}

}

http://www.cnblogs.com/tanghuawei/archive/2006/10/11/526387.html

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有