在没有客户区移动的情况下更改FormBorderStyle

时间:2016-12-03 11:35:40

标签: c#

我有一个包含两个按钮的浮动无边框表单。当用户将鼠标移到窗体上时,FormBorderStyle将更改为SizableToolWindow,然后使用计时器在设置的时间后返回。发生这种情况时,表单会移动以适应标题栏和表单边缘。我可以对此进行补偿,但是当它发生时,你会在被放回原位之前闪现一个闪烁的形状。

有没有什么方法可以让窗户恢复,直到我已经将它移回原位?

这是我的代码:

        if (this.FormBorderStyle != FormBorderStyle.None)
        {
            // Suspend layout of Form
            this.SuspendLayout();

            // Suspend layout of button controls
            this.cbBlankDisplay.SuspendLayout();
            this.btnPurgeMessages.SuspendLayout();

            this.FormBorderStyle = FormBorderStyle.None;
            this.Left += this.change.Width;
            this.Top += this.change.Height;

            // Resume layout of buttons and Form
            this.btnPurgeMessages.ResumeLayout();
            this.cbBlankDisplay.ResumeLayout();
            this.ResumeLayout();
        }

1 个答案:

答案 0 :(得分:0)

我最终做了Hans Passant建议并创建了一个ClientRectangle区域的副本,该区域显示在已经透明的全屏覆盖图上。 我的MouseLeave事件只是启动计时器。

这是我的MouseEnter事件中的代码:

    private void FloatingButtons_MouseEnter(object sender, EventArgs e)
    {
        if (this.FormBorderStyle != FormBorderStyle.SizableToolWindow)
        {
            if (this.Tag is Bitmap)
            {
                Owner.overlay.CreateGraphics().DrawImage(
                    (Bitmap)this.Tag,
                    new Rectangle(
                        Point.Add(this.Location, this.change),
                        this.ClientRectangle.Size),
                    new Rectangle(this.Location, this.ClientRectangle.Size),
                    GraphicsUnit.Pixel
                    );
            }
            this.Hide();
            this.SuspendLayout();
            this.cbBlankDisplay.SuspendLayout();
            this.btnPurgeMessages.SuspendLayout();
            if (this.change.Height == 0)
            {
                this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
                Rectangle scrPosition = RectangleToScreen(this.ClientRectangle);
                this.change.Width = (scrPosition.Left - this.Left);
                this.change.Height = (scrPosition.Top - this.Top);
            }
            this.Left -= this.change.Width;
            this.Top -= this.change.Height;
            this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            this.btnPurgeMessages.ResumeLayout();
            this.cbBlankDisplay.ResumeLayout();
            this.ResumeLayout();
            this.Show();
            if (this.Tag != null && this.Tag is Bitmap)
            {
                ((Bitmap)this.Tag).Dispose();
                this.Tag = null;
                Overlay.performUpdate = true;
            }
        }
    }

这是我的Timer_Tick事件中的代码:

        if (this.FormBorderStyle != FormBorderStyle.None)
        {
            // Create a bitmap the size of my Form
            Bitmap bmp = new Bitmap(this.Bounds.Width, this.Bounds.Height);
            // Copy the form to the newly created Bitmap
            this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Bounds.Size));
            // Create a rectangle of the Location and Size of the ClientArea
            Rectangle rect = new Rectangle((Point)this.change, this.ClientRectangle.Size);
            // The Owner Form contains a class that already performs overlays on the screen
            Owner.overlay.CreateGraphics().DrawImage(
                bmp,
                new Rectangle(
                    Point.Add(this.Location, this.change),
                    this.ClientRectangle.Size),
                rect,
                GraphicsUnit.Pixel
                );
            // Store the Bitmap in the Tag for Disposal after being used
            this.Tag = bmp;

            // Now I can hide my Form and perform necessary changes
            this.Hide();
            this.SuspendLayout();
            this.cbBlankDisplay.SuspendLayout();
            this.btnPurgeMessages.SuspendLayout();
            this.FormBorderStyle = FormBorderStyle.None;
            this.Left += this.change.Width;
            this.Top += this.change.Height;
            this.btnPurgeMessages.ResumeLayout();
            this.cbBlankDisplay.ResumeLayout();
            this.ResumeLayout();
            this.Show();
        }