Sliding Effect in Windows Form Application

◇◆丶佛笑我妖孽 提交于 2020-07-22 22:12:14

问题


how can i get my winforms c# app to be in the left of the screen?

I would like a small tab showing so that when i move the mouse cursor to that area of the screen it shows up on screen. But when i minimize it goes back to that position.


回答1:


So it sounds like you want to make your form stick to the side of the computer screen and then when a mouse-over happens, it expands or does something.

This is based on the Shaped Window which is documented by Microsoft here.

The following uses a rectangle stuck to the side of the screen. For my MouseHover, I just make the form big again. You might show a second form or do some animations.

Link these to your form's Load and MouseHover events. MouseEnter would probably serve too.

    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));
    }



回答2:


Or, if you don't want to be limited by shapes, you could use a bitmap to create a non-standard window shape. But with this technique your window is still square meaning that if people click on the invisible part it doesn't fall through to the lower window. To take care of that you have to make a custom shape.... borrow this guy's code.

  • You have to set your main form's background image to a picture.
  • Make sure the bottom corner (bottom-right?) is your "invisible color"
  • Set your form's BorderStyle property to None.

My success for this technique is a program that looks like a coffee cup. You can even click through the hole in the handle to get at things below it.

    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
    }


来源:https://stackoverflow.com/questions/28636734/sliding-effect-in-windows-form-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!