每次窗口出现/打开时重复FindWindow功能

时间:2015-05-01 12:24:38

标签: c# 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

0 个答案:

没有答案