C# - Make form semi-transparent while moving

旧巷老猫 提交于 2019-12-08 06:40:48

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/1584239/c-sharp-make-form-semi-transparent-while-moving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!