如何使我的 Windows 窗体应用程序对齐屏幕边缘? [英] How to make my Windows Form app snap to screen edges?

查看:21
本文介绍了如何使我的 Windows 窗体应用程序对齐屏幕边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道如何让您的 .net 窗口形成像 Winamp 一样的粘性/活泼的应用程序,以便它捕捉到屏幕的边缘?

Anyone out there know how to make your .net windows form app sticky/snappy like Winamp so it snaps to the edges of the screen?

目标框架是使用 VS08 用 C# 编写的 .NET 2.0 Windows 窗体.我希望将此功能添加到自定义用户控件中,但我认为更多人会受益于为应用程序及其主窗体描述它.

The target framework would be .NET 2.0 Windows Form written in C#, using VS08. I am looking to add this functionality to a custom user control, but I figured more people would benefit from having it described for the application and its main form.

谢谢.

推荐答案

这很有效,适用于多个显示器,观察任务栏:

This worked pretty well, works on multiple monitors, observes the taskbar:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
    private const int SnapDist = 100;
    private bool DoSnap(int pos, int edge) {
      int delta = pos - edge;
      return delta > 0 && delta <= SnapDist;
    }
    protected override void  OnResizeEnd(EventArgs e) {
      base.OnResizeEnd(e);
      Screen scn = Screen.FromPoint(this.Location);
      if (DoSnap(this.Left, scn.WorkingArea.Left)) this.Left= scn.WorkingArea.Left;
      if (DoSnap(this.Top, scn.WorkingArea.Top)) this.Top = scn.WorkingArea.Top;
      if (DoSnap(scn.WorkingArea.Right, this.Right)) this.Left = scn.WorkingArea.Right - this.Width;
      if (DoSnap(scn.WorkingArea.Bottom, this.Bottom)) this.Top = scn.WorkingArea.Bottom - this.Height;
    }
  }

这篇关于如何使我的 Windows 窗体应用程序对齐屏幕边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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