永发信息网

VB窗口最大化前置

答案:2  悬赏:60  手机版
解决时间 2021-03-21 17:36
  • 提问者网友:浮克旳回音
  • 2021-03-21 00:05
A.EXE已经窗口最大化,但被其他程序窗口遮挡置底,VB怎么才能使其窗口最大化前置?求代码!谢谢!
最佳答案
  • 五星知识达人网友:猎心人
  • 2021-03-21 01:04
需要用到API函数SetWindowPos,还是以记事本为例:
Option Explicit
Private Declare Function ShowWindow Lib "user32" (ByVal HWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal HWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Const SW_HIDE = 0
Const SW_SHOWNORMAL = 1
Const SW_SHOWMINIMIZED = 2
Const SW_SHOWMAXIMIZED = 3
Const HWND_TOP = 0
Const HWND_TOPMOST = -1
Const SWP_NOACTIVATE = &H10 '不激活窗口
Const SWP_NOMOVE = &H2 '保持当前位置 (x和y设定将被忽略)
Const SWP_NOREDRAW = &H8 '窗口不自动重画
Const SWP_NOSIZE = &H1 '保持当前大小 (cx和cy会被忽略)
Const SWP_NOZORDER = &H4 '保持窗口在列表的当前位置 (hWndInsertAfter将被忽略)
Const SWP_SHOWWINDOW = &H40 '显示窗口

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal HWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal HWnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal HWnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2
' 通过进程ID获得该进程的窗口句柄
Public Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long
    Dim test_pid As Long
    Dim test_thread_id As Long
    InstanceToWnd = 0
    'On Error Resume Next
    ' 获得首个handle.
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)

    ' 循环查找直到找到为给定进程ID的窗口句柄
    Do While test_hwnd <> 0
        '检查窗口句柄是否为顶级窗口
        If GetParent(test_hwnd) = 0 Then
            ' 是顶级窗口
            ' 取该窗口所属的进程ID
            test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)

            If test_pid = target_pid Then
                ' 是我们指定进程的窗口,则将该窗口的句柄返回到函数名,并退出
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If

        ' 取下一个窗口的句柄
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Function

Private Sub Command1_Click()
    Dim WinWnd As Long, PID As Long, pro, obj
    Set pro = GetObject("winmgmts:\\.\root\cimv2").execquery("select * from win32_process")
    For Each obj In pro
        Debug.Print obj.Name
        If obj.Name = "NOTEPAD.EXE" Then
            PID = obj.processid
            Exit For
        End If
    Next
    WinWnd = InstanceToWnd(PID)
'    WinWnd = FindWindow(vbNullString, "无标题 - 记事本")
    If WinWnd <> 0 Then

        ShowWindow WinWnd, SW_SHOWMAXIMIZED '激活窗口并最大化
        SetWindowPos WinWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_SHOWWINDOW '窗口置顶
    End If
End Sub
全部回答
  • 1楼网友:痴妹与他
  • 2021-03-21 02:32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 ‘新建模块,将以下代码复制到其中: optionexplicit publicdeclarefunctiongetwindowlong lib"user32"alias"getwindowlonga"(byvalhwnd aslong, byvalnindex aslong) aslong privatedeclarefunctionsetwindowlong lib"user32"alias"setwindowlonga"(byvalhwnd aslong, byvalnindex aslong, byvaldwnewlong aslong) aslong privatedeclarefunctionsetlayeredwindowattributes lib"user32"(byvalhwnd aslong, byvalcrkey aslong, byvalbalpha asbyte, byvaldwflags aslong) aslong privatedeclarefunctionsetwindowpos lib"user32"(byvalhwnd aslong, byvalhwndinsertafter aslong, byvalx aslong, byvaly aslong, byvalcx aslong, byvalcy aslong, byvalwflags aslong) aslong privateconstlwa_alpha = &h2 privateconstgwl_exstyle = (-20) privateconstws_ex_layered = &h80000 privateconstws_ex_transparent aslong= &h20& privateconstswp_nosize& = &h1 privateconstswp_nomove& = &h2 privateconsthwnd_topmost = -1 privateconsthwnd_notopmost = -2 privateconsthwnd_broadcast = &hffff& privateconsthwnd_top = 0 publicsubontop(byvaltform asform, optionalbyvaltop asboolean= true, optionaltouming aslong= 255, optionalcmouse asboolean= false) '************************************************************************* '**参 数 名:top '**说    明:是否将窗体放到z轴最上层 '**参 数 名:touming '**说    明:窗体透明度(win2000及以上有效) '**参 数 名:cmouse '**说    明:鼠标是否能穿透 '*************************************************************************     dimret aslong     ret = getwindowlong(tform.hwnd, gwl_exstyle)     ret = ret orws_ex_layered     ifcmouse thenret = ret orws_ex_transparent     setwindowlong tform.hwnd, gwl_exstyle, ret     iftouming     iftouming > 255 thentouming = 255     setlayeredwindowattributes tform.hwnd, 0, touming, lwa_alpha     iftop = truethen       setwindowpos tform.hwnd, hwnd_topmost, 0, 0, 0, 0, swp_nomove orswp_nosize     else       setwindowpos tform.hwnd, hwnd_notopmost, 0, 0, 0, 0, swp_nomove orswp_nosize     endif endsub '调用方法:在窗体中 privatesubform_load() ontop me, true endsub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