开发者

C#, WinForms - Change FormBorderStyle without the client area moving

开发者 https://www.devze.com 2023-03-07 15:15 出处:网络
I have a small tool window that normally has the FormBorderStyle to FixedDialog with no caption text and no control box so it looks like a border-less form with a raised 3d effect.

I have a small tool window that normally has the FormBorderStyle to FixedDialog with no caption text and no control box so it looks like a border-less form with a raised 3d effect.

When the user moves the mouse over the tool window it changes from this border-less FixedDialog mode to a SizableToolWindow w/ caption text and a control box.

The result is the client area moving.

The following code works but i do not want to hard code the top/left delta and I assume it is different depending on what theme/os the user has

    void Reposition()
    {
        var topDelta = 12; // this number is wrong, i have not found the right number for aero yet
        var leftDelta = 3;
        if (this.Bounds.Contains(MousePosition))
        {
            if (this.FormBorderStyle != Syst开发者_如何学JAVAem.Windows.Forms.FormBorderStyle.SizableToolWindow)
            {
                this.Location = new Point(this.Location.X - leftDelta, this.Location.Y - topDelta);
                this.ControlBox = true;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            }
        }
        else
        {
            if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.SizableToolWindow)
            {
                this.Location = new Point(this.Location.X + leftDelta, this.Location.Y + topDelta);
                this.ControlBox = false;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            }
        }
    }


Look into SystemParameters class. You will find the values you are hard-coding in your code there.

0

精彩评论

暂无评论...
验证码 换一张
取 消