C#查找某一窗口并按钮

 

using System.Runtime.InteropServices;

[DllImport(
"user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport(
"user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport(
"user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

const uint BM_CLICK = 0xF5;

IntPtr msgHandle 
= FindWindow(null"Form1");
if (msgHandle != IntPtr.Zero)
{
    
//找到Button
    IntPtr btnHandle = FindWindowEx(msgHandle, 0"Button""确定");
    
if (btnHandle != IntPtr.Zero)
    {
        SendMessage(btnHandle, BM_CLICK, 
00);
    }
}

 

 

posted @ 2010-06-08 10:45  说不得  阅读(3678)  评论(1编辑  收藏  举报