views:

127

answers:

2

First, sorry for my bad english :)
Second, I can know when the form is being moved/resized, using this code:

    protected override void WndProc(ref Message m)
    {

        if (m.Msg == WM_WINDOWPOSCHANGING)
        {
            WINDOWPOS winPos = new WINDOWPOS();
            winPos = (WINDOWPOS)Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));

            //Here I just need to change the values of the WINDOWPOS structure

            Marshal.StructureToPtr(winPos, m.LParam, true);
        }
    }

The WM_WINDOWPOSCHANGING message is sent also when the user is minimizing or maximizing the window. But how I can know when the user is maximizing/minimizing, not moving/resizing? I tried get the WindowState property, but it didn't work :(
The code of the WINDOWPOS structure is:

[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
    public IntPtr hwnd;
    public IntPtr hwndInsertAfter;
    public int x;
    public int y;
    public int cx;
    public int cy;
    public int flags;
}

Any help?

+2  A: 

You get WM_SYSCOMMAND when the user clicks one of the buttons in the title bar: http://msdn.microsoft.com/en-us/library/ms646360(VS.85).aspx

Tim Robinson
+1  A: 

You can trap the WM_SYSCOMMAND by overriding WndProc(). But it can easily be done as well with an event handler for the Resize event:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        mPrevState = this.WindowState;

    }
    FormWindowState mPrevState;
    protected override void OnResize(EventArgs e) {
        base.OnResize(e);
        if (mPrevState != this.WindowState) {
            mPrevState = this.WindowState;
            // Do something
            //..
        }
    }
}
Hans Passant
And the latter is, of course, much easier. Also, it will catch minimizes not done by clicking the minimize button.
Brian
I tried use the WM_SYSCOMMAND message, but it doesn't work when I set the WindowState property :( . I want to block the minimize action and replace it! But I'll try send system commands instead to set the windowState property.
F E Keglevich