【问题标题】:Autoconnect to MS Wireless display on Windows 10自动连接到 Windows 10 上的 MS Wireless 显示器
【发布时间】:2016-06-06 16:12:13
【问题描述】:

我想编写一个 Windows 服务(在 c# 中)或一个 powershell 脚本,将我的笔记本电脑(在启动或组合键时)自动连接到我的 MS 无线显示适配器以进行屏幕镜像。在 Windows 10 中,我只能通过转到通知并单击“连接”>“MS 无线适配器”>“连接”来手动完成。

我发现有一个Miracast API,但没有太多关于如何使用它的文档。

我也发现了这个documentation on MiraDisp.dll,里面有两个函数 OpenMiracastSession 和 CloseMiracastSession。

问题是我不知道如何在 c# 中使用这些函数。我知道我可能不得不使用 pInvoke。谁能指出我正确的方向?

【问题讨论】:

  • @MuratYıldız - 我们正在尝试连接到“无线显示器” - 而不仅仅是 WiFi 网络。
  • 我不认为这些 Miracast API 是您认为的那样,它们可能是 Windows 自己(在引擎盖下)用来进行屏幕镜像的,而不是屏幕镜像本身的 API .您是否考虑过类似AutoHotKey 的东西,您基本上可以记录按键+鼠标宏并通过按键执行它们
  • 根据您列出的文档,Miracast API 是驱动程序必须实现的功能列表;你不能调用这些,因为操作系统必须已经有一个到设备的套接字连接。您是否在 Notifications API 之上寻找自动化?这是最有可能的,因为您实际上只是在尝试自动单击“连接”->“MS 无线适配器”->“连接”。
  • 我会试试 autokotkey 和通知 API! :-)
  • @BrechtBaekelandt - 我尝试了 AutoHotkey - 下面是我想出的。你在通知 api 路线上有什么运气吗?

标签: c# miracast


【解决方案1】:

首先,感谢@CodingGorilla 对 AutoHotkey 的建议。过去几天我一直在玩这个。

我选择了 AutoHotkey 路线,因为我找不到从任何 Windows 10 API 开始的简单位置。那里有各种文档来推送 toast 通知,但我找不到任何东西来控制行动中心。 如果有人有这方面的建议,请发表。

这是我使用 AutoHotkey 得出的结论。非常简单但不是理想的解决方案,因为这有一些变量。下面是我用来打开操作中心的 AutoHotkey 脚本代码,点击连接,然后点击最上面列出的无线显示:

Send #a ;Sends Windows button + A to open the action center
Sleep, 750 ; Give it some time to slide open
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\Connect.png ;Try to find the Connect button tile
if ErrorLevel = 2
    MsgBox Could not conduct the search for the connect button in action center. Make sure your search image is in the correct location.
else if ErrorLevel = 1
    MsgBox Connect button cannot be found on the screen.
else
    MoveMouseAndClick(FoundX, FoundY)
Sleep, 1250 ;Delay so the wireless displays have a chance to load into the Action Center window
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\WirelessDisplay.png
if ErrorLevel = 2
    MsgBox Could not conduct the search for the wireless display. 
else if ErrorLevel = 1
    {   
        ;Search image cannot be found. Try 1 more time in case it took a long time for the wireless displays to appear 
        Sleep, 750
        ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\WirelessDisplay.png ;try to find the first Wireless Display device listed
        if ErrorLevel = 1
            MsgBox Wireless display image cannot be found on the screen. Make sure the wireless device is turned on.
        else
            MoveMouseAndClick(FoundX, FoundY)
    }
else
    MoveMouseAndClick(FoundX, FoundY)
Send {Esc} ;Send Esc to get rid of the Action Center window
Return

MoveMouseAndClick(x, y) {
    MouseMove, x + 25, y + 25 ;Move it down the right a bit to make sure we click the button
    Sleep, 250 
    MouseClick, left
}

我还附上了图片作为我制作的示例。 您需要制作自己的搜索图片。在制作这些图像之前,您还必须在 Windows 10 中关闭操作中心、开始和任务栏的透明度-设置->个性化->颜色->使开始、任务栏和操作中心透明->关闭。重做第二张图像尤其重要,因为我的图像在图像中列出了“Roku Stick”。我不得不在我的桌面开发机器和我正在运行这个脚本的 MS Surface 3 之间重做我的搜索图像。分辨率等将在设备之间改变。按照此处有关如何创建自己的搜索图像的说明进行操作:

https://autohotkey.com/docs/commands/ImageSearch.htm

最后,如果无线显示器已连接,这可能不起作用。在我的环境中,连接无线显示器会导致平板电脑的分辨率发生变化,因此无法在屏幕上找到图像。


