注册 登录
编程论坛 VB.NET论坛

高手看看下列VC代码能否改成VB代码

guchew 发布于 2016-09-06 14:59, 3024 次点击
VC代码:
if(hWnd)
        {
            HWND hForeWnd = ::GetForegroundWindow();
            DWORD dwForeID = ::GetWindowThreadProcessId(hForeWnd,NULL);
            DWORD dwCurID = ::GetCurrentThreadId();
            ::AttachThreadInput(dwCurID,dwForeID,TRUE);
            ::ShowWindow(hWnd,SW_SHOWNORMAL);
            ::SetWindowPos(hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
            ::SetWindowPos(hWnd,HWND_NOTOPMOST,0,0,0,0, SWP_NOSIZE|SWP_NOMOVE);
            ::SetForegroundWindow(hWnd);
            ::AttachThreadInput(dwCurID,dwForeID,FALSE);
            return 0;
        }
自己改写的运行出错:
        Dim hForeWnd As Integer = GetForegroundWindow()
        Dim dwForeID As Integer = GetWindowThreadProcessId(hForeWnd, 0)
        Dim dwCurID As Integer = GetCurrentThreadId()
        AttachThreadInput(dwCurID, dwForeID, 1)
        ShowWindow(hWnd, SW_SHOWNORMAL)
        SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE And SWP_NOMOVE)
        SetWindowPos(hWnd, HWND_NoTopmost, 0, 0, 0, 0, SWP_NOSIZE And SWP_NOMOVE)
        SetForegroundWindow(hWnd)
        AttachThreadInput(dwCurID, dwForeID, 0)
3 回复
#2
不说也罢2016-09-06 17:14
初看,楼主转换的应该没有问题。你把所有的integer替换成Long试试,
楼主没有说明出错的代码行及出错原因,又没有代码的上下文,无法能帮上你。
楼主检查下声明中的参数类型及返回值的类型。定义的变量类型以及代码中的常量要与参数类型一致。
#3
梦幻倩影2016-09-07 15:16
上面几个函数好象不是自带的,据我了解比如GetForegroundWindow()应该是user32.dll中的,如要引用得先处理一下:
Imports System.Runtime.InteropServices
    <DllImport("user32.dll", SetLastError:=True)> _
    Private Shared Function GetForegroundWindow() As IntPtr
    End Function

然后再:
Dim hForeWnd As Integer = GetForegroundWindow()
#4
guchew2016-11-07 11:35
问题找到了,SWP_NOSIZE And SWP_NOMOVE错了,应为:SWP_NOSIZE + SWP_NOMOVE
1