Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

[VB6] Full Screen a Form

$
0
0
This class was converted from the Chromium project.

CFullScreenHandler.cls
Code:

' Chromium full_screen_handler.h/.cc converted to VB6
' CFullScreenHandler.cls
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' WIN32 API
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private Declare Function IsZoomed Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND As Long = &H112
Private Const SC_RESTORE As Long = &HF120&
Private Const SC_MAXIMIZE As Long = &HF030&
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongW" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Const GWL_STYLE As Long = -16
Private Const GWL_EXSTYLE As Long = -20
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongW" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Const WS_CAPTION As Long = &HC00000
Private Const WS_THICKFRAME As Long = &H40000
Private Const WS_EX_DLGMODALFRAME As Long = &H1&
Private Const WS_EX_WINDOWEDGE As Long = &H100&
Private Const WS_EX_CLIENTEDGE As Long = &H200&
Private Const WS_EX_STATICEDGE As Long = &H20000
Private Type MONITORINFO
    cbSize As Long
    rcMonitor As RECT
    rcWork As RECT
    dwFlags As Long
End Type
Private Declare Function MonitorFromWindow Lib "user32" (ByVal hWnd As Long, ByVal dwFlags As Long) As Long
Private Declare Function GetMonitorInfo Lib "user32" Alias "GetMonitorInfoW" (ByVal hMonitor As Long, lpmi As Any) As Long
Private Const MONITOR_DEFAULTTONEAREST As Long = &H2
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 uFlags As Long) As Long
Private Const SWP_HIDEWINDOW As Long = &H80
Private Const SWP_NOMOVE As Long = &H2
Private Const SWP_NOREPOSITION As Long = &H200
Private Const SWP_NOOWNERZORDER As Long = &H200
Private Const SWP_NOSIZE As Long = &H1
Private Const SWP_NOZORDER As Long = &H4
Private Const SWP_NOACTIVATE As Long = &H10&
Private Const SWP_FRAMECHANGED As Long = &H20

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Type SavedWindowInfo
    Maximized  As Boolean
    Style      As Long
    ExStyle    As Long
    WindowRect  As RECT
End Type

Private WithEvents m_Parent As Form
Private m_hWnd              As Long
Private m_FullScreen        As Boolean
Private m_MetroSnap        As Boolean
Private m_SavedWindowInfo  As SavedWindowInfo

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Class Implementation
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Initialize()
    m_hWnd = GetActiveWindow()
End Sub

Public Function Init(Parent As Form, Optional HandleEscapeF11 As Boolean) As CFullScreenHandler
    If HandleEscapeF11 Then Set m_Parent = Parent
    m_hWnd = Parent.hWnd
    Set Init = Me
End Function

Public Property Let hWnd(Value As Long)
    m_hWnd = Value
End Property

Public Property Get FullScreen() As Boolean
    FullScreen = m_FullScreen
End Property

Public Property Let FullScreen(Value As Boolean)
    If (m_FullScreen = Value) Then Exit Property
    Call SetFullscreenImpl(Value, False)
End Property

Public Property Get MetroSnap() As Boolean
    MetroSnap = m_MetroSnap
End Property

Public Property Let MetroSnap(Value As Boolean)
    If (m_MetroSnap = Value) Then Exit Property
    Call SetFullscreenImpl(Value, True)
    m_MetroSnap = Value
End Property

Private Sub SetFullscreenImpl(ByVal FullScreen As Boolean, ByVal ForMetro As Boolean)
    'ScopedFullscreenVisibility visibility(hwnd_);  'Chrome's Multiple FullScreen handling not Implemented!

    ' Save current window state if not already fullscreen.
    If (Not m_FullScreen) Then
        ' Save current window information.  We force the window into restored mode
        ' before going fullscreen because Windows doesn't seem to hide the
        ' taskbar if the window is in the maximized state.
        m_SavedWindowInfo.Maximized = IsZoomed(m_hWnd)
       
        If (m_SavedWindowInfo.Maximized) Then _
            Call SendMessage(m_hWnd, WM_SYSCOMMAND, SC_RESTORE, ByVal 0&)
        m_SavedWindowInfo.Style = GetWindowLong(m_hWnd, GWL_STYLE)
        m_SavedWindowInfo.ExStyle = GetWindowLong(m_hWnd, GWL_EXSTYLE)
        Call GetWindowRect(m_hWnd, m_SavedWindowInfo.WindowRect)
    End If
   
    m_FullScreen = FullScreen
   
    If (m_FullScreen) Then
        ' Set new window style and size.
        Call SetWindowLong(m_hWnd, GWL_STYLE, _
                          m_SavedWindowInfo.Style And Not (WS_CAPTION Or WS_THICKFRAME))
        Call SetWindowLong(m_hWnd, GWL_EXSTYLE, _
                          m_SavedWindowInfo.ExStyle And Not (WS_EX_DLGMODALFRAME Or _
                          WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE Or WS_EX_STATICEDGE))
                         
        ' On expand, if we're given a window_rect, grow to it, otherwise do
        ' not resize.
        If (Not ForMetro) Then
            Dim mi As MONITORINFO
            mi.cbSize = LenB(mi)
            Call GetMonitorInfo(MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTONEAREST), mi)
            With mi.rcMonitor
            Call SetWindowPos(m_hWnd, NULL_, .Left, .Top, .Right - .Left, .Bottom - .Top, _
                              SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_FRAMECHANGED)
            End With
        End If
    Else
        ' Reset original window style and size.  The multiple window size/moves
        ' here are ugly, but if SetWindowPos() doesn't redraw, the taskbar won't be
        ' repainted.  Better-looking methods welcome.
        Call SetWindowLong(m_hWnd, GWL_STYLE, m_SavedWindowInfo.Style)
        Call SetWindowLong(m_hWnd, GWL_EXSTYLE, m_SavedWindowInfo.ExStyle)
       
        If (Not ForMetro) Then
            ' On restore, resize to the previous saved rect size.
            With m_SavedWindowInfo.WindowRect
            Call SetWindowPos(m_hWnd, NULL_, .Left, .Top, .Right - .Left, .Bottom - .Top, _
                              SWP_NOZORDER Or SWP_NOACTIVATE Or SWP_FRAMECHANGED)
            End With
        End If
        If (m_SavedWindowInfo.Maximized) Then _
            Call SendMessage(m_hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, ByVal 0&)
    End If
End Sub

Private Sub Class_Terminate()
    Set m_Parent = Nothing
End Sub

Private Sub m_Parent_KeyUp(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
    Case vbKeyEscape:  FullScreen = False
    Case vbKeyF11:      FullScreen = Not FullScreen
    End Select
End Sub

Form Code looks like this
Code:

Private m_fs    As New CFullScreenHandler

Private Sub Form_Initialize()
    m_fs.Init Me, True
End Sub


Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>