Windows 表单应用中的滑动效果

Sliding Effect in Windows Form Application

如何让我的 winforms c# 应用程序位于屏幕左侧?

我想要显示一个小标签,这样当我将鼠标光标移动到屏幕的那个区域时,它就会显示在屏幕上。但是当我最小化它回到那个位置。

听起来你想让你的表单贴在计算机屏幕的一侧,然后当鼠标悬停时,它会展开或做一些事情。

这是基于 Microsoft here 记录的 Shaped Window。

下面使用贴在屏幕一侧的矩形。对于我的 MouseHover,我只是再次将表单变大。您可能会展示第二种形式或制作一些动画。

Link 这些添加到表单的 Load 和 MouseHover 事件。 MouseEnter 可能也会服务。

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Drawing.Drawing2D.GraphicsPath shape = new System.Drawing.Drawing2D.GraphicsPath();
        shape.AddRectangle(new Rectangle(8, 40, 200, 200));
        // I just picked a shape.  The user will see whatever exists in these pixels of the form.
        // After you get it going, make sure that things not on the visible area are disabled.  
        // A user can't click on them but they could tab to them and hit ENTER.
        // Give the user a way to close it other than Alt-F4, stuff like that.
        this.Region = new System.Drawing.Region(shape);
        this.Top = (Screen.PrimaryScreen.WorkingArea.Height - 160) / 2;
        this.Left = Screen.PrimaryScreen.WorkingArea.Left;
    }

    private void Form1_MouseHover(object sender, EventArgs e)
    {
        this.Region = new Region(new Rectangle(0, 0, this.Width, this.Height));
    }

或者,如果您不想受形状限制,可以使用位图创建非标准 window 形状。但是使用这种技术,您的 window 仍然是正方形,这意味着如果人们单击不可见的部分,它不会掉落到较低的 window。为了解决这个问题,您必须制作自定义形状.... borrow this guy's code.

  • 您必须将主窗体的背景图像设置为图片。
  • 确保底角(右下角?)是您的“隐形颜色”
  • 将表单的 BorderStyle 属性 设置为 None。

我在这项技术上的成功是一个看起来像咖啡杯的程序。您甚至可以通过手柄上的孔点击进入下方的东西。

    private void Form1_Load(object sender, EventArgs e)
    {
        this.TransparencyKey = new Bitmap(this.BackgroundImage).GetPixel(1, 1);
        this.Width = this.BackgroundImage.Width;
        this.Height = this.BackgroundImage.Height;
        this.Region = GetRegion(new Bitmap(this.BackgroundImage), this.TransparencyKey);
        // GetRegion fetched from referenced blog post
    }