20

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

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

我还在MiraDisp.dll 上找到了这个文档,并且有两个函数 OpenMiracastSession 和 CloseMiracastSession。

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

4

6 回答 6

9

首先,感谢@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

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

操作中心中的连接按钮的图像
在此处输入图像描述

于 2016-04-12T18:23:29.963 回答
8

首先我想说@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. 等待后按回车键会自动选择列表中的第一个无线显示适配器并触发连接功能(如果找不到无线显示适配器,您的默认浏览器将打开“帮助”链接)
  4. 脚本做的最后一件事是按 ESC 键关闭操作中心

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

于 2016-04-22T13:16:05.477 回答
2

这是我编写的 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}
于 2019-09-23T16:43:42.723 回答
2

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

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

基本步骤是:

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

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

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

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

于 2019-07-21T16:55:47.957 回答
0

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

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

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

基本铸造

高级铸造

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

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

启动 UWP 项目

于 2019-09-12T14:11:25.987 回答
0

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

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

于 2021-07-21T13:15:24.297 回答