代码之家  ›  专栏  ›  技术社区  ›  Codename K

VB6-如何使窗体排在第二位?

  •  1
  • Codename K  · 技术社区  · 6 年前

    我使用了以下VB6代码,

    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
    Private Const HWND_TOPMOST = -1
    Private Const HWND_NOTOPMOST = -2
    Private Const SWP_NOACTIVATE = &H10
    Private Const SWP_SHOWWINDOW = &H40
    Private Const SWP_NOMOVE = 2
    Private Const SWP_NOSIZE = 1
    
    Private Sub Form_Activate()
        Dim R As Long
        R = SetWindowPos(frmSlide.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE)
    End Sub
    

    表单设置了这些属性,

       MaxButton       =   False
       MinButton       =   False
       ShowInTaskbar   =   False
       StartUpPosition =   CenterScreen
       WindowState     =   Maximized
    

    这是为了使窗体转到后台。它没有进入后台。这里的想法是使表单只返回一个窗口。例如:如果记事本程序窗口打开。此程序只需要在记事本的背景中,而不需要在其他程序窗口中。这可能吗?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Codename K    6 年前

    我根据 Tarun Lalwani information 这对我来说很有用,

    添加一个 Timer 并使用此代码,

    Option Explicit
    
    Private Declare Function FindWindow1 Lib "User32" Alias "FindWindowA" (ByVal lpclassname As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Const GWL_HWNDPARENT = -8
    
    Private parenthwnd As Long
    Private strTitle As String
    
    Public Function FindWindowHandle(Caption As String) As Long
      FindWindowHandle = FindWindow1(vbNullString, Caption)
    End Function
    
    Private Sub Form_Load()
        On Error Resume Next
        strTitle = "Untitled - Notepad"
    
        With Timer1
            .Interval = 2000
            .Enabled = True
        End With
    End Sub
    
    Private Sub Timer1_Timer()
        If FindWindowHandle(strTitle) <> 0 Then
            Timer1.Enabled = False
            parenthwnd = 0
            parenthwnd = FindWindow1(vbNullString, strTitle)
            Dim R As Long
            R = SetWindowLong(parenthwnd, GWL_HWNDPARENT, Me.hWnd)
        End If
    End Sub
    

    当记事本打开时,它将是此表单的父级。

    警告: 我已将表单属性设置为,

       MaxButton       =   False
       MinButton       =   False
       ShowInTaskbar   =   False
       StartUpPosition =   CenterScreen
       WindowState     =   Maximized
    

    如果设置了相同的属性,请确保添加按钮或任何其他方法来关闭表单。否则,表单将位于顶部,可能很难将其关闭。

        2
  •  0
  •   Tarun Lalwani    6 年前

    是的,你可以这样做。您需要做的是找到外部程序的窗口句柄,然后告诉windows您的窗体应被视为父窗体

    SetWindowLong(hwndChild, GWL_HWNDPARENT, hwndOwner)
    

    PS:学分到 https://stackoverflow.com/a/834509/2830850

    也可参见下面的SO线程

    Win32 window Owner vs window Parent?