简体   繁体   English

如何使用 VB.NET 代码关闭显示器

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

How do I turn off a monitor using VB.NET code?如何使用 VB.NET 代码关闭显示器? OK, actually I found the C# solution.好的,实际上我找到了 C# 解决方案。 But I need the VB.NET solution.但我需要 VB.NET 解决方案。 I have tried an online C# to VB.NET converter, but the converter is complaining that there are errors in it.我尝试了在线 C# 到 VB.NET 转换器,但转换器抱怨其中有错误。

How can the following C# code be translated to 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
}

UPDATE:更新:

I use the online developer Fusion converter .我使用在线开发者 Fusion 转换器

Try this尝试这个

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

Yes, the declarations in the accepted answer are not correct.是的,已接受答案中的声明不正确。 Random failure is possible on a 64-bit version of Windows since the passed arguments have the wrong size.在 Windows 的 64 位版本上可能出现随机故障,因为通过的 arguments 的大小错误。 They should look like this:它们应该如下所示:

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

You could add a check on the return value of SendMessage(), it should return IntPtr.Zero.您可以检查 SendMessage() 的返回值,它应该返回 IntPtr.Zero。 Not so sure it is useful, it will be pretty obvious to the user that the command didn't work for some reason.不太确定它是否有用,用户很明显该命令由于某种原因不起作用。

this one works on Windows 7 using VB 2008. and dont worry about turning the monitor back on again because this is just like "turn off the display" in the power options.这个可以使用 VB 2008 在 Windows 7 上工作。不用担心再次打开显示器,因为这就像电源选项中的“关闭显示器”一样。 the monitor turns off and to turn it back on you have to either press any key on the keyboard or move your mouse.显示器关闭并重新打开,您必须按键盘上的任意键或移动鼠标。

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