Raxdiam
Raxdiam

Reputation: 73

Re-sizing border-less form with a custom border

The problem isn't that I don't know how to make a border-less form re-sizable, or to how to draw a border. The problem is what happens when you re-size the form with that custom border.

Here is a screenshot, because I don't know how to explain it: When expanding the form with the borders

Here is how I created the border (currently):

private void Form1_Paint(object sender, PaintEventArgs e)
{
    int width = 1;
    Rectangle rec = this.ClientRectangle;
    ButtonBorderStyle bbs = ButtonBorderStyle.Solid;
    Color clr = Color.Gray;
    ControlPaint.DrawBorder(e.Graphics, rec, clr, width, bbs, clr, width, bbs, clr, width, bbs, clr, width, bbs);
}

As for re-sizing a border-less form; I created a repository for the project.
Resize Custom Border - Bitbucket

I don't have any idea as to why this happens, so I wouldn't know where to begin. I just need to draw a border without it doing this. I have tried other ways of drawing one, but the results were the same.

Hopefully this and the repository becomes useful for anyone trying to do the same.

Thank you for taking your time to read if you did.

Upvotes: 0

Views: 126

Answers (2)

Nilay Vishwakarma
Nilay Vishwakarma

Reputation: 3363

Use Graphic library :

Step 1: Override the OnPaint handler for your main form

Step 2: Define a rectangle that covers your current form

Step 3: Draw the defined rectangle

protected override void OnPaint(PaintEventArgs e)
{
  Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
  e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}

You may also implement this using a condition statement like:

this.form.Resize += // some handler1

//in hadler1
{
   this.form.Paint += // Your new paint handler2
}

//in handler2
{
   Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
   e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}

Upvotes: 0

Shell
Shell

Reputation: 6849

Try to use Graphics.DrawRectangle instead of DrawBorder

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    Single fWidth = 5.0f;
    Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
    e.Graphics.DrawRectangle(new Pen(Color.Gray, fWidth), r);
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    this.Invalidate();
}

Upvotes: 1

Related Questions