【问题标题】:How to translate this VB.NET code to C#?如何将此 VB.NET 代码翻译成 C#?
【发布时间】:2015-05-23 06:55:04
【问题描述】:

我正在尝试将此 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);
 }

MakeTopMost() 中的错误:参数无效

【问题讨论】:

  • 在 C# 中你不必使用0x1 而不是&H1 吗?
  • 一个十六进制常量在c#中写成0x1;
  • 哦,谢谢,我只需要翻译 MakeTopMost() 函数,我认为“或”不好
  • 你问过Roslyn yet?

标签: c# vb.net vb.net-to-c#


【解决方案1】:
    [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);
    }

它不应该出现任何错误。

【讨论】:

  • 谢谢 :D 但是,我的声明函数 SetWindowPos Lib 在我的 c# 代码中正确吗?
  • 我尝试使用您的代码,但无法识别 SetWindowPos 函数
【解决方案2】:

十六进制数字以0x开头:

private const int SWP_NOSIZE = 0x1;
private const int SWP_NOMOVE = 0x2;

【讨论】:

  • o 这项工作,但 mi 问题出在函数 MakeTopMos() 中:错误:运算符 '||'不能应用于“int”类型的操作数
  • |是 c# 中的按位或运算符。
  • 是的,我有这个 :D 但我的声明函数 SetWindowPos Lib 在我的 c# 代码中正确吗?因为我那里有错误
【解决方案3】:

对于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);
}

【讨论】:

  • 谢谢 :D 但是,我的声明函数 SetWindowPos Lib 在我的 c# 代码中是否正确?因为我那里有错误
  • 与 |你不应该得到错误,现在是另一个错误吗?
  • 是的,我的 SetWindowPos 无效参数中有错误:C
  • 你能附上这个新错误的截图吗?相信一定是windows.handle等后续值
  • 我把图片放在我的问题中:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 1970-01-01
  • 2020-12-01
  • 2017-07-24
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
相关资源
最近更新 更多