【发布时间】:2011-06-03 00:15:32
【问题描述】:
如何使用 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
}
更新:
【问题讨论】:
-
转换器可能会抱怨,因为此代码中没有类构造。尝试包装从 using 语句之后到最后一行之后的所有内容:
class MonitorShutdown { ...code here }。 -
请注意,C# 代码和翻译后的代码都相当糟糕。
hWnd应该是IntPtr,而不是int。并且在代码中使用魔法常数 (2) 也不是一个好主意。 -
@Konrad Rudolph:我不知道 C# 或 Win32 API。如果您可以完善答案,请执行此操作。它将帮助我和任何未来的访客。
-
试试 codechanger.com - 我删除了代码 cmets 并且它完美地转换了;)
-
关于检查当前监视器状态,这可能与stackoverflow.com/questions/203355/…有关
标签: c# .net vb.net visual-studio-2010 screen