【讨论】:

    【解决方案2】:

    首先我想说@jaredbaszler 提供了一个非常好的解决方案。它就像一个魅力谢谢你:)

    我也在玩 AutoHotkey,因为我想知道是否有其他方法可以做到这一点。过了一会儿,我想出了以下脚本:

    Send #k ; Sends Windows button + K to open the Action Center Connect window
    Sleep, 3000 ; Wait some time so the wireless display dongle can be found
    Send {Enter} ; Send ENTER key to connect to wireless display dongle (works when only 1 is found)
    Send {Esc} ; Send ESC key to close the Action Center Connect window
    

    好的。现在让我解释一下这个脚本是如何工作的:

    1. 首先它会按下 WIN+K,这将打开 Action Center Connect 窗口
    2. 然后它会等待 3 秒,以便找到无线显示加密狗(您可以随意调整此值,但我需要等待超过 2 秒才能显示我的无线显示加密狗)
    3. 等待后按 ENTER 键会自动选择列表中的第一个无线显示适配器并触发连接功能(如果找不到无线显示适配器,您的默认浏览器将打开“帮助”链接)
    4. 脚本执行的最后一件事是按 ESC 键关闭操作中心

    嗯,就是这样。这没什么特别的,但它确实有效。我已经用我的平板电脑和无线显示加密狗(我有这个here)测试了这个脚本几次,它似乎工作得很好。 不幸的是,如果您同时启动并运行多个无线显示加密狗,我的脚本将无法按预期工作,因为我的脚本只会选择出现的第一个。 (这对我来说不是问题,因为我只有一个无线显示加密狗)

    【讨论】:

    • 这是一个非常简单的解决方案,应该可以很好地工作。请注意,您还可以发送一些 {Tab} 键将突出显示移动到所需适配器上,然后发送 {Enter} 键以激活它。
    • 另外,如果您有多个适配器,您可以使用 {Tab} 突出显示“搜索”选项,然后发送一些文本进行搜索,然后 {Tab} 到生成的适配器并发送 {Enter }.
    • 我发现我需要在 {Return} 之前发送一个 {Tab} 我的情况是,在 Connect 上,我的无线显示器 (MSDisplayAdapter_D1) 首先列出,然后是 Jabro Halo(无线耳机)。
    【解决方案3】:

    这个问题有点老了,但目前我遇到了同样的问题。

    我建议根据Advanced casting sample 中的示例 5 + 6 使用 ProjectionManager 类编写 UWP 应用程序。

    主要的步骤是:

    • 获取 DeviceID(例如通过使用 DevicePicker 类进行选择)
    • 调用ProjectionManager.StartProjectingAsync(newViewId, currentViewId, selectedDeviceInformation)开始投影

    DeviceInformation 对象可以通过使用获取(并保存)的 deviceId 调用 DeviceInformation.CreateFromIdAsync() 来获取。 viewIds 可能被设置为 0(或其他无效值),投影无论如何都会开始。

    如果投影已经开始,应用可以关闭,投影仍然保持活动状态。

    如果有人对解决方案感兴趣,我会将代码上传到 GitHub。

    【讨论】:

    【解决方案4】:

    这是我编写的 AutoHotKey 脚本;

    Run, explorer.exe ms-settings-connectabledevices:devicediscovery
    Sleep, 800
    Send, {Tab}
    Send,+{Tab}
    Send,{Enter}
    Sleep, 200
    ; then next Send command types the first few letters of the name of the WiDi adaptor
    Send, Microsoft
    Sleep, 200
    Send, {Tab}
    Sleep, 200
    Send, {Enter}
    

    【讨论】:

      【解决方案5】:

      要自动化和控制 Miracast,您需要使用 Windows.Media.Casting 命名空间和 Windows 通用示例编写一个 UWP 应用程序。

      它们可以用许多常用语言编写,包括 C#、C++、Javascript 和 Visual Basic。

      Windows 提供了两组功能示例程序来演示您可以使用可以作为 Visual Studio 解决方案 (.sln) 打开和启动的 Miracast 做什么。

      Basic Casting

      Advanced Casting

      由于 UWP 应用旨在在 Windows 应用商店中提供,因此它们具有特定的功能要求和规定。 (我建议研究这些。)

      还可以避免我在开始编写通用程序然后想知道通用 Windows 库在哪里时所犯的错误:

      Starting a UWP project

      【讨论】:

        【解决方案6】:

        我最终从这里使用了基于 vbs 的解决方案: https://superuser.com/questions/1062789/how-to-connect-to-a-wireless-display-adapter-automatically-through-scripts-or-ta

        虽然它基本上执行相同的步骤,但我发现这比其他解决方案更强大。更重要的是,vbs 在大多数 Win 机器上都可用(我认为)。

        【讨论】:

          猜你喜欢
          • 2018-05-02
          • 2017-07-20
          • 2021-07-22
          • 2015-11-04
          • 2012-09-14
          • 2021-04-08
          • 2010-09-12
          • 2017-04-06
          • 1970-01-01
          相关资源
          最近更新 更多