3 Replies - 1594 Views - Last Post: 26 October 2014 - 12:55 PM Rate Topic: -----

#1 FamilyJohnson   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-October 14

Create Borders on all sides, and Resize them.

Posted 26 October 2014 - 07:52 AM

How would one go about doing this? I've searched everywhere, I'm basically trying to Resize a borderless form, I Do not care about moving it all I need is to create a rectangle on top left right and bottom, then handle the mousedown to resize it to the current position...
Is This A Good Question/Topic? 0
  • +

Replies To: Create Borders on all sides, and Resize them.

#2 rusoaica   User is offline

  • They're watching you, Neo!
  • member icon

Reputation: 220
  • View blog
  • Posts: 691
  • Joined: 10-March 12

Re: Create Borders on all sides, and Resize them.

Posted 26 October 2014 - 08:01 AM

View PostFamilyJohnson, on 26 October 2014 - 04:52 PM, said:

I've searched everywhere


Your definition of 'everywhere' is a bit odd...
Tutorial 1
Tutorial 2
Tutorial 3
etc...
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • a lovely bit of linq
  • member icon

Reputation: 6891
  • View blog
  • Posts: 28,512
  • Joined: 12-December 12

Re: Create Borders on all sides, and Resize them.

Posted 26 October 2014 - 08:25 AM

If I've understood part of your question, then you would use the form's Paint event to redraw the rectangle just within the form's area, its ClientSize or ClientRectangle.
Public Class Form1

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        Dim g = e.Graphics
        g.DrawRectangle(Pens.Red, New Rectangle(5, 5, _
            Me.ClientRectangle.Width - 10, Me.ClientRectangle.Height - 10))
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.ResizeRedraw = True
    End Sub
End Class

Rather than using ResizeRedraw (although it is possible to trigger the Resize event) you would invalidate the form as you resize it, causing the rectangle to be redrawn.

This post has been edited by andrewsw: 26 October 2014 - 08:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1540
  • View blog
  • Posts: 3,869
  • Joined: 01-February 13

Re: Create Borders on all sides, and Resize them.

Posted 26 October 2014 - 12:55 PM

Hi,
You can Simplify your code to do this all in the WndProc overrides sub by filtering the Windows Messages that are sent to the window (Form).

When the WndProc receives a WM_NCHITTEST message you can translate the cursor`s position into client coordinates of your form using the PointToClient function.

If the cursor position is within 3 pixels of any edge of the form then you can return a Result to the WM_NCHITTEST message to indicate that the mouse is over a Border or maybe the Caption (TitleBar) area or any other non client area of your form.

This will make the form act as if it is a standard Sizable form without having to manually change the cursor back and forth between the correct sizing arrow or the default cursor and having to handle the code to resize or move the form.

If you also return the HTCAPTION hittest message when the cursor is over a specific area of your form, it treats that specific area of the form as if it is the Caption (TitleBar) area of the form.

If you do that, the form will be moved like a standard window does when the user presses the left mouse button down and drags the form around by its TitleBar.

Here is an example you can copy into a new Form project. You should add a button to the form so you can close it. This is just an example code to show how to use the WM_NCHITTEST messages to implement a Sizable Border and Caption Area for a borderless form.

Public Class Form1
    Private Const WM_NCHITTEST As Integer = &H84
    Private Const HTCAPTION As Integer = &H2
    Private Const HTLEFT As Integer = &HA
    Private Const HTRIGHT As Integer = &HB
    Private Const HTTOP As Integer = &HC
    Private Const HTTOPLEFT As Integer = &HD
    Private Const HTTOPRIGHT As Integer = &HE
    Private Const HTBOTTOM As Integer = &HF
    Private Const HTBOTTOMLEFT As Integer = &H10
    Private Const HTBOTTOMRIGHT As Integer = &H11

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = FormBorderStyle.None
        Me.ResizeRedraw = True
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim bnds As New Rectangle(0, 0, Me.Width, Me.Height)
        e.Graphics.FillRectangle(Brushes.Blue, 0, 0, Me.Width, SystemInformation.CaptionHeight)
        ControlPaint.DrawBorder(e.Graphics, bnds, Color.Blue, 3, ButtonBorderStyle.Solid, Color.Blue, 3, ButtonBorderStyle.Solid, Color.Black, 3, ButtonBorderStyle.Solid, Color.Black, 3, ButtonBorderStyle.Solid)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        'If the WM_NCHITTEST message is received then we need to get the location of the cursor over the form
        'to determin if we need to return a hit test message to display and use the correct sizing arrow cursor.
        If m.Msg = WM_NCHITTEST Then
            'Use the PointToClient function to translate the mouse position into this forms client area coordinates.
            Dim loc As Point = Me.PointToClient(MousePosition)

            Dim bTop As Boolean = (loc.Y < 4) 'True if cursor is over the 3 top most pixels of the form
            Dim bLeft As Boolean = (loc.X < 4) 'True if cursor is over the 3 left most pixels of the form
            Dim bRight As Boolean = (loc.X > Me.ClientSize.Width - 4) 'True if cursor is over the 3 right most pixels of the form
            Dim bBottom As Boolean = (loc.Y > Me.ClientSize.Height - 4) 'True if cursor is over the 3 bottom most pixels of the form
            Dim bCaption As Boolean = (loc.Y > 3 And loc.Y < SystemInformation.CaptionHeight) 'True if the cursor is within our Caption area (TitleBar) 

            If bCaption Then
                m.Result = CType(HTCAPTION, IntPtr) 'Return the Caption hittest message
                Return
            ElseIf bTop And bLeft Then
                m.Result = CType(HTTOPLEFT, IntPtr) 'Return the Top Left Corner hittest message
                Return
            ElseIf bTop And bRight Then
                m.Result = CType(HTTOPRIGHT, IntPtr) 'Return the Top Right Corner hittest message
                Return
            ElseIf bBottom And bLeft Then
                m.Result = CType(HTBOTTOMLEFT, IntPtr) 'Return the Bottom Left Corner hittest message
                Return
            ElseIf bBottom And bRight Then
                m.Result = CType(HTBOTTOMRIGHT, IntPtr) 'Return the Bottom Right Corner hittest message
                Return
            ElseIf bLeft Then
                m.Result = CType(HTLEFT, IntPtr) 'Return Left Side hittest message
                Return
            ElseIf bTop Then
                m.Result = CType(HTTOP, IntPtr) 'Return Top Side hittest message
                Return
            ElseIf bRight Then
                m.Result = CType(HTRIGHT, IntPtr) 'Return Right Side hittest message
                Return
            ElseIf bBottom Then
                m.Result = CType(HTBOTTOM, IntPtr) 'Return Bottom Side hittest message
                Return
            End If
        End If
        MyBase.WndProc(m)
    End Sub
End Class



Hope this helps. :)

This post has been edited by IronRazer: 26 October 2014 - 03:57 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1