//How I can get the form object from handle ?
//下面的代码有问题
using System;
using System.Management;
using System.Diagnostics;
using System.Windows.Forms;
public class WindowWrapper : IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
public class RemoteConnect
{
public static void Main()
{
Process[] procs = Process.GetProcessesByName("Notepad");
if (procs.Length != 0)
{
IntPtr hwnd = procs[0].MainWindowHandle;
}
else
Console.WriteLine("Notepad is not running.");
if (procs.Length != 0)
{
IntPtr hwnd = procs[0].MainWindowHandle;
Control c = Control.FromHandle(hwnd);
if (c != null) //c is null here
{
Console.WriteLine((c as Form).Text);
}
else
{
Console.WriteLine("Null");
}
}
}
}
[此贴子已经被作者于2006-8-13 15:58:05编辑过]