Question

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?

Was it helpful?

Solution

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;
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top