2016-02-24 76 views
13

我想寫一個Windows服務(在C#中)或PowerShell腳本,將我的筆記本電腦自動(在啓動或組合鍵時)連接到我的MS無線顯示適配器進行屏幕鏡像。在Windows 10中,我只能通過轉到通知手動進行操作,然後單擊連接> MS無線適配器>連接。在Windows 10上自動連接到MS無線顯示器

我發現有一個Miracast API(https://msdn.microsoft.com/en-us/library/windows/hardware/dn265515(v=vs.85).aspx),但沒有太多關於如何使用它的文檔。

我還發現這個http://filelog.net/file/MiraDisp.dll/2c279c8d57a47a9ca06aa279bd9e0e8e22c7c1c1,並且有兩個函數OpenMiracastSession和CloseMiracastSession。

問題是我不知道如何在c#中使用這些函數。我知道我可能不得不使用pInvoke。任何人都可以將我指向正確的方向嗎?

+0

@MuratYıldız - 我們試圖連接到「無線顯示」 - 而不僅僅是一個WiFi網絡。 – jaredbaszler

+2

我不認爲這些Miracast API是你自己認爲的,它們可能是Windows使用自己(底層)執行屏幕鏡像的原因,而不是屏幕鏡像本身的API。你有沒有考慮過像[AutoHotKey](https://www.autohotkey.com/)這樣的東西,你可以記錄按鍵+鼠標宏,並用按鍵執行它們 – CodingGorilla

+0

根據你列出的文檔,Miracast API是列表駕駛員必須執行的功能;你不能調用它們,因爲操作系統必須已經有一個到設備的套接字連接。您是否在Notifications API之上尋找自動化?這將是最有可能的,因爲你只是試圖自動點擊連接 - > MS無線適配器 - >連接。 –

回答

5

首先,感謝@CodingGorilla對AutoHotkey的建議。過去幾天我一直在玩這個遊戲。

我去了AutoHotkey路線,因爲我找不到一個容易的地方開始與任何Windows 10 API。各種各樣的文件推出吐司通知,但我找不到任何東西來控制行動中心。 如果有人對此有建議,請發佈。

這是我想出的使用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

最後,這可能不會,如果無線顯示已經連接工作。在我的環境中,連接無線顯示器會導致平板電腦上的分辨率發生變化,因此無法在屏幕上找到圖像。

Image of the connect button in Action Center
enter image description here

4

首先我想說的是,@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,這將打開操作中心連接窗口
  2. 然後,它會等待3秒,使無線顯示軟件狗可以發現(您可以隨心所欲地調整此值,但我需要等待超過2秒纔會顯示我的無線顯示屏加密狗)
  3. 等待後,將按下ENTER鍵,它將自動選擇列表中的第一個無線顯示加密狗並觸發連接功能(如果沒有無線顯示軟件狗可以找到您的默認瀏覽器將打開「幫助」鏈接)
  4. T腳本所做的最後一件事就是按ESC鍵關閉操作中心

好吧,就是這樣。它沒什麼特別的,但它的工作原理。我用我的平板電腦和我的無線顯示加密狗(我有這個here)測試了這個腳本幾次,它似乎工作得很好。 不幸的是,如果您有多個無線顯示加密狗同時運行,我的腳本將無法按預期工作,因爲我的腳本只會選擇顯示的第一個無線顯示加密狗。 (這對我來說不是問題,因爲我只有一個無線顯示加密狗)

+1

這是一個非常簡單的解決方案,應該可以很好地工作。 請注意,您也可以發送一些{Tab}鍵將高亮區移到所需的適配器上,然後發送{Enter}鍵以激活它。 –

+0

另外,如果您有多個適配器,則可以使用{Tab}突出顯示「搜索」選項,然後發送一些文本進行搜索,然後{製表符}將其發送到生成的適配器併發送{輸入}。 –

相關問題