WindowsManager 程序(二)

1.遍历桌面所有可见窗体.

该功能可以通过 Windows Api来实现.

首先: 定义Api EnumWindows ,EnumWindows 就是用来遍历所有窗口,并通过回调函数返回,如下:

private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);

然后就是调用了.代码如下:

public void GetProcessInfo()
{
try
{
this.UpdateList(3, "", "", "",false);
EnumWindows(new WNDENUMPROC(GerProcessInfo_back), 0);
this.ShowStatus("获取进程信息结束");
}
catch (System.Exception exp)
{
this.ShowStatus("获取进程信息错误:" + exp.Message);
}
finally
{
ShowButtonStatus(1, true);

}
}

public bool GerProcessInfo_back(IntPtr handle, int lpararm)
{
if (handle.ToInt32() == this.meHandle)
{
return true;
}

if (APIs.GetWindow(handle, APIs.GetWindow_Cmd.GW_OWNER) == (IntPtr)0 && APIs.IsWindowVisible(handle) && CheckStyle(handle))
{
StringBuilder sb = new StringBuilder(256);
APIs.GetWindowTextW(handle, sb, sb.Capacity);
string title = sb.ToString();
if (title != "")
{
this.UpdateList(1, title, "", handle.ToInt32().ToString(),false);
}
}
return true;

}

在回调的时候, 还需要判断或取得Handle 是不是子窗体, 并且判断是不是可见状态 , 如果不是子窗体并且可见,这显示到列表中。

判断是不是子窗体可以使用GetWindow 的Api,如果是Get GW_OWNER,是0,表示已经是主窗体了。

判断是否可见,可以使用IsWindowVisble的Api

CheckStyle 是一个方法,该方法用来判断该窗体是不是一个popup 。

public bool CheckStyle(IntPtr handle)
{

int Style = APIs.GetWindowLong(handle,-10);
Console.WriteLine(Style.ToString());
if ((Style & APIs.WindowStyles.WS_POPUP) == APIs.WindowStyles.WS_POPUP)
{

if (((Style & APIs.WindowStyles.WS_GROUP) == APIs.WindowStyles.WS_GROUP) && (Style & APIs.WindowStyles.WS_DISABLED)!= APIs.WindowStyles.WS_DISABLED && (Style & APIs.WindowStyles.WS_SYSMENU)== APIs.WindowStyles.WS_SYSMENU)
{
return true;
}
else
{
return false;
}
}
else
{
return true;
}

}
通过如上的方法便可遍历出所有的Desktop上所有可见的窗体了,并将各个窗体的Handle保存在List中。



2. 窗体置顶,取消置顶

该方法也是使用Api的方式显示,由于通过上面的方法已经将各个窗体的Handle保存起来了。

所以这里可以使用SetWindowPos 的Api来实现该功能。

该Api 的定义如下:

[DllImportAttribute("user32.dll")]
public static extern bool SetWindowPos(IntPtr hwnd, int hwnd2, int x, int y, int cx, int cy, int uFlags);

if(隐藏)
{
IntPtr iprt = (IntPtr)int.Parse(item.SubItems[1].Text);
APIs.SetWindowPos(iprt,-1,0,0,0,0,(int)(APIs.SWP.SWP_NOACTIVATE|APIs.SWP.SWP_SHOWWINDOW|APIs.SWP.SWP_NOMOVE|APIs.SWP.SWP_NOSIZE));
}
else
{
IntPtr iprt = (IntPtr)int.Parse(item.SubItems[1].Text);
APIs.SetWindowPos(iprt, -2, 0, 0, 0, 0, (int)(APIs.SWP.SWP_NOACTIVATE | APIs.SWP.SWP_SHOWWINDOW | APIs.SWP.SWP_NOMOVE | APIs.SWP.SWP_NOSIZE));

}

SWP的定义如下:


public enum SWP:int
{
SWP_NOSIZE = 0x0001,
SWP_NOMOVE = 0x0002,
SWP_NOZORDER = 0x0004,
SWP_NOREDRAW = 0x0008,
SWP_NOACTIVATE = 0x0010,
SWP_FRAMECHANGED = 0x0020,
SWP_SHOWWINDOW = 0x0040,
SWP_HIDEWINDOW = 0x0080,
SWP_NOCOPYBITS = 0x0100,
SWP_NOOWNERZORDER = 0x0200,
SWP_NOSENDCHANGING = 0x0400,
SWP_DEFERERASE = 0x2000,
SWP_ASYNCWINDOWPOS = 0x4000
}

3. 对窗体的显示和隐藏

该功能使用Api 的ShowWindow 的方法,该方法定义如下:

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, ShowWindowStyles State);
#region ShowWindow Styles
public enum ShowWindowStyles : short
{
HIDE = 0,
SHOWNORMAL = 1,
NORMAL = 1,
SHOWMINIMIZED = 2,
SHOWMAXIMIZED = 3,
MAXIMIZE = 3,
SHOWNOACTIVATE = 4,
SHOW = 5,
MINIMIZE = 6,
SHOWMINNOACTIVE = 7,
SHOWNA = 8,
RESTORE = 9,
SHOWDEFAULT = 10,
FORCEMINIMIZE = 11,
MAX = 11
}
#endregion

调用方法:

APIs.ShowWindow(iprt, APIs.ShowWindowStyles.HIDE);

APIs.ShowWindow(iprt, APIs.ShowWindowStyles.SHOW);

iprt 对应窗体的Handle 。

4 . 通过窗体的Handle 获取对应的Process,并关闭该Process。

该方法如下:

IntPtr iprt = (IntPtr)int.Parse(item.SubItems[1].Text);
uint pid = 0;

//获取对应的ProcessID
APIs.GetWindowThreadProcessId(iprt, out pid);
if (pid != 0)
{
try
{

// 获取对应的Process
Process p = Process.GetProcessById((int)pid);

//杀掉该进程

p.Kill();
}
catch { }
}


本次先写这些,下次再介绍如何设定全局的鼠标挂钩及全局的快捷键的定义。

原文地址:https://www.cnblogs.com/zhucl1006/p/903905.html