473,756 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Componen tModel.CancelEv entArgs) 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.Securit y.Permissions.P ermissionSetAtt ribute(System.S ecurity.Permiss ions.SecurityAc tion.Demand,
Name:="FullTrus t")> _
Protected Overrides Sub WndProc(ByRef m As
System.Windows. Forms.Message)
' Listen for operating system messages
Select Case (m.Msg)
Case WM_QUERYENDSESS ION
Application.Exi t()
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 2884
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******** *************@g 14g2000cwa.goog legroups.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.Componen tModel.CancelEv entArgs) 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.Securit y.Permissions.P ermissionSetAtt ribute(System.S ecurity.Permiss ions.SecurityAc tion.Demand,
Name:="FullTrus t")> _
Protected Overrides Sub WndProc(ByRef m As
System.Windows. Forms.Message)
' Listen for operating system messages
Select Case (m.Msg)
Case WM_QUERYENDSESS ION
Application.Exi t()
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.Componen tModel.Componen t

' Component designer generated code omitted.

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

Private Sub menuOptions_Cli ck(...) Handles menuOptions.Cli ck
' this would be your settings dialog.
Dim dialog as New OptionsDialog
dialog.ShowDial og()
dialog.Dispose( )
End Sub

Private Sub menuExit_Click( ...) Handles menuExit.Click
Me.Dispose()
Application.Exi t()
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.S essionEnding & SessionEnded
to identify when the system is shutting down.

Hope this helps
Jay
"Phuff" <pc*****@gmail. com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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.Componen tModel.CancelEv entArgs) 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.Securit y.Permissions.P ermissionSetAtt ribute(System.S ecurity.Permiss ions.SecurityAc tion.Demand,
| Name:="FullTrus t")> _
| Protected Overrides Sub WndProc(ByRef m As
| System.Windows. Forms.Message)
| ' Listen for operating system messages
| Select Case (m.Msg)
| Case WM_QUERYENDSESS ION
| Application.Exi t()
| 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
3250
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." exception. The problem is that this exception isn't raised somewhere in a method, so it just shows up, and it causes the application to shut down. Is there anyway how to catch this kinds of exceptions? Can I put somewhere a
2
1579
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 right now is that the designer view goes nutz when it opens up because the program doesn't load the configuration correctly. Is there anything i can do to keep that block of code from running when in designer view? Perhaps something like this: #if...
0
1235
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 a manner simmilar to the following: Page.RegisterArrayDeclaration( dataObjectArrayName, string.Format( "{{ spanID:'{0}', calendarID:'{1}' }}", spanID,
0
2179
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 either a) System.Windows.Forms.ComboBox or b) System.Windows.Forms.ComboBox.ObjectCollection. If I add (a) I can't override Items.Add.
1
8609
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 the maze, it appends each step it takes to the StringBuilder. When the maze is solved, the user can click a menu item to display the results. When the menu item is clicked, I set the TextBox Text value using the StringBuilder's ToString()...
2
4085
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
1242
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 controls. I can handle the messaging by overriding WndProc, but I can't get App B started without showing itself. Any suggestions on how to do this?
2
2667
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 the web. Thanks! Ron
6
4499
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 exception occurs pointing to FileSaveDialog1.ShowDialog(). The exception also indicates some problem with system.drawing.dll. The exception text is: "Attempted to read or write protected memory. This is often an
0
9431
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.