Overriding the "X" control and using WndProc

P

Phuff

I have an application that should run in the system tray while open.
It is supposed to be open at all times and I need it to dissapear when
the "X" button is pushed on the form...but without closing the app.

I've tried overriding the closing event
Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
'Don't close this form
e.Cancel = True
Me.Hide()


End Sub

but when you try to shutdown windows or reboot it will cancel the event
and windows can't shutdown without closing my app first.

So I tried to override the WndProc() method and catch the message for a
system shutdown.

<code>
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand,
Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As
System.Windows.Forms.Message)
' Listen for operating system messages
Select Case (m.Msg)
Case WM_QUERYENDSESSION
Application.Exit()
End Select


' Handle the message
MyBase.WndProc(m)
End Sub
</code>

This will close my app while windows is Shutting down/rebooting but it
will still cancel the shut down process. I've been looking everywhere
and can't seem to find how to do this. I tried using WndProc to catch
the message sent by the "X" button and did so successfully, but it
would still close the app.

Any help would be greatly appreciated!!

Paul Huff
 
J

Jay B. Harlow [MVP - Outlook]

Phuff,
Matthew MacDonald's book "Microsoft Visual Basic .NET Programmer's Cookbook"
from MS Press has a topic on creating a system tray program.

Basically: Rather than using a Form as the startup object, use a Component
instead.

Create a new Component class (use Project - Add Component). Add a NotifyIcon
to the component designer. Also add a ContextMenu object for the
NotifyIcon. When you click the menu, create and show the form. Remember to
put an Exit option on the menu. Then you can let the form close normally,
the component itself is the startup item, so your app continues to run.

Make the Component the startup object, adding a Shared Sub Main to the
component.

Public Class Component1
Inherits System.ComponentModel.Component

' Component designer generated code omitted.

Public Shared Sub Main
Dim app as New Component1
Application.Run()
End Sub

Private Sub menuOptions_Click(...) Handles menuOptions.Click
' this would be your settings dialog.
Dim dialog as New OptionsDialog
dialog.ShowDialog()
dialog.Dispose()
End Sub

Private Sub menuExit_Click(...) Handles menuExit.Click
Me.Dispose()
Application.Exit()
End Sub

End Sub

The problem is you cannot edit the menu from the Component Designer. What I
do is use cut & paste from a Form Designer onto the Component Designer to
get the menu to the component...

The call to Me.Dispose enables the icon to be removed from the system tray
right away rather then waiting.

FWIW: You can use Microsoft.Win32.SystemEvents.SessionEnding & SessionEnded
to identify when the system is shutting down.

Hope this helps
Jay


|I have an application that should run in the system tray while open.
| It is supposed to be open at all times and I need it to dissapear when
| the "X" button is pushed on the form...but without closing the app.
|
| I've tried overriding the closing event
|
| > Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As
| > System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
| > 'Don't close this form
| > e.Cancel = True
| > Me.Hide()
| >
| >
| > End Sub
|
| but when you try to shutdown windows or reboot it will cancel the event
| and windows can't shutdown without closing my app first.
|
| So I tried to override the WndProc() method and catch the message for a
| system shutdown.
|
| <code>
|
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand,
| Name:="FullTrust")> _
| Protected Overrides Sub WndProc(ByRef m As
| System.Windows.Forms.Message)
| ' Listen for operating system messages
| Select Case (m.Msg)
| Case WM_QUERYENDSESSION
| Application.Exit()
| End Select
|
|
| ' Handle the message
| MyBase.WndProc(m)
| End Sub
| </code>
|
| This will close my app while windows is Shutting down/rebooting but it
| will still cancel the shut down process. I've been looking everywhere
| and can't seem to find how to do this. I tried using WndProc to catch
| the message sent by the "X" button and did so successfully, but it
| would still close the app.
|
| Any help would be greatly appreciated!!
|
| Paul Huff
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top