background thread
在打开OpenFIleDialog
. 在后台线程中,搜索可用的打开文件对话框并设置文件名的值。您也可以自动单击打开按钮。有可以在 .net 中使用的 windows API 来执行此操作。
FindWindow 示例:
**Calling Code:**
Thread thread2 = new Thread(new ThreadStart(MyClass.SelectFile));
thread2.IsBackground = true;
thread = thread2;
thread.Start();
**Class Code:**
[DllImport("user32.dll", SetLastError=true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError=true)]
private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
public void SelectFile(string filename)
{
Thread.Sleep(0x3e8);
IntPtr zero = IntPtr.Zero;
IntPtr parentHandle = IntPtr.Zero;
IntPtr child = new WinAPI(Process.GetCurrentProcess().MainWindowHandle, "#32770").GetChild();
while (child == IntPtr.Zero)
{
Application.DoEvents();
}
if (child != IntPtr.Zero)
{
zero = child;
parentHandle = FindWindowEx(zero, IntPtr.Zero, "ComboBoxEx32", "");
if (parentHandle != IntPtr.Zero)
{
parentHandle = FindWindowEx(parentHandle, IntPtr.Zero, "ComboBox", "");
if (parentHandle != IntPtr.Zero)
{
parentHandle = FindWindowEx(parentHandle, IntPtr.Zero, "Edit", "");
if (parentHandle != IntPtr.Zero)
{
SendMessage(parentHandle, 12, IntPtr.Zero, fileName);
parentHandle = FindWindowEx(zero, IntPtr.Zero, "Button", "&Open");
if (!(parentHandle == IntPtr.Zero))
{
SendMessage(parentHandle, 0xf5, IntPtr.Zero, "");
}
}
}
}
}
Thread.Sleep(0x7d0);
}