.net Tray Applications and Shut Downs

by Andrew Jackson 18. June 2004 10:49

Had a tricky problem today whilst working on my wallpaper application.

The program resides in the tray and has a settings form that I don't want to really quit the application when closed, just hide the window and still sit in the tray.  The trouble was I wanted to respond to windows shut down requests and actually quit the application, without the following fix my application cancelled the windows shutdown!

After much searching I came across the handy WM_QueryEndSession message.  The code snippet below shows it's use for detecting a true windows shut down rather than just a close of the form.

Private Shared WM_QUERYENDSESSION As Integer = &H11 Private Shared systemShutdown As Boolean = False Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_QUERYENDSESSION Then systemShutdown = True End If ' If this is WM_QUERYENDSESSION, the closing event should be fired in the base WndProc MyBase.WndProc(m) End Sub 'WndProc Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If Not systemShutdown Then Me.Visible = False e.Cancel = True Else e.Cancel = False End If End Sub

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | VB.net

Saving Settings

by Andrew Jackson 3. June 2004 10:52

It's always a problem giving users settings they can change.. where exactly should you save these... first it was win.ini, then the registry and now it's the users application data folder.

This new place has the advantages that it's user specific and relatively secure and it's easy to see/edit/move to another machine.

.net has Application.UserAppDataPath which returns the current recommended path for application data for this application.  Unfortunately it also appends the current version number of the application, which although good for large applications where versions may change radically, for minor revisions it's annoying in that every new build would effectively loose users settings and they'd have to set it up again (or copy the file)

I've knocked up this simple function that simply removes the version info and returns a path that can then just have your settings file appended to get a consistent path.  Thought I'd share it here for the masses;

Public Function AppSettingsPath() As String
Dim AppPath As String
Dim LastSlash As Int16
AppPath = Application.UserAppDataPath
LastSlash = InStrRev(AppPath, "\")
Return AppPath.Substring(0, LastSlash)
End Function

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | VB.net

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About Me

Andrew Jackson

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008,2009 Andrew K. Jackson

RecentComments

Comment RSS