c#本段代码需要如何实现?【通过句柄激活窗口 相对窗口点击 通过句柄关闭窗口】

Process[] processes = Process.GetProcessesByName("123"); foreach (Process p in processes) { IntPtr windowHandle = p.MainWindowHandle;// 获取句柄 if (windowHandle != IntPtr.Zero) { MessageBox.Show("找到窗口"); //通过句柄激活窗口 //鼠标点击x=10y=10 //通过句柄关闭窗口 } else MessageBox.Show("没有找到窗口"); }

个人感觉,c# 托管的代码没跨进程这个权限。

如果可行也是用了全局消息钩子

如下  但是杀毒软件估计会各种弹出提示吧

//常用的

DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode);

//这个外部函数(非托管) 可以向句柄发送消息,关闭事件就是这个
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);


/// 你的代码段 if块 
const int WM_CLOSE = 0x0010;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))
{
    SendMessage(ptrWnd, WM_CLOSE, 0, 0);  // 调用了 发送消息 发送关闭窗口的消息
}
else
{
    ptrWnd = IntPtr.Zero;
}

如果你的程序 有这个权限 是一定可以关闭窗口的

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答