views:

141

answers:

3

How can I set the opacity of the winform to something like 50% while moving the form by draging the title bar and reset its opacity to 100% once left mouse button is up.

+2  A: 

Set Form.Opacity to 0.5 in response to WM_NCLBUTTONDOWN in the WndProc of your form.

Then set Opacity to 1.0 when WM_NCLBUTTONUP is received.

Kevin Montrose
+2  A: 

Here's a code example:

    public partial class Form1 : System.Windows.Forms.Form
{
    private const long BUTTON_DOWN_CODE = 0xa1;
    private const long BUTTON_UP_CODE = 0xa0;
    private const long WM_MOVING = 0x216;

    static bool left_button_down = false;

    protected override void DefWndProc(ref System.Windows.Forms.Message m)
    {
        //Check the state of the Left Mouse Button
        if ((long)m.Msg == BUTTON_DOWN_CODE)
            left_button_down = true;
        else if ((long)m.Msg == BUTTON_UP_CODE)
            left_button_down = false;

        if (left_button_down)
        {
            if ((long)m.Msg == WM_MOVING)
            {
                //Set the forms opacity to 50% if user is moving
                if (this.Opacity != 0.5)
                    this.Opacity = 0.5;
            }
        }

        else if (!left_button_down)
            if (this.Opacity != 1.0)
                this.Opacity = 1.0;

        base.DefWndProc(ref m);
    }
}
alex
+2  A: 

Interestingly, you can also do it in the OnResizeBegin and OnResizeEnd overrides -- this will apply to both moving and resizing the form.

If you want to change the opacity only when moving, and not when resizing, then alex's answer is better.

Judah Himango