Minimise Button

J

Jonathan Dixon

Can someone tell me how to make the minimise button call a different method

I have a method which minimises the program to a tray but want the minimise
button when clicked to
call this method

Regards

Jonathan Dixon
 
N

Nico Vrouwe

Hi,

I'm not sure this is *the* way to do it, but I hope it'll help you..
What I do is create a Resize event, and in there check the windowstate:

if( this.WindowState==FormWindowState.Minimized )
{
// The window was just minimized
}

Hope this helps!

Nico Vrouwe
 
A

Andreas Håkansson

Jonathan,

In order to do this, you will need to subclass your form an check for
the
WM_SYSCOMMAND message with the SC_MINIMIZE command. To
do this you derive a new class from NativeWindow (which you can locate
in System.Window.Forms)

public delegate void MsgWindowMessageDelegate(ref Message m, ref bool
handled);

public class MsgWindow : NativeWindow, IDisposable
{
public event MsgWindowMessageDelegate OnMessage;

public MsgWindow(IntPtr handle)
{
this.AssignHandle(handle);
}

protected override void WndProc(ref Message m)
{
bool handled = false;
if( this.OnMessage != null )
this.OnMessage(ref m, ref handled);
if( !handled )
base.WndProc (ref m);
}

#region IDisposable Members

public void Dispose()
{
if( this.Handle != IntPtr.Zero )
this.DestroyHandle();
}

#endregion
}

Now all you need to do is add a private variable in your form of the
MsgWindow type
(i called mine msgWindow) and add the following to your form constructor

this.msgWindow = new MsgWindow(this.Handle);
this.msgWindow.OnMessage +=
new MsgWindowMessageDelegate(msgWindow_OnMessage);

You will also have to create the msgWindow_OnMessage callback method and
catch the message, like so

private void msgWindow_OnMessage(ref Message m, ref bool handled)
{
if(m.Msg == 0x0112 && (int)m.WParam == 0xF020)
{
handled = true;
MessageBox.Show("Minimized clicked");
}
}

The "handled = true" will tell our subclasser that we have handled the
current
message and that we do not want it to call the defult window proc to process
the message (in some cases you would want it to). Now just replace the
messagebox with your custom code.

HTH,

//Andreas
 
J

Jonathan Dixon

Could you email me a cut down version of your code e.g just enough to show
me how the minimize works

Thanks for your help

Regards

Jonathan Dixon
 
A

Andreas Håkansson

Jonathan,

I am afraid that all the provided source code in my previous reply
is needed to make it work. That is, I did not include any code which
you do not need. Please read the documentation for the
NativeWindow class for some background information.

HTH,

//Andreas
 
J

Jonathan Dixon

Thanks guys for you help i had help from a friend and chose this way of
doing things

protected override void WndProc(ref Message m)

{

// Basically Check if Min was Pressed

if(m.Msg == 0x0112 && (int)m.WParam == 0xF020)

{

// Do uffSt

}

else

{

base.WndProc (ref m); // If Not filter up to Super Class

}

}
 
A

Andreas Håkansson

PocketNerd,

Without downloading and examining the code, I would say that the steps
taken
would be the same as the ones I took in the code I provided. So I do not
thing he
would find that any harder, unless it's the callback part of the MsgWindow
class
which is confusing him. =)

//Andreas
 
J

Jonathan Dixon

The bit that confised me was deriving a new class from NativeWindow.

THanks for the help

Jonathan Dixon
 

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