WinForms窗口拖动事件

发布于 2024-08-26 19:01:28 字数 130 浏览 4 评论 0原文

WinForms 中是否有拖动窗口时触发的事件?

或者是否有更好的方法来做我想要的事情:当窗口被拖动时将窗口不透明度降低到 80%?

不幸的是,这很难搜索,因为每个人都在寻找从 shell 或其他对象中拖放的内容。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的旅程 2024-09-02 19:01:28

无需 WndProc 黑客攻击,这可以正常工作:

protected override void OnResizeBegin(EventArgs e) {
  this.Opacity = 0.6;
}
protected override void OnResizeEnd(EventArgs e) {
  this.Opacity = 1.0;
}

移动还会触发 OnResizeXxx 事件。

No need for WndProc hacking, this works fine:

protected override void OnResizeBegin(EventArgs e) {
  this.Opacity = 0.6;
}
protected override void OnResizeEnd(EventArgs e) {
  this.Opacity = 1.0;
}

Moves also trigger the OnResizeXxx events.

赢得她心 2024-09-02 19:01:28

这是 LocationChanged 你想要的事件:

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

你必须重写WndProc并处理退出移动事件以将不透明度重置回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);
}

不要忘记定义消息代码:

private const int WMEXITSIZEMOVE = 0x0232;

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

It's the LocationChanged event you want:

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

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;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文