自动连接到 Windows 10 上的 MS 无线显示器

2024-01-13

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

我发现有一个Miracast API https://learn.microsoft.com/en-us/windows-hardware/drivers/display/wireless-displays--miracast-但关于如何使用它的文档并不多。

我也发现了这个MiraDisp.dll 的文档 http://filelog.net/file/MiraDisp.dll/2c279c8d57a47a9ca06aa279bd9e0e8e22c7c1c1有两个函数OpenMiracastSession和CloseMiracastSession。

问题是我不知道如何在 C# 中使用这些函数。我知道我可能必须使用 pInvoke。有人能指出我正确的方向吗?


首先感谢@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 https://autohotkey.com/docs/commands/ImageSearch.htm

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自动连接到 Windows 10 上的 MS 无线显示器 的相关文章

随机推荐