WinForms窗口拖动事件 [英] WinForms window drag event
问题描述
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屋!