1

I have border-less simple win-form in c#. Now I want to resize form using mouse when form state is WindowState = FormWindowState.Normal. Resize behavior should be same like normal form with border.

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

Attached is form image for reference.

enter image description here

Please suggest me the best way to perform this task. It is required to have border-less form. Because form UI should be like flat UI.

Prem
  • 301
  • 2
  • 10
  • The 2nd answer on this page is what you're after. The first answer is simpler, but the 2nd has all 4 sides: https://stackoverflow.com/questions/2575216/how-to-move-and-resize-a-form-without-a-border – Davesoft Aug 30 '18 at 09:55
  • Thanks for reply. I already read this post, but it didn't fulfilled my requirement. – Prem Aug 30 '18 at 10:29
  • Really? Following the 2nd answer, it seems to match perfectly. I have a nice borderless window that can be resized from all 4 edges and all 4 corners. What more do you want? :) – Davesoft Aug 30 '18 at 10:31
  • It doesn't allow me to resize from sides. Mouse can be at any position of side. Means it can be at top, middle, bottom anywhere, Not at just corner of Left-Top or Bottom-Right like that. Help me if you already have solution. – Prem Aug 30 '18 at 10:33
  • ...k, answer incoming. It'll be in vb, but you can translate it. – Davesoft Aug 30 '18 at 10:36

1 Answers1

4

Here you go, a copy of the 2nd answer from the below link. Translate it if you need to.

How to move and resize a form without a border?

VB Code

Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = FormBorderStyle.None
        Me.DoubleBuffered = True
        Me.SetStyle(ControlStyles.ResizeRedraw, True)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        e.Graphics.FillRectangle(Brushes.Green, Top)
        e.Graphics.FillRectangle(Brushes.Green, Left)
        e.Graphics.FillRectangle(Brushes.Green, Right)
        e.Graphics.FillRectangle(Brushes.Green, Bottom)
    End Sub


    Private Const HTLEFT As Integer = 10, HTRIGHT As Integer = 11, HTTOP As Integer = 12, HTTOPLEFT As Integer = 13, HTTOPRIGHT As Integer = 14, HTBOTTOM As Integer = 15, HTBOTTOMLEFT As Integer = 16, HTBOTTOMRIGHT As Integer = 17

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        If m.Msg = &H84 Then
            Dim mp = Me.PointToClient(Cursor.Position)

            If TopLeft.Contains(mp) Then
                m.Result = CType(HTTOPLEFT, IntPtr)
            ElseIf TopRight.Contains(mp) Then
                m.Result = CType(HTTOPRIGHT, IntPtr)
            ElseIf BottomLeft.Contains(mp) Then
                m.Result = CType(HTBOTTOMLEFT, IntPtr)
            ElseIf BottomRight.Contains(mp) Then
                m.Result = CType(HTBOTTOMRIGHT, IntPtr)
            ElseIf Top.Contains(mp) Then
                m.Result = CType(HTTOP, IntPtr)
            ElseIf Left.Contains(mp) Then
                m.Result = CType(HTLEFT, IntPtr)
            ElseIf Right.Contains(mp) Then
                m.Result = CType(HTRIGHT, IntPtr)
            ElseIf Bottom.Contains(mp) Then
                m.Result = CType(HTBOTTOM, IntPtr)
            End If
        End If
    End Sub

    Dim rng As New Random
    Function randomColour() As Color
        Return Color.FromArgb(255, rng.Next(255), rng.Next(255), rng.Next(255))
    End Function

    Const ImaginaryBorderSize As Integer = 16

    Function Top() As Rectangle
        Return New Rectangle(0, 0, Me.ClientSize.Width, ImaginaryBorderSize)
    End Function

    Function Left() As Rectangle
        Return New Rectangle(0, 0, ImaginaryBorderSize, Me.ClientSize.Height)
    End Function

    Function Bottom() As Rectangle
        Return New Rectangle(0, Me.ClientSize.Height - ImaginaryBorderSize, Me.ClientSize.Width, ImaginaryBorderSize)
    End Function

    Function Right() As Rectangle
        Return New Rectangle(Me.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, Me.ClientSize.Height)
    End Function

    Function TopLeft() As Rectangle
        Return New Rectangle(0, 0, ImaginaryBorderSize, ImaginaryBorderSize)
    End Function

    Function TopRight() As Rectangle
        Return New Rectangle(Me.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, ImaginaryBorderSize)
    End Function

    Function BottomLeft() As Rectangle
        Return New Rectangle(0, Me.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize)
    End Function

    Function BottomRight() As Rectangle
        Return New Rectangle(Me.ClientSize.Width - ImaginaryBorderSize, Me.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize)
    End Function

