views:

788

answers:

3

Is there any way to make the form semi-transparent while it is being moved and then become opaque when it's not being moved anymore? I have tried the Form_Move event with no luck.
I'm stuck, any help?

A: 

use Jquery C# / .NET can't be a good idea. Jquery will do your job.

Moksha
Don't think JQuery works on the desktop just yet...
Gurdas Nijor
the question didn't mention windows form, just form, I thought its web form
Moksha
Sorry, should've been more clear about that.
Nate Shoffner
Not really, I think it's very clear. What asp.net form has opacity, can be moved, and raises a `Form_Move` event?
Kobi
+1  A: 

To do it properly I expect you'd need to override the message processing to respond to the title bar being held, etc. But you could cheat, and just use a timer so that you make it opaque for a little while when moved, so continuous movement works:

[STAThread]
static void Main()
{
    using (Form form = new Form())
    using (Timer tmr = new Timer())
    {
        tmr.Interval = 500;
        bool first = true;
        tmr.Tick += delegate
        {
            tmr.Stop();
            form.Opacity = 1;
        };
        form.Move += delegate
        {
            if (first) { first = false; return; }
            tmr.Stop();
            tmr.Start();
            form.Opacity = 0.3;
        };
        Application.Run(form);
    }
}

Obviously you could tweak this to fade in/out, etc - this is just to show the overall concept.

Marc Gravell
I get an error saying "SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application."
Nate Shoffner
So do that... I don't get that error, presumably culture related.
Marc Gravell
Ok, I put Application.SetCompatibleTextRenderingDefault(false); before everything else and it runs. But no transparency when the form is moved.
Nate Shoffner
+3  A: 

The reason the form loads as semi-transparent is because the form has to be moved into the starting position, which triggers the Move event. You can overcome that by basing whether the opacity is set, on whether the form has fully loaded.

The ResizeEnd event fires after a form has finished moving, so something like this should work:

bool canMove = false;

private void Form1_Load(object sender, EventArgs e)
{
    canMove = true;
}

private void Form1_Move(object sender, EventArgs e)
{
    if (canMove)
    {
     this.Opacity = 0.5;
    }
}

private void Form1_ResizeEnd(object sender, EventArgs e)
{
    this.Opacity = 1;
}
Bauer
Nice one! I didn't know that ResizeEnd fired when the user finished moving a form. I notice that ResizeBegin fires when the user starts moving a form, so maybe he could use this rather than the Form_Load / canMove / Form_Move trick?
itowlson
Thank you so much! This is perfect! I'll experiment with it even more. By the way, love your avatar Bauer ;).
Nate Shoffner