1

我正在尝试调用“开始”菜单上经常出现的“运行”对话框——我做了一些研究,只找到了一种访问它的方法(使用“Windows 键”+ R)。

所以我假设模拟击键,例如:

SendKeys.Send("{TEST}") 

会做这项工作吗?虽然你怎么能模拟键盘上的“Windows”键呢?

我确信有一种更简单的方法可以做到这一点 - 不使用 sendkeys - 任何人有任何想法吗?

4

1 回答 1

1

您可以使用 PInvoke 来调用运行对话框。

[Flags()]
public enum RunFileDialogFlags : uint
{

    /// <summary>
    /// Don't use any of the flags (only works alone)
    /// </summary>
    None = 0x0000,    

    /// <summary>
    /// Removes the browse button
    /// </summary>
    NoBrowse = 0x0001,

    /// <summary>
    /// No default item selected
    /// </summary>
    NoDefault = 0x0002,

    /// <summary>
    /// Calculates the working directory from the file name
    /// </summary>
    CalcDirectory = 0x0004,

    /// <summary>
    /// Removes the edit box label
    /// </summary>
    NoLabel = 0x0008,

    /// <summary>
    /// Removes the separate memory space checkbox (Windows NT only)
    /// </summary>
    NoSeperateMemory = 0x0020
}

我们需要使用 DllImport 属性导入 DLL。

[DllImport("shell32.dll", CharSet = CharSet.Auto, EntryPoint = "#61", SetLastError = true)]

static extern bool SHRunFileDialog(IntPtr hwndOwner, 
                                   IntPtr hIcon, 
                                   string lpszPath,
                                   string lpszDialogTitle, 
                                   string lpszDialogTextBody, 
                                   RunFileDialogFlags uflags);

执行:

private void ShowRunDialog(object sender, RoutedEventArgs e)
{
    SHRunFileDialog(IntPtr.Zero, 
                    IntPtr.Zero, 
                    "c:\\",
                    "Run Dialog using PInvoke",
                    "Type the name of a program, folder or internet address 
            and Windows will open that for you.",
                    RunFileDialogFlags.CalcDirectory);

}

于 2012-09-09T11:08:29.760 回答