Results 1 to 2 of 2

Thread: Minimize WCF Console On Start Up

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Feb 2016

    Minimize WCF Console On Start Up

    The following code does minimize as expected while in Visual Studio how ever after compiling and running the complied code from desktop it does not. Any thoughts of why

    HTML Code:
    Imports System.ComponentModel
    Imports System.Net
    Imports System.Runtime.InteropServices
    Imports System.ServiceModel
    Imports System.ServiceModel.Description
    Imports System.Windows.Forms
    'Imports ServerConsole.HostModule
    Public Module NotifyIcon1    
        <DllImport("user32.dll", SetLastError:=True)>
        Private Function SetParent(hWndChild As IntPtr, hWndNewParent As IntPtr) As IntPtr
        End Function    <DllImport("user32.dll")>
        Private Function GetShellWindow() As IntPtr
        End Function    <DllImport("user32.dll")>
        Private Function GetDesktopWindow() As IntPtr
        End Function    Private ViewConsoleState As WindowShowStyle
        Private Enum WindowShowStyle As UInteger        
            Hide = 0
            ShowNormal = 1
            ShowMinimized = 2
            ShowMaximized = 3
            Maximize = 3
            ShowNormalNoActivate = 4
            Show = 5
            Minimize = 6
            ShowMinNoActivate = 7
            ShowNoActivate = 8
            Restore = 9
            ShowDefault = 10
            ForceMinimized = 11    End Enum
        Dim WithEvents notifyIcon As NotifyIcon
        Dim processHandle As IntPtr
        Dim WinShell As IntPtr
        Dim WinDesktop As IntPtr
        Dim HideMenu As MenuItem
        Dim RestoreMenu As MenuItem
        Dim RestartMenu As MenuItem
        'Dim MinimizeMenu As MenuItem
        Sub Main() 
            notifyIcon = New NotifyIcon()
            notifyIcon.Icon = My.Resources.MangagedMonitoringConsole
            notifyIcon.Text = "TattleTale External Monitor Running" & Environment.NewLine & GetLocalIPAddress()
            notifyIcon.Visible = True        Dim menu As New ContextMenu()
            HideMenu = New MenuItem("Hide Console", New EventHandler(AddressOf Minimize_Click))
            RestoreMenu = New MenuItem("Show Console", New EventHandler(AddressOf Maximize_Click))
            'MinimizeMenu = New MenuItem("Minimize Image Server", New EventHandler(AddressOf MinimizeConsole))        menu.MenuItems.Add(RestoreMenu)
            menu.MenuItems.Add(New MenuItem("Restart Image Server", New EventHandler(AddressOf RestartServer)))
            menu.MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf CleanExit)))
            notifyIcon.ContextMenu = menu
            'You need to spin off your actual work in a different thread so that the Notify Icon works correctly
            Task.Factory.StartNew(AddressOf Run)
            processHandle = Process.GetCurrentProcess().MainWindowHandle
            WinShell = GetShellWindow()
            WinDesktop = GetDesktopWindow()        'Hide the Window
            'ViewConsoleState = WindowShowStyle.ShowMinimized
            'This is required for triggering WinForms activity in Console app
          End Sub
        Private Sub Run()
                Dim uri = New Uri("http://" & GetLocalIPAddress() & ":8080/hello")        Dim baseAddress As Uri = New Uri("http://" & GetLocalIPAddress() & ":8080/hello")        Try
                Dim Host As New ServiceHost(GetType(WcfServiceLibrary1.Service1), baseAddress)
                Dim smb As New ServiceMetadataBehavior()
                smb.HttpGetEnabled = True
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
                Console.WriteLine("The service is ready at {0}", baseAddress)
                Console.WriteLine("Press <Enter> to stop the service.")
                Console.WriteLine("Press minimize button to hide console.")
                ' Close the ServiceHost.
                MinimizeConsole()        Catch commProblem As CommunicationException
                'There is already a WCF instance Running
                notifyIcon.Visible = False
                MsgBox("Image Server Is Already Running")
                'Console.WriteLine("There was a communication problem. " + commProblem.Message)
            End Try        While True
            End While
        End Sub
        Private Sub CleanExit(sender As Object, e As EventArgs)
            notifyIcon.Visible = False
        End Sub
        Private Sub Minimize_Click(sender As Object, e As EventArgs)
        End Sub    Private Sub Maximize_Click(sender As Object, e As EventArgs)
            ViewConsoleState = WindowShowStyle.Show
        End Sub
        Private Sub ResizeWindow(Optional Restore As Boolean = True)
            If Restore Then
                RestoreMenu.Enabled = False
                HideMenu.Enabled = True
                SetParent(processHandle, WinDesktop)
                RestoreMenu.Enabled = True
                HideMenu.Enabled = False
                SetParent(processHandle, WinShell)
            End If
        End Sub
        Private Sub RestartServer()
                notifyIcon.Visible = False
            Catch ex As Exception
            End Try    End Sub
        Private Sub MinimizeConsole()
            ViewConsoleState = WindowShowStyle.Hide
        End Sub
        Private Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As WindowShowStyle) As Boolean
            Return ShowWindow
        End Function
        Public Function GetLocalIPAddress() As String
            Dim _IP As String = Nothing
            Dim _IPHostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())        ' IPAddress class contains the address of a computer on an IP network. 
            For Each _IPAddress As System.Net.IPAddress In _IPHostEntry.AddressList
                ' InterNetwork indicates that an IP version 4 address is expected 
                ' when a Socket connects to an endpoint
                If _IPAddress.AddressFamily.ToString() = "InterNetwork" Then
                    _IP = _IPAddress.ToString()
                End If
            Next _IPAddress
            Return _IP
        End Function
    End Module

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Quote Originally Posted by documentation
    A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.
    Use GetConsoleWindow function to get the handle: getconsolewindow (kernel32)
    [xcode=vb] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017


Posting Permissions

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