views:

32

answers:

2

Is there an event in WinForms that get's fired when a window is dragged?

Or is there a better way of doing what I want: to drop the window opacity to 80% when the window is being dragged around?

Unfortunately this is stupidly tricky to search for because everyone is looking for drag and drop from the shell, or some other object.

+2  A: 

It's the LocationChanged event you want:

private void YourApp_LocationChanged(object sender, EventArgs e)
{
    this.Opacity = 0.8;
}

You'll have to override WndProc and handle the exit move event to reset the opacity back to 1:

protected override void WndProc(ref Message m)
{
    Trace.WriteLine(m.ToString());
    switch (m.Msg)
    {
        case WMEXITSIZEMOVE:
            this.Opacity = 1.0;
            break;
    }
    base.WndProc(ref m);
}

Not forgetting to define the message code:

private const int WMEXITSIZEMOVE = 0x0232;

It might be more efficient to handle the WM_ENTERSIZEMOVE (code 0x0231) message instead of LocationChanged as this would only result in setting the opacity once (at the start of the drag) rather than continually throughout the drag.

ChrisF
I will try both WMEXITSIZEMOVE vs WM_ENTERSIZEMOVE, but this certainly does what I need. Thanks!
Steve Syfuhs
@Steve - I meant use WM_ENTERSIZEMOVE instead of LocationChanged.
ChrisF
I doubt this will work unless you set the Opacity for the regular form to 99%
Hans Passant
@nobugz - The default `Opacity` is `1.0`. I've had both versions of this code working in an application without any problems.
ChrisF
The window handle gets recreated when you change the Opacity from 1.0 to 0.8. That must cancel the modal size/move loop, you get a fresh new window. If it works anyway, please consider this useless chatter.
Hans Passant
@nobugz - that's interesting, I'll have to check that out.
ChrisF
@nobugz It does work, though you have an interesting point.
Steve Syfuhs
It is amazing that it works, can't explain why.
Hans Passant
+1  A: 

No need for WndProc hacking, this works fine:

protected override void OnResizeBegin(EventArgs e) {
  this.Opacity = 0.6;
}
protected override void OnResizeEnd(EventArgs e) {
  this.Opacity = 1.0;
}

Moves also trigger the OnResizeXxx events.

Hans Passant
Nice find - this is cleaner
ChrisF
Good call on this. It handled much better. If you do something like Aero-shake on Win7, and then do it again to bring the windows back, it loses the handle and keeps the window at whatever the set opacity was until you move it again.
Steve Syfuhs