473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Overriding the "X" control and using WndProc

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.PermissionSetAttribut e(System.Security.Permissions.SecurityAction.Deman d,
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

Nov 21 '05 #1
2 2857
Hi,

You never the let the form close even when the system is
shutting down. Here is an example.

http://msdn.microsoft.com/library/de...ndingtopic.asp

Ken
------------------------
"Phuff" <pc*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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.PermissionSetAttribut e(System.Security.Permissions.SecurityAction.Deman d,
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

Nov 21 '05 #2
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
"Phuff" <pc*****@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
|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.PermissionSetAttribut e(System.Security.Permissions.SecurityAction.Deman d,
| 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
|
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: DraguVaso | last post by:
Hi, In my application, on some given actions while debugging in Visual Studio, I suddenly get a "System.ComponentModel.Win32Exception was unhandled" Message="Error creating window handle."...
2
by: Benny Raymond | last post by:
Hi, I'm working on an app that has to inherit Form and override WndProc for several different reasons. Anyway it performs some actions on the window when WM_CREATE is called. The big problem...
0
by: Ken Baltrinic | last post by:
I am authoring a composite server control (inheriting from System.Web.UI.Control) that uses a good chunk of client side javascript. To make this work I need to call RegisterArrayDeclaration and in...
0
by: HKSHK | last post by:
Hi guys, I'm currently trying to implement the VB6-ItemsData property into a CombolBox and it works so far, but there is one problem... I can only inherit one class to another class, which is...
1
by: Flack | last post by:
Hey guys, Here is whats happening. I have a StringBuilder, a TextBox, and a TabControl with one TabPage. On my main form, I created and displayed a fairly big maze. While the app is solving...
2
by: Chris Ashley | last post by:
Hi, I'm overriding WndProc to process some custom messages like so: protected override void WndProc(ref Message m) { if (m.Msg == ImageFileMsg.MSG_IF_NEW_DATA) { ProcessNewDataMessage(m);
5
by: =?Utf-8?B?RGF2ZSBCdXJkb24=?= | last post by:
I have an unmanged C++ app (App A) which launches my VB app (App B). I need the form in AppB to be invisible until App A post a windows message that App B recognises and shows its form and...
2
by: RSH | last post by:
Is it possible to override the onPaint event of the RichTextBox control to set the smoothing mode to anti-aliased? If so how do I do it? I havent been able to find anything about doing this on...
6
by: Scott Gravenhorst | last post by:
Windows XP SP3 My application is set to open a SaveFile dialog when an exit is requested. When I click the app's close button, the save dialog opens, but when I click to change the folder, the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.