Link to home
Start Free TrialLog in
Avatar of SIMON-VERMOT Christophe
SIMON-VERMOT Christophe

asked on

Notifyicon and dynamic menu

Hello,
Sorry for my very limited English, i'm french.

Having a concern with a security dll provided by an external company, it responds too slowly in VB.net, 30 to 50 seconds which is unacceptable.

I am trying to create an application that would be hidden at boot time with a notifyicon and a dynamically create menu. This software would be the security management of our other software.

I then need my other software to interoperate the security dll management software, to allow the application to start and retrieve the access levels. (user / super user / admin)

I manage to manage the security part dll. My software recover all the information.

But for now I am trying to overcome two problems:
1) The dynamic creation of the notifyicon and the contextmenustrip. I can create the notifyicon, but the menu does not appear.
2) The dialogue between the 2 software. I found a tutorial to use remoting. It works perfectly, but from a console application, hence the dynamic creation of the notifyicon and contextmenustrip.

I will need a form in my management application. So I tested by creating a form with the notifyicon and the contextmenustrip.
I instantiate the form, without displaying it, and I add a menustrip to my contextMenu, but again I have the icon, but not the menu.

Code below:
 Public Shared Sub Main(ByVal args() As String)
        Try
            ' Création d'un nouveau canal d'écoute sur le port 1069
            Dim channel As TcpChannel = New TcpChannel(1069)
            ' Enregistrement du canal
            ChannelServices.RegisterChannel(channel)
            ' Démarrage de l'écoute en exposant l'objet en Singleton
            RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemoteOperation), "RemoteOperation", WellKnownObjectMode.Singleton)
            Console.WriteLine("Le serveur a démarré avec succès")
            FormMain = New frmMain
 
            If DetectionLicence(tpSoftWare.WinSparc) = 0 Then
                MenuWinsparc = New ToolStripMenuItem
                MenuWinsparc.Font = New Font(FontFamily.GenericSerif, 10, FontStyle.Regular)
                MenuWinsparc.Text = "WinSparc:" & mProdkey(tpSoftWare.WinSparc) & "|" & mExpireDate(tpSoftWare.WinSparc)
                FormMain.ContextMenuStrip1.Items.Add(MenuWinsparc)
            End If

            FormMain.NotifyIcon1.Visible = True

            mtime = New System.Threading.Timer(AddressOf mTimerTick, Nothing, 60000, 60000)

            Console.ReadLine()
        Catch
            Console.WriteLine("Erreur lors du démarrage du serveur")
            Console.ReadLine()
        End Try

    End Sub

Open in new window

Currently I have the console that starts, but in the end it will not console mode.
Avatar of it_saige
it_saige
Flag of United States of America image

Personally I would create a Windows Form App project as opposed to a Console App project.  That being said, it would probably be easier to use an application context (so you don't have to deal with any form hiding/showing).  Proof of concept -
MainContext.vb -
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms

Public Class MainContext
	Private Sub OnApplicationExit(sender As Object, e As EventArgs)
		NotifyIcon1.Visible = False
	End Sub

	Private Sub OnClick(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
		Application.Exit()
	End Sub

	Private Sub OnDoubleClick(sender As Object, e As EventArgs) Handles NotifyIcon1.DoubleClick
		NotifyIcon1.ShowBalloonTip(10000)
	End Sub

	Public Sub New()

		' This call is required by the designer.
		AddHandler Application.ApplicationExit, AddressOf OnApplicationExit
		InitializeComponent()
		Dim asm = Assembly.GetExecutingAssembly()
		Dim stream = asm.GetManifestResourceStream("EE_Q29077916.1_E1f_icon.ico")
		NotifyIcon1.Icon = New Icon(stream)
		NotifyIcon1.Visible = True
	End Sub
End Class

Open in new window

MainContext.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class MainContext
	Inherits System.Windows.Forms.ApplicationContext

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()>
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	<System.Diagnostics.DebuggerStepThrough()>
	Private Sub InitializeComponent()
		Me.components = New System.ComponentModel.Container()
		Me.ContextMenuStrip1 = New System.Windows.Forms.ContextMenuStrip(Me.components)
		Me.ExitToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
		Me.NotifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)
		Me.ContextMenuStrip1.SuspendLayout()
		'
		'ContextMenuStrip1
		'
		Me.ContextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.ExitToolStripMenuItem})
		Me.ContextMenuStrip1.Name = "ContextMenuStrip1"
		Me.ContextMenuStrip1.Size = New System.Drawing.Size(93, 26)
		'
		'ExitToolStripMenuItem
		'
		Me.ExitToolStripMenuItem.Name = "ExitToolStripMenuItem"
		Me.ExitToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
		Me.ExitToolStripMenuItem.Text = "Exit"
		'
		'NotifyIcon1
		'
		Me.NotifyIcon1.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info
		Me.NotifyIcon1.BalloonTipText = "This is a test"
		Me.NotifyIcon1.BalloonTipTitle = "Testing"
		Me.NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
		Me.NotifyIcon1.Text = "NotifyIcon1"
		'
		'frmMain
		'
		Me.ContextMenuStrip1.ResumeLayout(False)
	End Sub
	Friend WithEvents ExitToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
	Friend WithEvents ContextMenuStrip1 As System.Windows.Forms.ContextMenuStrip
	Friend WithEvents NotifyIcon1 As System.Windows.Forms.NotifyIcon
End Class

Open in new window

Module1.vb -
Imports System.Drawing
Imports System.Windows.Forms

Module Module1

	Sub Main()
		Try
			Application.EnableVisualStyles()
			Application.SetCompatibleTextRenderingDefault(False)
			'' Création d'un nouveau canal d'écoute sur le port 1069
			'Dim channel As TcpChannel = New TcpChannel(1069)
			'' Enregistrement du canal
			'ChannelServices.RegisterChannel(channel)
			'' Démarrage de l'écoute en exposant l'objet en Singleton
			'RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemoteOperation), "RemoteOperation", WellKnownObjectMode.Singleton)
			'Console.WriteLine("Le serveur a démarré avec succès")
			'FormMain = New frmMain

			'If DetectionLicence(tpSoftWare.WinSparc) = 0 Then
			'	MenuWinsparc = New ToolStripMenuItem
			'	MenuWinsparc.Font = New Font(FontFamily.GenericSerif, 10, FontStyle.Regular)
			'	MenuWinsparc.Text = "WinSparc:" & mProdkey(tpSoftWare.WinSparc) & "|" & mExpireDate(tpSoftWare.WinSparc)
			'	FormMain.ContextMenuStrip1.Items.Add(MenuWinsparc)
			'End If
			Dim Context As MainContext = New MainContext()
			Dim MenuWinsparc = New ToolStripMenuItem With {
				.Font = New Font(FontFamily.GenericSerif, 10, FontStyle.Regular),
				.Text = "WinSparc"
			}
			Context.ContextMenuStrip1.Items.Add(MenuWinsparc)
			Context.NotifyIcon1.Visible = True
			Application.Run(Context)
		Catch ex As Exception
			Console.WriteLine("Erreur lors du démarrage du serveur")
		End Try
	End Sub

End Module

Open in new window

Which produces the following output -User generated image
You will want to include your own icon embedded resource and change line 24 in MainContext.vb -
Dim stream = asm.GetManifestResourceStream("<NameSpace>.<ResourceName>")

Open in new window


-saige-
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.