locked
What's the code beinhd FormBorderStyle = None? RRS feed

  • Question

  • I've looked through .NET Reflector but the .NET code is too much for my head, way too complicated.

    I'm looking for a way to remove the Form border without changing the FormBorderStyle property to None because I need to know that property value for other things.

    How can I remove the Form border without changing the FormBorderStyle property to None?
    Friday, November 21, 2008 12:27 PM

Answers

  • With the help of Mick Doherty, this topic's problem is now fixed :)

    Here's the final code that solved the issue for me:
    1internal class MyForm : Form { 
    2    private FormBorderStyle formBorderStyle; 
    3    private bool enableSkin; 
    4 
    5    [DefaultValue(false)] 
    6    public bool EnableSkin { 
    7        get { 
    8                return enableSkin; 
    9        } set { 
    10                enableSkin = value; 
    11 
    12                if(enableSkin) { 
    13                        BackColor = Color.Lime; 
    14                        MaximizedBounds = Screen.GetWorkingArea(this); 
    15                        TransparencyKey = Color.Lime; 
    16 
    17                        base.FormBorderStyle = FormBorderStyle.None; 
    18                } else { 
    19                        BackColor = SystemColors.Control; 
    20                        MaximizedBounds = Rectangle.Empty; 
    21                        TransparencyKey = Color.Empty; 
    22 
    23                        base.FormBorderStyle = FormBorderStyle; 
    24 
    25                        ReCreateHandle(); 
    26                } 
    27        } 
    28    } 
    29 
    30    [DefaultValue(FormBorderStyle.Sizable)] 
    31    public new FormBorderStyle FormBorderStyle { 
    32        get { 
    33                return formBorderStyle; 
    34        } set { 
    35                formBorderStyle = value; 
    36 
    37                if(EnableSkin) { 
    38                        base.FormBorderStyle = FormBorderStyle.None; 
    39                } else { 
    40                        base.FormBorderStyle = formBorderStyle; 
    41                } 
    42 
    43        } 
    44    } 
    45 
    46    internal MyForm() { 
    47        EnableSkin = false
    48        FormBorderStyle = FormBorderStyle.Sizable; 
    49    } 
    50

    • Marked as answer by Nazgulled Tuesday, November 25, 2008 4:30 PM
    Tuesday, November 25, 2008 4:25 PM

All replies

  • You cannot. That's why these properties are there. You change their value if you want to change the looks of the form.
    If you need the value for other things, you can just assign it to a variable and use that variable for other things.
    Friday, November 21, 2008 1:05 PM
  • It is indeed possible, but I can't imagine why you'd want to do it this way (or what ramifications it may have).
    Anyway, to remove the border without changing the value of the FormBorderStyle property, add the following code to your Form class:

    protected override CreateParams CreateParams 
        get 
        { 
            CreateParams param = base.CreateParams; 
            param.Style &= ~0x00C40000; 
            return param; 
        } 
     

    • Proposed as answer by Matthew Watson Friday, November 21, 2008 2:35 PM
    • Unproposed as answer by Nazgulled Sunday, November 23, 2008 3:38 AM
    Friday, November 21, 2008 2:35 PM
  • Hum...

    After reading Tan Silliksaar suggestion I came up with the following code for my project:
    private FormBorderStyle formBorderStyle; 
     
    public new FormBorderStyle FormBorderStyle {  
        get {  
            return formBorderStyle;  
        } set {  
            formBorderStyle = value;  
     
     
            if(EnableSkin) base.FormBorderStyle = FormBorderStyle.None;  
            else base.FormBorderStyle = formBorderStyle;  
        }  
    }  

    Basically, my application is skinned and so I need to have FormBorderStyle = None. However, instead of creating a new property, I wanted to use the FormBorderStyle value to decide if the skinned form should be resiezable or not. That's why I wanted to keep the FormBorderStyle but set it to None. To draw my custom skin and yet know if the form should be resiezable or not.

    The code above allows me to do that. When the FormBorderStyle property is changed, it will change the base form value to remove the form border if set to None or in this case, if the EnableSkin is true, the border will be removed no matter what. But the return value for FormBorderStyle will not be the base value, but "real" value for the FormBorderStyle. I hope you understand what I'm saying, my native language is not English and it can be hard for me to explain this in the best possible way.

    But I still tested Matthew Watson's solution (removing my property). It seems a better solution because avoids creating a property with the new keyword and all that code. However, if I set the FormBorderStyle to Fixed3D or FixedDialog, I will still see a little border around my Form at design time. It will not show that little border at run time though...

    If that little border problem can be fixed, I think I would rather use that CreateParams solution, otherwise, I would prefer the above one. Can the problem be fixed or I'm better off using "my" solution?
    • Marked as answer by Nazgulled Saturday, November 22, 2008 12:10 AM
    • Unmarked as answer by Nazgulled Sunday, November 23, 2008 3:38 AM
    Friday, November 21, 2008 3:16 PM
  • I ended up finding all the constant values and to fix the Fixed3D and FixedDialog borders problems I came up with this final code:
    protected override CreateParams CreateParams { 
        get { 
            CreateParams param = base.CreateParams; 
     
            param.Style &= ~(int)NativeMethods.WindowStylesFlags.WS_CAPTION; 
            param.Style &= ~(int)NativeMethods.WindowStylesFlags.WS_THICKFRAME; 
     
            param.ExStyle &= ~(int)NativeMethods.ExtendedWindowStylesFlags.WS_EX_DLGMODALFRAME; 
            param.ExStyle &= ~(int)NativeMethods.ExtendedWindowStylesFlags.WS_EX_CLIENTEDGE; 
     
            return param; 
        } 

    But I will not be able to use... The thing is, I'm simulating a border on my skinned form by trapping the WM_NCHITTEST and forcing the result message to be HTLEFT, HTRIGH, HTTOP, etc... But with the CreateParams code above, the simulated border will not exist at all and the HTLEFT messages will not have any effect.

    Unless there is some workaround, I guess I'll have to use the method posted earlier...

    But this is for my special case. Anyone that, for some reason, is looking for something in the lines of this, I would suggest using Matthew Watson's idea, I'll leave is post as "proposed answer" and keep the code snipped that fixed my problem as "answer".
    Saturday, November 22, 2008 12:09 AM
  • Actually, my code is not working properly either... :(

    The current problem:
    The FormBorderStyle property is the default value (Sizable) and my custom EnableSkin property is set to false by default. Let's say I change EnableSkin = True, my skin will be drawn on screen. And now I change it back to EnableSkin = False. The default Windows border (Sizable) will be drawn. So far so good... But if I now change the FormBorderStyle property to something else other than Sizable, the form at design-time will not be correctly drawn.

    I have no idea what's going on...

    However, If I go to Build » Build Solution, the design-time form will flash for a split second and it will be drawn correctly this time. I now can freely change the FormBorderStyle and the design-time form will be correctly drawn. But if I change the EnableSkin to True and then Back to False, and then try to change the FormBorderStyle again, the problem will repeat itself...

    The build process is refreshing the design-time painting somehow. If only I could find out how is this refresh being made, maybe I can fix the problem...

    Here's my current code:
    public new FormBorderStyle FormBorderStyle { 
        get { 
            return formBorderStyle; 
        } set { 
            formBorderStyle = value; 
     
            if(EnableSkin) { 
                base.FormBorderStyle = FormBorderStyle.None; 
            } else { 
                base.FormBorderStyle = formBorderStyle; 
            } 
     
        } 
     
    public bool EnableSkin { 
        get { 
            return enableSkin; 
        } set { 
            enableSkin = value; 
     
            if(enableSkin) { 
                base.FormBorderStyle = FormBorderStyle.None; 
            } else { 
                base.FormBorderStyle = FormBorderStyle; 
            } 
        } 

    Any ideas now?
    Sunday, November 23, 2008 4:06 AM
  • I guess you could look into wm_nccalcsize and simply force the border size to whatever you want and leave the borderstyle property as it is.

    http://msdn.microsoft.com/en-us/library/ms632634(VS.85).aspx

    Eg, if your borderstyle property is Sizable, you could probably force the size of it to be 0 pixels using wm_nccalcsize.

    But it was far too long since I played with that kind stuff so Im not sure if it will work.

    //Roger
    Sunday, November 23, 2008 10:14 AM
  • Have you seen the latest 2 examples on my Form tips page?
    http://dotnetrix.co.uk/form.htm

    You should be able to modify these up to do what you want.
    Mick Doherty
    http://dotnetrix.co.uk
    Sunday, November 23, 2008 11:21 AM
  • @Roger Alsing
    I tried that once, but WM_NCCALCSIZE is very tricky and hard to handle properly... It's too much for my knowledge...

    @Mick Doherty
    Your last example is very very nice indeed. I've been looking in the past for something just like that... However, it doesn't really help me on my current problem. What you are proposing is using your example and adapt it to my needs but I don't have the time for that right now, maybe in the future. but this is definitely going into my bookmarks.

    But there's one tiny issue that I see everywhere in these type of things and no one seems to fix it... When the form is maximized, parts of the form are drawn outside the screen visible area, the caption text is also partly visible. Anyway to fix this for the maximized states?

    Anyway to fix my current problem?
    Sunday, November 23, 2008 2:27 PM

  • Nazgulled said:

    But there's one tiny issue that I see everywhere in these type of things and no one seems to fix it... When the form is maximized, parts of the form are drawn outside the screen visible area,

     I've tackled this problem in my Borderless Form based example.  In the constructor you will see:


        this.MaximizedBounds = Screen.GetWorkingArea(this);

    Nazgulled said:

     the caption text is also partly visible. Anyway to fix this for the maximized states?


    I think I understand what you're saying here. The solution to this problem is shown at the following link:
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=4053898&SiteID=1


    Nazgulled said:

    Anyway to fix my current problem?



    I had a quick look at implementing the behaviour that you are looking for, and it is more complex than we would imagine it to be. I didn't come up with a solution that I was happy with (didn't spend too much time on it), but feel that the CreateParams override method is probably what you should be working on. You can simply call 

        this.UpdateStyles()

     to force the form to re-run the CreateParams() method.


    If you can send me a simple example of your current form then I may be able to find a solution which is suitable for you. You'll find my email address on my contacts page.

    Mick Doherty
    http://dotnetrix.co.uk
    Sunday, November 23, 2008 6:02 PM
  • The MaximizedBounds thing didn't work on the PerPixelAlphaForm. I added that line to it's constructor but the problem remained... And the caption text being only partly visible is because of the same issue. The problem is that the form when maximized, the window size is bigger than it should and it's drawn a bit off screen. By the way, the link you posted points to an empty MSDN page... :/

    The problem with CreateParams to remove the border is that my custom implemented border will not work...

    I'll prepare a basic sample and send it to you. Thanks.
    Sunday, November 23, 2008 6:30 PM
  • Have recieved your sample and will work with it.

    The Link text I provided is correct, but for some reason the actual hyperlink gets mangled. Even tried to edit the hyperlink twice but the result is the same.

    Note that the article isn't an exact answer to the problem, but the answer is very similar.
    Mick Doherty
    http://dotnetrix.co.uk
    Sunday, November 23, 2008 9:47 PM
  • With the help of Mick Doherty, this topic's problem is now fixed :)

    Here's the final code that solved the issue for me:
    1internal class MyForm : Form { 
    2    private FormBorderStyle formBorderStyle; 
    3    private bool enableSkin; 
    4 
    5    [DefaultValue(false)] 
    6    public bool EnableSkin { 
    7        get { 
    8                return enableSkin; 
    9        } set { 
    10                enableSkin = value; 
    11 
    12                if(enableSkin) { 
    13                        BackColor = Color.Lime; 
    14                        MaximizedBounds = Screen.GetWorkingArea(this); 
    15                        TransparencyKey = Color.Lime; 
    16 
    17                        base.FormBorderStyle = FormBorderStyle.None; 
    18                } else { 
    19                        BackColor = SystemColors.Control; 
    20                        MaximizedBounds = Rectangle.Empty; 
    21                        TransparencyKey = Color.Empty; 
    22 
    23                        base.FormBorderStyle = FormBorderStyle; 
    24 
    25                        ReCreateHandle(); 
    26                } 
    27        } 
    28    } 
    29 
    30    [DefaultValue(FormBorderStyle.Sizable)] 
    31    public new FormBorderStyle FormBorderStyle { 
    32        get { 
    33                return formBorderStyle; 
    34        } set { 
    35                formBorderStyle = value; 
    36 
    37                if(EnableSkin) { 
    38                        base.FormBorderStyle = FormBorderStyle.None; 
    39                } else { 
    40                        base.FormBorderStyle = formBorderStyle; 
    41                } 
    42 
    43        } 
    44    } 
    45 
    46    internal MyForm() { 
    47        EnableSkin = false
    48        FormBorderStyle = FormBorderStyle.Sizable; 
    49    } 
    50

    • Marked as answer by Nazgulled Tuesday, November 25, 2008 4:30 PM
    Tuesday, November 25, 2008 4:25 PM