的标题说明了一切,我有这样的代码:重复FindWindow函数每次出现的窗口/打开
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
const UInt32 WM_CLOSE = 0x0010;
和这里就是我加入Form1_Load
:
IntPtr windowPtr = FindWindowByCaption(IntPtr.Zero, "Untitled - Notepad");
if (windowPtr == IntPtr.Zero)
{
MessageBox.Show("Window not found");
return;
}
SendMessage(windowPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
所以我添加的代码上面的Form1_Load
函数,它实际上工作,它关闭记事本,当我打开我的程序,但我的问题是,如何使功能重复,如关闭记事本,每当它打开,不仅在Form1_Load
?
[编写自动化等待窗口被创建(并解雇它)](http://blogs.msdn.com/b/oldnewthing/archive/2014/02/17/10500645.aspx) – GSerg
@GSerg我试过了,它什么也没做,它没有关闭它,尝试用记事本,运行和很多其他的... – user3542613
它的运行方式与运行对话框一样,只要用''Run''字面替换此对话框的标题显示在您的Windows语言中。对于记事本,由于它没有此代码按下的取消按钮,因此您需要编写不同的结束代码。然而,我的观点是,这是您在创建新窗口时收到通知的方式。您可以使用现有的代码作为事件处理程序。 – GSerg