如何使用VB.NET代码关闭监视器
本文关键字:监视器 代码 NET 何使用 VB | 更新日期: 2023-09-27 17:58:13
如何使用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
是的,接受的答案中的声明不正确。64位版本的Windows可能会出现随机故障,因为传递的参数大小错误。它们应该是这样的:
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。虽然不确定它是否有用,但用户很明显,由于某种原因,该命令不起作用。
这个在使用VB2008的Windows7上运行。不要担心再次打开显示器,因为这就像电源选项中的"关闭显示器"一样。显示器关闭,要重新打开它,你必须按下键盘上的任何键或移动鼠标。
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