如何将此 VB.NET 代码转换为 C#?
How to translate this VB.NET code to C#?
我正在尝试将此 VB.NET 代码转换为 C#,但我是 C# 新手。
这是我的代码VB.NET代码:
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" ( _
ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Private windows As New WindowInteropHelper(Me)
Public Function MakeTopMost()
SetWindowPos(windows.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function
这是我的 C# 代码:
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(string hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
public void MakeTopMost()
{
SetWindowPos(windows.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
Error in MakeTopMost(): invalid Arguments
十六进制数以0x
:
开头
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private WindowInteropHelper windows = new WindowInteropHelper(this);
public void MakeTopMost()
{
SetWindowPos(windows.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
应该不会有任何错误。
对于 MakeTopMost()
函数,您需要使用单个 |不是两个,因为两个翻译成 "OrAlso" 只能在条件中使用。这是完整的代码(从评论线程中反映出来)
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
private readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private WindowInteropHelper windows = new WindowInteropHelper(this);
public static extern IntPtr SetWindowPos(string hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
public void MakeTopMost()
{
SetWindowPos(windows.handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
我正在尝试将此 VB.NET 代码转换为 C#,但我是 C# 新手。
这是我的代码VB.NET代码:
Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" ( _
ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Shared ReadOnly HWND_TOPMOST As New IntPtr(-1)
Private Shared ReadOnly HWND_NOTOPMOST As New IntPtr(-2)
Private windows As New WindowInteropHelper(Me)
Public Function MakeTopMost()
SetWindowPos(windows.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
End Function
这是我的 C# 代码:
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(string hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
public void MakeTopMost()
{
SetWindowPos(windows.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
Error in MakeTopMost(): invalid Arguments
十六进制数以0x
:
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private WindowInteropHelper windows = new WindowInteropHelper(this);
public void MakeTopMost()
{
SetWindowPos(windows.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
应该不会有任何错误。
对于 MakeTopMost()
函数,您需要使用单个 |不是两个,因为两个翻译成 "OrAlso" 只能在条件中使用。这是完整的代码(从评论线程中反映出来)
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;
private readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private WindowInteropHelper windows = new WindowInteropHelper(this);
public static extern IntPtr SetWindowPos(string hWnd, IntPtr hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
public void MakeTopMost()
{
SetWindowPos(windows.handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}