End Class

C# Code

public Form1()
{
    InitializeComponent();

    this.DoubleBuffered = true;
    this.SetStyle(ControlStyles.ResizeRedraw, true);
}

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.FillRectangle(Brushes.Transparent, Top());
    e.Graphics.FillRectangle(Brushes.Transparent, Left());
    e.Graphics.FillRectangle(Brushes.Transparent, Right());
    e.Graphics.FillRectangle(Brushes.Transparent, Bottom());
}

private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    base.WndProc(ref m);
    if (m.Msg == 0x84)
    {
        var mp = this.PointToClient(Cursor.Position);

        if (TopLeft().Contains(mp))
            m.Result = (IntPtr)HTTOPLEFT;
        else if (TopRight().Contains(mp))
            m.Result = (IntPtr)HTTOPRIGHT;
        else if (BottomLeft().Contains(mp))
            m.Result = (IntPtr)HTBOTTOMLEFT;
        else if (BottomRight().Contains(mp))
            m.Result = (IntPtr)HTBOTTOMRIGHT;
        else if (Top().Contains(mp))
            m.Result = (IntPtr)HTTOP;
        else if (Left().Contains(mp))
            m.Result = (IntPtr)HTLEFT;
        else if (Right().Contains(mp))
            m.Result = (IntPtr)HTRIGHT;
        else if (Bottom().Contains(mp))
            m.Result = (IntPtr)HTBOTTOM;
    }
}

private Random rng = new Random();
public Color randomColour()
{
    return Color.FromArgb(255, rng.Next(255), rng.Next(255), rng.Next(255));
}

const int ImaginaryBorderSize = 2;

public new Rectangle Top()
{
    return new Rectangle(0, 0, this.ClientSize.Width, ImaginaryBorderSize);
}

public new Rectangle Left()
{
    return new Rectangle(0, 0, ImaginaryBorderSize, this.ClientSize.Height);
}

public new Rectangle Bottom()
{
    return new Rectangle(0, this.ClientSize.Height - ImaginaryBorderSize, this.ClientSize.Width, ImaginaryBorderSize);
}

public new Rectangle Right()
{
    return new Rectangle(this.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, this.ClientSize.Height);
}

public Rectangle TopLeft()
{
    return new Rectangle(0, 0, ImaginaryBorderSize, ImaginaryBorderSize);
}

public Rectangle TopRight()
{
    return new Rectangle(this.ClientSize.Width - ImaginaryBorderSize, 0, ImaginaryBorderSize, ImaginaryBorderSize);
}

public Rectangle BottomLeft()
{
    return new Rectangle(0, this.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize);
}

public Rectangle BottomRight()
{
    return new Rectangle(this.ClientSize.Width - ImaginaryBorderSize, this.ClientSize.Height - ImaginaryBorderSize, ImaginaryBorderSize, ImaginaryBorderSize);
}
Prem
  • 301
  • 2
  • 10
Davesoft
  • 724
  • 1
  • 4
  • 10
  • Yes, excellent answer. It helped me. Thanks. I converted this code to c# and it worked fine. – Prem Aug 30 '18 at 11:02
  • 2
    No worries. I went from "you cant!" to "ooo cool, you can!" in 30mins of tinkering. A decade later and I'm still discovering stuff :) – Davesoft Aug 30 '18 at 11:06