WinForms窗口拖动事件 [英] WinForms window drag event

查看:106
本文介绍了WinForms窗口拖动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WinForms中是否存在一个事件,该事件在拖动窗口时会被触发?

Is there an event in WinForms that get's fired when a window is dragged?

还是有一种更好的方式来做我想做的事情:在拖动窗口时将窗口不透明度降低到80%?

Or is there a better way of doing what I want: to drop the window opacity to 80% when the window is being dragged around?

不幸的是,这很棘手,因为每个人都在寻找从外壳或其他对象中拖放的东西.

Unfortunately this is stupidly tricky to search for because everyone is looking for drag and drop from the shell, or some other object.

推荐答案

已更改

It's the LocationChanged event you want:

private void YourApp_LocationChanged(object sender, EventArgs e)
{
    this.Opacity = 0.8;
}

您将必须覆盖WndProc并处理退出移动事件,以将不透明度重新设置为1:

You'll have to override WndProc and handle the exit move event to reset the opacity back to 1:

protected override void WndProc(ref Message m)
{
    Trace.WriteLine(m.ToString());
    switch (m.Msg)
    {
        case WMEXITSIZEMOVE:
            this.Opacity = 1.0;
            break;
    }
    base.WndProc(ref m);
}

不要忘记定义消息代码:

Not forgetting to define the message code:

private const int WMEXITSIZEMOVE = 0x0232;

处理 (代码为0x0231)而不是LocationChanged消息,因为这只会导致设置一次不透明度(在拖动开始时),而不是在整个拖动过程中连续设置.

It might be more efficient to handle the WM_ENTERSIZEMOVE (code 0x0231) message instead of LocationChanged as this would only result in setting the opacity once (at the start of the drag) rather than continually throughout the drag.

这篇关于WinForms窗口拖动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