winform.知道知道窗体句柄,怎么关闭这个窗体?
2015-08-19 · 知道合伙人软件行家
关注
展开全部
--相应的引用
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern int FindWindowEx(IntPtr lpClassName, IntPtr lpWindowName, string isnull, string anniu);
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void test()
{
const int WM_CLOSE = 0x10; //关闭
const uint WM_DESTROY = 0x02;
const uint WM_QUIT = 0x12;
const int BM_CLICK = 0xF5; //单击
IntPtr Window_Handle = (IntPtr)FindWindow(null, "Microsoft Word");//查找所有的窗体,看看想查找的句柄是否存在,Microsoft Word 句柄 //
if (Window_Handle ==IntPtr.Zero) //如果没有查找到相应的句柄
{
MessageBox.Show("没有找到窗体");
}
else //查找到相应的句柄
{
SendMessage(Window_Handle, WM_CLOSE, 0, 0); //关闭窗体
// IntPtr childHwnd = (IntPtr)FindWindowEx(Window_Handle, IntPtr.Zero, null, "点击");//查找句柄中相应的按钮
// if (childHwnd == IntPtr.Zero)
// {
// MessageBox.Show("没有找到按钮");
// }
// else
// {
// SendMessage(childHwnd, BM_CLICK, 0, 0); //单击这个句柄中的按钮
// }
}
}
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern int FindWindowEx(IntPtr lpClassName, IntPtr lpWindowName, string isnull, string anniu);
[DllImport("user32.dll", EntryPoint = "SendMessageA")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void test()
{
const int WM_CLOSE = 0x10; //关闭
const uint WM_DESTROY = 0x02;
const uint WM_QUIT = 0x12;
const int BM_CLICK = 0xF5; //单击
IntPtr Window_Handle = (IntPtr)FindWindow(null, "Microsoft Word");//查找所有的窗体,看看想查找的句柄是否存在,Microsoft Word 句柄 //
if (Window_Handle ==IntPtr.Zero) //如果没有查找到相应的句柄
{
MessageBox.Show("没有找到窗体");
}
else //查找到相应的句柄
{
SendMessage(Window_Handle, WM_CLOSE, 0, 0); //关闭窗体
// IntPtr childHwnd = (IntPtr)FindWindowEx(Window_Handle, IntPtr.Zero, null, "点击");//查找句柄中相应的按钮
// if (childHwnd == IntPtr.Zero)
// {
// MessageBox.Show("没有找到按钮");
// }
// else
// {
// SendMessage(childHwnd, BM_CLICK, 0, 0); //单击这个句柄中的按钮
// }
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询