Shanks
Shanks

Reputation: 31

Winform form border issue in windows 10

I am new to WinForms, so need your expert advice on this issue that I am facing when I deploy my Winform Application in Window 10 Pro environment. I see that the dialog form which has FormBorderStyle set to SizableToolWindow(or FixedToolWindow for that matter) doesnt paint the Borders on all side on the window except on top.

Border Issue when FormBorderStyle is set to SizableToolWindow
Border Issue when FormBorderStyle is set to SizableToolWindow

Border is seen when FormBorderStyle is set to FixedSingle
Border is seen when FormBorderStyle is set to FixedSingle

Sample Complete Code is given below:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.FormBorderStyle = FormBorderStyle.FixedSingle;
            form.ShowDialog();
        }
    }

Is there a solution which can override this behavior maybe just for windows 10?

EDIT: I observed that when I set the ControlBox Property of the Form to false the client site is only shown and has the complete border but then the Caption bar is not visible.

Upvotes: 1

Views: 4571

Answers (1)

aleksandaril
aleksandaril

Reputation: 170

Well, I would say the behavior and the rendering depends on the operating system and I think there is no real answer to your question.

However, you can create custom form/window and you can use it as a tool window or however you want.

First, you would need to set the FormBorderStyle to None

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

Then you can override the OnPaint method and draw your border there like the code below

protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle borderRectangle = new Rectangle(1, 1, ClientRectangle.Width - 2, ClientRectangle.Height - 2);

            e.Graphics.DrawRectangle(Pens.Blue, borderRectangle);
            base.OnPaint(e);
        }

The outcome would be something like in the image below: enter image description here

Please note that you will have to take care of other things like giving the ability to this form to be moved around, make sure you add custom close button etc.

Once you have this done, you can use this form as a base class and inherit your future form classes from that one.

The complete code of that custom form would be:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomForm
{
    public partial class CustomBorderForm : Form
    {
        public CustomBorderForm()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle borderRectangle = new Rectangle(1, 1, ClientRectangle.Width - 2, ClientRectangle.Height - 2);

            e.Graphics.DrawRectangle(Pens.Blue, borderRectangle);
            base.OnPaint(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

I hope this was helpful.

Upvotes: 1

Related Questions