历史评论归档

使用问题 · 286 次浏览
197906+abcd张三 创建于 2020-05-10 23:11
~历史讨论归档。~

回复内容
zdf153 2020-05-18 11:44
#1
采纳率:42%11级 2013.05.24
百度找的不知道行不行 看不懂
获得窗体句柄可以用GetWindow方法获得窗体标题用GetWindowText方法,显示隐藏某个窗体用ShowWindow方法,给你举个例子

using System.Runtime.InteropServices;

private const int WS_VISIBLE = 268435456;//窗体可见
private const int WS_MINIMIZEBOX = 131072;//有最小化按钮
private const int WS_MAXIMIZEBOX = 65536;//有最大化按钮
private const int WS_BORDER = 8388608;//窗体有边框
private const int GWL_STYLE = (-16);//窗体样式
private const int GW_HWNDFIRST = 0;
private const int GW_HWNDNEXT = 2;
private const int SW_HIDE = 0;
private const int SW_SHOW = 5;

[DllImport("User32.dll")]
private extern static int GetWindow(int hWnd, int wCmd);
[DllImport("User32.dll")]
private extern static int GetWindowLongA(int hWnd, int wIndx);
[DllImport("user32.dll")]
private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private extern static int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ShowWindow(int hwnd, int nCmdShow);

//获得包含窗体可见、有边框、有最大化按钮的窗体的句柄和标题(窗体的属性出这几种外还有很多种)
public static List<FromInfo> GetHandleList(int Handle)
{
List<FromInfo> fromInfo = new List<FromInfo>();
int handle = GetWindow(Handle, GW_HWNDFIRST);
while (handle > 0)
{
int IsTask = WS_VISIBLE | WS_BORDER | WS_MAXIMIZEBOX;//窗体可见、有边框、有最大化按钮
int lngStyle = GetWindowLongA(handle, GWL_STYLE);
bool TaskWindow = ((lngStyle & IsTask) == IsTask);
if (TaskWindow)
{
int length = GetWindowTextLength(new IntPtr(handle));
StringBuilder stringBuilder = new StringBuilder(2 * length + 1);
GetWindowText(handle, stringBuilder, stringBuilder.Capacity);
string strTitle = stringBuilder.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
fromInfo.Add(new FromInfo(strTitle, handle));
}
else
{
fromInfo.Add(new FromInfo("", handle));
}
}
handle = GetWindow(handle, GW_HWNDNEXT);
}
return fromInfo;
}
//获得所有窗体的句柄和标题
public static List<FromInfo> GetHandleList(int Handle)
{
List<FromInfo> fromInfo = new List<FromInfo>();
int handle = GetWindow(Handle, GW_HWNDFIRST);
while (handle > 0)
{
int length = GetWindowTextLength(new IntPtr(handle));
StringBuilder stringBuilder = new StringBuilder(2 * length + 1);
GetWindowText(handle, stringBuilder, stringBuilder.Capacity);
string strTitle = stringBuilder.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
fromInfo.Add(new FromInfo(strTitle, handle));
}
else
{
fromInfo.Add(new FromInfo("", handle));
}
handle = GetWindow(handle, GW_HWNDNEXT);
}
return fromInfo;
}

public class FromInfo
{
public FromInfo(string title, int handle)
{
this.title = title;
this.handle = handle;
}
private string title;
private int handle;

public string Title
{
get { return title; }
set { title = value; }
}
public int Handle
{
get { return handle; }
set { handle = value; }
}
}

//获得窗体句柄和标题
private void button1_Click(object sender, EventArgs e)
{
List<FromInfo> fromInfo = GetHandleList(this.Handle.ToInt32());
}
private void button2_Click(object sender, EventArgs e)
{
//隐藏窗体
ShowWindow(this.Handle.ToInt32(), SW_HIDE);
//显示窗体
ShowWindow(this.Handle.ToInt32(), SW_SHOW);
}
K_Y_D 回复 zdf153 2020-05-18 12:20 :

大概看了一下,应该也不能用于多标签的窗口

zdf153 回复 K_Y_D 2020-05-18 12:31 :

好吧

zdf153 2020-05-18 10:17
#2

发现同一个应用有多个窗口的画只能扫描到上层的,比360极速浏览器,win10看图

K_Y_D 回复 zdf153 2020-05-18 11:04 :

确实是这样,因为只是查找了主窗口(主窗口找起来快)。至于查找所有窗口的,我不大会写(因为C#语言我并不熟悉)。你可以去讨论区问问有谁会写读取所有窗口标题和句柄的子程序。

zdf153 回复 K_Y_D 2020-05-18 11:29 :

好吧  谢谢

zdf153 2020-05-16 21:38
#3

ok  谢谢啦

zdf153 2020-05-16 21:26
#4


K_Y_D 回复 zdf153 2020-05-16 21:34 :

emmm,大概你同时运行的进程太多了,你这1.几秒,不慢了

zdf153 回复 K_Y_D 2020-05-16 21:36 :

好吧  谢谢

zdf153 2020-05-16 21:25
#5

不行呀 发现还是一样慢  这个动作 https://getquicker.net/sharedaction?code=94419921-8729-4f28-c4d0-08d7f7cf8b53

K_Y_D 回复 zdf153 2020-05-16 21:32 :

呃。。。你这个我试了一下,第一次运行慢,后面的运行很快(<1秒)啊

zdf153 回复 K_Y_D 2020-05-16 21:33 :

你开启多窗口没,我这边测试就是要2 3秒才可以弹出来

K_Y_D 回复 zdf153 2020-05-16 21:37 :

开了,并不慢,你试试关闭大部分程序试试,可能这些程序占用的系统资源太多了

zdf153 2020-05-16 20:36
#6

速度有些慢不知道能不能解决

K_Y_D 回复 zdf153 2020-05-16 21:06 :

这个暂时没办法解决,目前是第一次运行(自开机以来)时速度最慢(2~4秒,时间花在程序向计算机请求数据,构建“通讯线路”),之后再次运行就快了(<1秒,“通讯线路”缓存在内存上了)

zdf153 回复 K_Y_D 2020-05-16 21:13 :

请问一下第二次运行是用重复 命令挂后台,还是要怎么样

K_Y_D 回复 zdf153 2020-05-16 21:21 :

不用挂后台,就是第二次运行,比如说这样的一个动作,动作第一次运行慢,但再点击动作运行,就快了。这就相当于第一次运行给以后的运行开了一条通往计算机窗口信息的路(缓存在内存中)。开路当然就慢了,而有路就快。


回复主贴