当对话框打开时,焦点位于名称框上。我必须选择10次才能找到文件,然后向下箭头指向我想要的文件。
我使用键盘而非鼠标。
private void LoadFileNow()
{
OpenFileDialog Open = new OpenFileDialog();
Open.Filter = "GCode Files|*.ngc";
Open.Title = "Select a GCode File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a .ngc file was selected, open it.
if (Open.ShowDialog() == DialogResult.OK)
{
// Assign the GCode FileName to Var.
GV.GCodeFile = Open.FileName;
string f = "";
f = String.Format("{0}{1}", "GCode File: ", Open.FileName); //Change Ver. 4.0.1.1
label6.Text = f;
TextBox.LoadFile(Open.FileName, RichTextBoxStreamType.PlainText);
}
Thread.Sleep(500); //Wait a moment while file loads.
}
答案 0 :(得分:0)
好的,如果我说得对,你想在对话框打开时关注文件选择窗口?哦,你会爱上这个哈哈。可能是其他方式,但这有效:-p。仅在Windows 7中测试过。您可能需要更改控件搜索。
让我们调用一些东西
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);
[DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("user32.dll")]
internal static extern IntPtr SetForegroundWindow(IntPtr hWnd);
让我们获得父级的子控件的方法
private static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent)
{
List<IntPtr> result = new List<IntPtr>();
IntPtr prevChild = IntPtr.Zero;
IntPtr currChild = IntPtr.Zero;
while (true)
{
currChild = FindWindowEx(hParent, prevChild, null, null);
if (currChild == IntPtr.Zero)
{
break;
}
result.Add(currChild);
prevChild = currChild;
}
return result;
}
这是我们将在您打开对话框之前在线程中运行的方法
private void FocusFileDialog()
{
bool windowFound = false;
while (!windowFound)
{
IntPtr od = FindWindow(null, "Select a GCode File");
//found main dialog
if (od != IntPtr.Zero)
{
IntPtr od1 = FindWindowEx(od, IntPtr.Zero, "DUIViewWndClassName", null);
if (od1 != IntPtr.Zero)
{
IntPtr od2 = FindWindowEx(od1, IntPtr.Zero, "DirectUIHWND", null);
if (od2 != IntPtr.Zero)
{
List<IntPtr> results = GetAllChildrenWindowHandles(od2);
results.ForEach(hwd =>
{
IntPtr od3 = FindWindowEx(hwd, IntPtr.Zero, "SHELLDLL_DefView", null);
if (od3 != IntPtr.Zero)
{
IntPtr found = FindWindowEx(od3, IntPtr.Zero, "DirectUIHWND", null);
if (found != IntPtr.Zero)
{
SetForegroundWindow(found);
SetFocus(found);
windowFound = true;
}
}
});
}
}
}
}
}
以下是打开对话框的方法
new Thread(FocusFileDialog).Start();
using (OpenFileDialog Open = new OpenFileDialog())
{
//your stuff here
}
附件来自Spy ++可能会帮助您理解。选择的是我们需要的指针。