C#如何调用API查找窗体并关闭找到的窗体 如题,只知道这个窗体是其他软件系统抛出的错误的窗体,现在我的工具要实现找到该窗体后关闭它窗体上显示的名字比如是HuangHePower第一次接触调用API函数的东西,大家给个例子学习下主要是如何找到该窗体,然后程序能够关闭找到的窗体 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先找到,然后关 public const int WM_CLOSE = 0x10; [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private void button1_Click(object sender, System.EventArgs e) { IntPtr hwnd_win; hwnd_win = FindWindow(null, "要找的窗体名"); SendMessage(hwnd_win, WM_CLOSE, 0, 0); } //查找窗体 [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); static void Main() { //@Form1根据窗体的Text文本值获得窗体 int WINDOW_HANDLER = FindWindow(null, @"Form1"); } 利用2楼的方法测试一个记事本窗口可以关闭想问下的是那个WM_CLOSE的定义是什么意思? public const int WM_CLOSE = 0x10; [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SendMessageA")] public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private void button1_Click(object sender, System.EventArgs e) { IntPtr hwnd_win; hwnd_win = FindWindow(null, "要找的窗体名"); SendMessage(hwnd_win, WM_CLOSE, 0, 0); }2楼这段测试可行,搂主可以直接用。 //获取应用程序名称 public List<string> GetRunApplicationList(Form appForm) { List<string> appString = new List<string>(); try { int handle = (int)appForm.Handle; int hwCurr; hwCurr = GetWindow(handle, GW_HWNDFIRST); while (hwCurr > 0) { int isTask = (WS_VISIBLE | WS_BORDER); int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE); bool taskWindow = ((lngStyle & isTask) == isTask); if (taskWindow) { int length = GetWindowTextLength(new IntPtr(hwCurr)); StringBuilder sb = new StringBuilder(2 * length + 1); GetWindowText(hwCurr, sb, sb.Capacity); string strTitle = sb.ToString(); if (!string.IsNullOrEmpty(strTitle)) { appString.Add(strTitle); } } hwCurr = GetWindow(hwCurr, GW_HWNDNEXT); } } catch (Exception ex) { MessageBox.Show("读取应用程序信息时出错:" + ex.Message); } return appString; }调用List<string> list=GetRunApplicationList(this);foreach(string var in list){if(var=="HuangHePower")......}应该可以的。 //关闭不信任的应用程序,如我的电脑,网上邻居 try { IntPtr findPtr = FindWindow(null, "HuangHePower"); PostMessage(findPtr, WM_CLOSE, 0, 0); } catch { } 如何将端口内的视频流数据直接转发? .net 安装程序,如何在第二个对话框中获取第一个对话框中的Edit1Value? c# 读取xml文件内容 winform 中如何在gridview中实现双选项的下拉类表 窗体布局问题 navigate没有采用5个参数的重载。什么意思 使用NHinernate,建表的时候只能将一个字段做为主键吗? 颜色转换 C# 裏用 MailMessage 發郵件時,CC 和 BCC 有數量限制嗎? listview数据刷新后,原来的数据怎么删除? 怎么在listbox中显示一个窗体? 求救各位大侠
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hwnd_win; hwnd_win = FindWindow(null, "要找的窗体名");
SendMessage(hwnd_win, WM_CLOSE, 0, 0);
}
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
static void Main()
{ //@Form1根据窗体的Text文本值获得窗体
int WINDOW_HANDLER = FindWindow(null, @"Form1");
}
想问下的是那个WM_CLOSE的定义是什么意思?
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hwnd_win; hwnd_win = FindWindow(null, "要找的窗体名");
SendMessage(hwnd_win, WM_CLOSE, 0, 0);
}2楼这段测试可行,搂主可以直接用。
//获取应用程序名称
public List<string> GetRunApplicationList(Form appForm)
{
List<string> appString = new List<string>();
try
{
int handle = (int)appForm.Handle;
int hwCurr;
hwCurr = GetWindow(handle, GW_HWNDFIRST);
while (hwCurr > 0)
{
int isTask = (WS_VISIBLE | WS_BORDER);
int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE);
bool taskWindow = ((lngStyle & isTask) == isTask);
if (taskWindow)
{
int length = GetWindowTextLength(new IntPtr(hwCurr));
StringBuilder sb = new StringBuilder(2 * length + 1);
GetWindowText(hwCurr, sb, sb.Capacity);
string strTitle = sb.ToString();
if (!string.IsNullOrEmpty(strTitle))
{
appString.Add(strTitle);
}
}
hwCurr = GetWindow(hwCurr, GW_HWNDNEXT);
}
}
catch (Exception ex)
{
MessageBox.Show("读取应用程序信息时出错:" + ex.Message);
}
return appString;
}调用List<string> list=GetRunApplicationList(this);
foreach(string var in list)
{
if(var=="HuangHePower")
......
}应该可以的。
try
{
IntPtr findPtr = FindWindow(null, "HuangHePower");
PostMessage(findPtr, WM_CLOSE, 0, 0);
}
catch
{
}