简体   繁体   English

C#-使表格在移动时半透明

[英]C# - Make form semi-transparent while moving

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. 我已经尝试过Form_Move事件,但是没有运气。
I'm stuck, any help? 我被卡住了,有什么帮助吗?

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. 表单加载为半透明的原因是因为必须将表单移到起始位置,这会触发Move事件。 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: ResizeEnd事件在表单移动完成后触发,因此应执行以下操作:

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

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. 显然,您可以对其进行调整以使其淡入/淡出,等等-这只是为了显示整体概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM