如何从Visual Studio应用程序打开“文件资源管理器”窗口并设置位置和大小?

埃尔伍德472

我有一个VB.net应用程序,该应用程序可以压缩JPG文件,对其进行重命名,然后将它们从一个位置复制到另一个位置。当最终用户使用该程序时,他们将打开两个文件浏览器窗口以获取源位置和目标位置,并将它们拖到文本框中。

我添加了打开两个文件浏览器以设置位置的代码,但是我希望一个窗口位于屏幕的左下角,另一个窗口位于右下角。每个尺寸将占据屏幕的1/4。

我发现的大多数东西都很老。我发现有人说无法做到这一点,而其他人则提供了非常旧的代码,这些代码似乎无法在Visual Studio 2019中很好地发挥作用。

Private Sub btnOpenExplorer_Click(sender As Object, e As EventArgs) Handles btnOpenExplorer.Click

    Process.Start("explorer.exe", String.Format("/n, /e, {0}", "C:\Users\" & Environment.UserName & "\Box\Site Visit Photos"))
    Process.Start("explorer.exe", String.Format("/n, /e, {0}", "P:\"))

End Sub

上面的代码运行良好。我只需要添加大小和位置。

用户名

解决方案1

问题

我认为在调用程序例程中很难做到这一点,btnOpenExplorer_Click因为要获得一个分配了所有属性的流程对象还为时过早。通常,解决此问题所需ProcessMainWindowTitleProcess.MainWindowHandle属性。一种解决方法是使调用方启动进程,并由Timer通过SetWindowPos函数进行定位和调整大小

这是我的处理方式:

API函数

<DllImport("user32.dll", EntryPoint:="SetWindowPos")>
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll")>
Private Shared Function IsIconic(hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<DllImport("user32.dll")>
Public Shared Function ShowWindow(hWnd As IntPtr, <MarshalAs(UnmanagedType.I4)> nCmdShow As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

类级别常量和变量

Private Const HWND_TOP As Integer = &H0
Private Const SW_SHOWNORMAL As Integer = &H1

Private dir1, dir2 As String
Private WithEvents Timer1 As New Timer With {.Interval = 250}

流程查找器

Private Function GetExplorerProcess(title As String) As Process
    Dim dirName As String = If(IO.Directory.Exists(title), New IO.DirectoryInfo(title).Name, title).ToLower

    Return Process.GetProcesses.Where(
        Function(a) a.ProcessName.ToLower.Equals("explorer") AndAlso
        a.MainWindowTitle.ToLower.Equals(dirName)
        ).FirstOrDefault
End Function

呼叫者,召集者

Private Sub btnOpenExplorer_Click(sender As Object, e As EventArgs) Handles btnOpenExplorer.Click
    Dim proc1 As Process = GetExplorerProcess(dir1)

    If proc1 Is Nothing Then
        Dim procInfo1 As New ProcessStartInfo With {
        .FileName = "explorer.exe",
        .Arguments = dir1,
        .WindowStyle = ProcessWindowStyle.Normal
        }

        Process.Start(procInfo1)
    End If

    Dim proc2 As Process = GetExplorerProcess(dir2)

    If proc2 Is Nothing Then
        Dim procInfo2 As New ProcessStartInfo With {
        .FileName = "explorer.exe",
        .Arguments = dir2,
        .WindowStyle = ProcessWindowStyle.Normal
        }

        Process.Start(procInfo2)
    End If

    Timer1.Start()
End Sub

计时器-激活Windows,同时设置大小和位置

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim proc1 As Process = GetExplorerProcess(dir1)
    Dim proc2 As Process = GetExplorerProcess(dir2)

    If proc1 IsNot Nothing AndAlso proc2 IsNot Nothing Then
        Timer1.Stop()

        Dim R As Rectangle = Screen.PrimaryScreen.WorkingArea
        Dim R1 As New Rectangle(R.X, R.Height - (R.Height / 3), R.Width / 2, R.Height / 4)
        Dim R2 As New Rectangle(R1.Right, R1.Y, R1.Width, R1.Height)
        Dim hWnd1 As IntPtr = proc1.MainWindowHandle
        Dim hWnd2 As IntPtr = proc2.MainWindowHandle

        'Restore the first window if its minimized.
        If IsIconic(hWnd1) Then
            ShowWindow(hWnd1, SW_SHOWNORMAL)
        End If

        'Set the size and location of the first window.
        SetWindowPos(hWnd1, IntPtr.op_Explicit(HWND_TOP), R1.X, R1.Y, R1.Width, R1.Height, 0)

        'Restore the second window if its minimized.
        If IsIconic(hWnd2) Then
            ShowWindow(hWnd2, SW_SHOWNORMAL)
        End If

        'Set the size and location of the second window.
        SetWindowPos(hWnd2, IntPtr.op_Explicit(HWND_TOP), R2.X, R2.Y, R2.Width, R2.Height, 0)
    End If
End Sub

解决方案2

经过四处搜寻之后,我想出了一种更好的方法(我认为),方法是使用Microsoft Internet控件-SHDocVw组件。

首先,我们需要添加对该COM组件的引用:

  • 在项目浏览器中,右键单击该References节点,然后选择Add Reference
  • 在参考管理器中,选择COM选项卡。
  • 查找并检查,Microsoft Internet Controls然后单击确定。

我们只需要从solution1的API和常量,以及一个新的函数来获取IE窗口:

Private Function GetIE(dir As String) As SHDocVw.InternetExplorer
    Return (From ie In New SHDocVw.ShellWindows
            Where New Uri(DirectCast(ie, SHDocVw.InternetExplorer).LocationURL).LocalPath.Equals(dir, StringComparison.OrdinalIgnoreCase)
            Select DirectCast(ie, SHDocVw.InternetExplorer)).FirstOrDefault
End Function

最后,调用者:

Private Sub btnOpenExplorer_Click(sender As Object, e As EventArgs) Handles btnOpenExplorer.Click
    Dim dir1 As String = "FirstPath"
    Dim dir2 As String = "SecondPath"
    Dim ie1, ie2 As SHDocVw.InternetExplorer

    If Not IO.Path.GetPathRoot(dir1).Equals(dir1, StringComparison.OrdinalIgnoreCase) Then
        dir1 = dir1.TrimEnd(IO.Path.DirectorySeparatorChar)
    End If

    If Not IO.Path.GetPathRoot(dir2).Equals(dir2, StringComparison.OrdinalIgnoreCase) Then
        dir2 = dir2.TrimEnd(IO.Path.DirectorySeparatorChar)
    End If

    ie1 = GetIE(dir1)
    ie2 = GetIE(dir2)

    If ie1 Is Nothing OrElse ie2 Is Nothing Then
        Process.Start(dir1)
        Process.Start(dir2)
        Threading.Thread.Sleep(1000)
    End If

    If ie1 Is Nothing Then ie1 = GetIE(dir1)
    If ie2 Is Nothing Then ie2 = GetIE(dir2)

    If ie1 IsNot Nothing AndAlso ie2 IsNot Nothing Then
        Dim hWnd1 = IntPtr.op_Explicit(ie1.HWND)
        Dim hWnd2 = IntPtr.op_Explicit(ie2.HWND)
        Dim R As Rectangle = Screen.PrimaryScreen.WorkingArea
        Dim R1 As New Rectangle(R.X, R.Height - (R.Height \ 3), R.Width \ 2, R.Height \ 4)
        Dim R2 As New Rectangle(R1.Right, R1.Y, R1.Width, R1.Height)

        SetWindowPos(hWnd1, IntPtr.op_Explicit(HWND_TOP), R2.X, R2.Y, R2.Width, R2.Height, 0)
        SetWindowPos(hWnd2, IntPtr.op_Explicit(HWND_TOP), R1.X, R1.Y, R1.Width, R1.Height, 0)

        If IsIconic(hWnd1) Then
            ShowWindow(hWnd1, SW_SHOWNORMAL)
        End If

        If IsIconic(hWnd2) Then
            ShowWindow(hWnd2, SW_SHOWNORMAL)
        End If
    End If
End Sub

请注意,该解决方案也可用于驱动器(IE:c:\d:\.. etc。),而第一个驱动器则不行。

这是一个演示:

IEDemo

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Visual Studio 2012 Ultimate中打开2个团队资源管理器窗口

来自分类Dev

Visual Studio测试资源管理器窗口将不会打开

来自分类Dev

Visual Studio 代码资源管理器设置

来自分类Dev

如何设置 Windows 文件资源管理器以在地址栏中使用自定义“别名”(如“桌面”或“程序文件”)自动打开位置?

来自分类Dev

在构建 Android 应用程序时,如何获得一个按钮以在文件资源管理器中打开特定文件夹?

来自分类Dev

Visual Studio 2010 C#-将已保存的文件从文件资源管理器加载到应用程序中

来自分类Dev

如何从Qt打开文件资源管理器?

来自分类Dev

如何使 gedit 成为 Ubuntu 20/文件资源管理器中 HTML 文件的默认应用程序?

来自分类Dev

每次从文件资源管理器打开终端时,如何获取 PowerShell 当前位置

来自分类Dev

如何使用Azure资源管理器模板创建虚拟应用程序/文件夹?

来自分类Dev

如何知道应用程序是从控制台还是资源管理器运行的?

来自分类Dev

如何在Visual Studio的解决方案资源管理器中添加文件?

来自分类Dev

Visual Studio 2017:解决方案资源管理器和其他辅助选项卡/窗口总是关闭并且必须重新打开

来自分类Dev

如何使用某个驱动器中打开的文件夹关闭Windows资源管理器窗口

来自分类Dev

如何关闭从某个驱动器中打开的文件夹的Windows资源管理器窗口

来自分类Dev

如何将特定文件的资源管理器窗口设置为TopMost表单的子窗口?

来自分类Dev

在Visual Studio 2012中打开TFS源代码管理资源管理器的热键?

来自分类Dev

编译后Visual Studio冻结。当我尝试启动应用程序时,资源管理器也是如此

来自分类Dev

如何自动在包资源管理器中打开与查看的.java相对应的文件夹位置

来自分类Dev

如何使用Android设备监视器的文件资源管理器查找我的应用程序的/ data / data

来自分类Dev

如何获取打开的资源管理器窗口的PIDL?

来自分类Dev

如何在Visual Studio 2019中的解决方案资源管理器中打开包含文件的文件夹

来自分类Dev

Visual Studio测试资源管理器图标

来自分类Dev

Visual Studio 2013测试资源管理器

来自分类Dev

更改Visual Studio解决方案资源管理器的字体大小

来自分类Dev

如何以新创建的资源管理器进程的子级启动应用程序?

来自分类Dev

如何从C#应用程序访问Windows资源管理器上下文菜单?

来自分类Dev

资源管理器崩溃后如何将最小化的应用程序带到托盘中?

来自分类Dev

如何打开Pydev包资源管理器

Related 相关文章

  1. 1

    在Visual Studio 2012 Ultimate中打开2个团队资源管理器窗口

  2. 2

    Visual Studio测试资源管理器窗口将不会打开

  3. 3

    Visual Studio 代码资源管理器设置

  4. 4

    如何设置 Windows 文件资源管理器以在地址栏中使用自定义“别名”(如“桌面”或“程序文件”)自动打开位置?

  5. 5

    在构建 Android 应用程序时,如何获得一个按钮以在文件资源管理器中打开特定文件夹?

  6. 6

    Visual Studio 2010 C#-将已保存的文件从文件资源管理器加载到应用程序中

  7. 7

    如何从Qt打开文件资源管理器?

  8. 8

    如何使 gedit 成为 Ubuntu 20/文件资源管理器中 HTML 文件的默认应用程序?

  9. 9

    每次从文件资源管理器打开终端时,如何获取 PowerShell 当前位置

  10. 10

    如何使用Azure资源管理器模板创建虚拟应用程序/文件夹?

  11. 11

    如何知道应用程序是从控制台还是资源管理器运行的?

  12. 12

    如何在Visual Studio的解决方案资源管理器中添加文件?

  13. 13

    Visual Studio 2017:解决方案资源管理器和其他辅助选项卡/窗口总是关闭并且必须重新打开

  14. 14

    如何使用某个驱动器中打开的文件夹关闭Windows资源管理器窗口

  15. 15

    如何关闭从某个驱动器中打开的文件夹的Windows资源管理器窗口

  16. 16

    如何将特定文件的资源管理器窗口设置为TopMost表单的子窗口?

  17. 17

    在Visual Studio 2012中打开TFS源代码管理资源管理器的热键?

  18. 18

    编译后Visual Studio冻结。当我尝试启动应用程序时,资源管理器也是如此

  19. 19

    如何自动在包资源管理器中打开与查看的.java相对应的文件夹位置

  20. 20

    如何使用Android设备监视器的文件资源管理器查找我的应用程序的/ data / data

  21. 21

    如何获取打开的资源管理器窗口的PIDL?

  22. 22

    如何在Visual Studio 2019中的解决方案资源管理器中打开包含文件的文件夹

  23. 23

    Visual Studio测试资源管理器图标

  24. 24

    Visual Studio 2013测试资源管理器

  25. 25

    更改Visual Studio解决方案资源管理器的字体大小

  26. 26

    如何以新创建的资源管理器进程的子级启动应用程序?

  27. 27

    如何从C#应用程序访问Windows资源管理器上下文菜单?

  28. 28

    资源管理器崩溃后如何将最小化的应用程序带到托盘中?

  29. 29

    如何打开Pydev包资源管理器

热门标签

归档