Module modMain

    Sub Main(ByVal args As String())

        Dim frmMain As New frmMain
        Dim frmOpt As New frmOptions
        Dim frmPrv As New frmPreview


            'Make sure the screen saver is not already running. This can happen
            If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then Application.Exit()

            'Command Line Arguments
            Dim gOpenArgs() As String = Environment.GetCommandLineArgs()

            'get our arguments
            Select Case gOpenArgs.Length
                Case 0, 1  'Started from IDE or Explorer
                    frmMain.sDisplay = gOpenArgs
                Case Else
                    Select Case Mid(gOpenArgs(1).ToLower, 1, 2)
                        Case "/p"   'Preview
                            If gOpenArgs.Length > 1 Then
                                SetForm(frmPrv, gOpenArgs(2))
                                frmMain.sDisplay = gOpenArgs
                            End If
                        Case "/c"   'Configure
                        Case "/s"   'Normal
                        Case Else
                            frmMain.sDisplay = gOpenArgs
                    End Select
            End Select

        Catch Ex As Exception
            MessageBox.Show(Ex.ToString, "Error Loading Screen Saver", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub SetForm(ByRef frm As Form, ByRef arg As String)

        Dim style As Integer
        Dim previewHandle As Integer = Int32.Parse(CType(arg, String))
        Dim clsAPI As New clsWinAPI
        Dim R As New clsWinAPI.RECT

        'get dimensions of preview window
        clsAPI.GetClientRect(previewHandle, R)

        With frm
            .WindowState = FormWindowState.Normal
            .FormBorderStyle = FormBorderStyle.None
            .Width = R.right
            .Height = R.bottom
        End With

        'get and set new window style
        style = clsAPI.GetWindowLong(frm.Handle.ToInt32, clsAPI.GWL_STYLE)
        style = style Or clsAPI.WS_CHILD
        clsAPI.SetWindowLong(frm.Handle.ToInt32, clsAPI.GWL_STYLE, style)

        'set parent window (preview window)
        clsAPI.SetParent(frm.Handle.ToInt32, previewHandle)

        'save preview in forms window structure
        clsAPI.SetWindowLong(frm.Handle.ToInt32, clsAPI.GWL_HWNDPARENT, previewHandle)
        clsAPI.SetWindowPos(frm.Handle.ToInt32, 0, R.left, 0, R.right, R.bottom, clsAPI.SWP_NOACTIVATE Or clsAPI.SWP_NOZORDER Or clsAPI.SWP_SHOWWINDOW)

    End Sub

End Module

Project Homepage: