簡體   English   中英

如何使用 VB.NET 代碼關閉顯示器

[英]How to turn off a monitor using VB.NET code

如何使用 VB.NET 代碼關閉顯示器? 好的,實際上我找到了 C# 解決方案。 但我需要 VB.NET 解決方案。 我嘗試了在線 C# 到 VB.NET 轉換器,但轉換器抱怨其中有錯誤。

下面的C#代碼怎么翻譯成VB.NET?

using System.Runtime.InteropServices; //to DllImport

public int WM_SYSCOMMAND = 0x0112;
public int SC_MONITORPOWER = 0xF170; //Using the system pre-defined MSDN constants that can be used by the SendMessage() function .

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
//To call a DLL function from C#, you must provide this declaration.

private void button1_Click(object sender, System.EventArgs e)
{
    SendMessage( this.Handle.ToInt32() , WM_SYSCOMMAND , SC_MONITORPOWER ,2 );//DLL function
}

更新:

我使用在線開發者 Fusion 轉換器

嘗試這個

Public WM_SYSCOMMAND As Integer = &H112
Public SC_MONITORPOWER As Integer = &Hf170

<DllImport("user32.dll")> _
Private Shared Function SendMessage(hWnd As Integer, hMsg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function

Private Sub button1_Click(sender As Object, e As System.EventArgs)
    SendMessage(Me.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub

是的,已接受答案中的聲明不正確。 在 Windows 的 64 位版本上可能出現隨機故障,因為通過的 arguments 的大小錯誤。 它們應該如下所示:

Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MONITORPOWER As Integer = &HF170
Private Const MonitorToLowPower As Integer = 1
Private Const MonitorShutoff As Integer = 2

<DllImport("user32.dll")> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal hMsg As Integer, _
                          ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SendMessage(Me.Handle, WM_SYSCOMMAND, 
                CType(SC_MONITORPOWER, IntPtr), CType(MonitorShutoff, IntPtr))
End Sub

您可以檢查 SendMessage() 的返回值,它應該返回 IntPtr.Zero。 不太確定它是否有用,用戶很明顯該命令由於某種原因不起作用。

這個可以使用 VB 2008 在 Windows 7 上工作。不用擔心再次打開顯示器,因為這就像電源選項中的“關閉顯示器”一樣。 顯示器關閉並重新打開,您必須按鍵盤上的任意鍵或移動鼠標。

Imports System.Runtime.InteropServices

Public Class Monitoroff

Public WM_SYSCOMMAND As Integer = &H112

Public SC_MONITORPOWER As Integer = &HF170

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SendMessage(ByVal hWnd As Integer, ByVal hMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    SendMessage(Me.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2)
End Sub
End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM