Results 1 to 6 of 6

Thread: Have one form shown behind another form? how?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2009
    Location
    Missouri
    Posts
    770

    Have one form shown behind another form? how?

    I cant get one form to show up behind another one... How would i do this with code? I have them both as borderstyle = none... can i not have this?
    Rate my post if i helped you!


    Button Configuration Control For VB6 GetAsyncKeyState
    I'm the 7th best high school programmer in the nation!(according to SkillsUSA Nationals)(Used C#.Net)

  2. #2

  3. #3
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Have one form shown behind another form? how?

    It is easier to have one form appear in front of another form as normal drawing is from the back to the front... However, you could try the API with SetWindowPos and HWND_BOTTOMMOST const...



    Good Luck
    Option Explicit should not be an Option!

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Have one form shown behind another form? how?

    As vb5prgrmr has said you can use the SetWindowPos API. Since you are subclassing in this example, make sure you close the form properly.

    in a module
    Code:
    Option Explicit
    
    Public Declare Function SetWindowLong Lib "user32" _
        Alias "SetWindowLongA" (ByVal hwnd As Long, _
        ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function CallWindowProc Lib "user32" _
        Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
        ByVal hwnd As Long, ByVal Msg As Long, _
        ByVal wParam As Long, ByVal lParam As Long) As Long
    Public 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
    
    Public Const GWL_WNDPROC = (-4)
    Public Const WM_PAINT = &HF
    Public Const WM_ACTIVATE = &H6
    Public Const SWP_NOMOVE = &H2
    Public Const SWP_NOSIZE = &H1
    Public Const HWND_BOTTOM = 1
    
    Public winProc As Long
    
    
    Public Function WindowProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim lFlags As Long
    
        lFlags = SWP_NOSIZE Or SWP_NOMOVE
        
        Select Case wMsg
            Case WM_ACTIVATE
                SetWindowPos hwnd, HWND_BOTTOM, 0, 0, 0, 0, lFlags
            Case Else
                WindowProc = CallWindowProc(winProc, hwnd, wMsg, wParam, lParam)
                Exit Function
        End Select
    End Function
    
    Public Sub SubClass(hwnd As Long)
        On Error Resume Next
        winProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc)
    End Sub
    
    Public Sub UnSubClass(hwnd As Long)
        If winProc Then
            SetWindowLong hwnd, GWL_WNDPROC, winProc
            winProc = 0
        End If
    End Sub
    and in your form
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Unload Me
    End Sub
    
    Private Sub Form_Load()
        SubClass Me.hwnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        UnSubClass Me.hwnd
    End Sub

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Have one form shown behind another form? how?

    No subclassing required.


    In a code module:
    Code:
    Option Explicit
    
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) 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_STYLE = (-16)
    Private Const GWL_EXSTYLE = (-20)
    'Requires Windows 2000 or later:
    Private Const WS_EX_LAYERED = &H80000
    
    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    Private Declare Function GetLayeredwindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
    Private Const LWA_COLORKEY = &H1
    Private Const LWA_ALPHA = &H2
    
    Public Sub MakeWindowTransparent(ByVal hWnd As Long, ByVal alphaAmount As Byte)
        Dim lStyle As Long
        
        lStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
        lStyle = lStyle Or WS_EX_LAYERED
        SetWindowLong hWnd, GWL_EXSTYLE, lStyle
        SetLayeredWindowAttributes hWnd, 0, alphaAmount, LWA_ALPHA
    End Sub
    In the transparent form:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    frmOpaque.Move Me.Left + 100
    frmOpaque.Show
    End Sub
    
    Private Sub Form_Load()
           MakeWindowTransparent Me.hWnd, 100 'Can be 0 to 255
           Me.Show
    End Sub

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Have one form shown behind another form? how?

    Game

    I cant get one form to show up behind another one... How would i do this with code?
    If you have 2 forms, and
    Form2 is in front of Form1, then

    Code:
    Form2.Zorder 1
    will place Form2 at the back of the z-order.

    Is that what you want to do?

    Spoo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width