none
Resize PictureBox in a Fix PictureBox With Mouse RRS feed

  • Question

  • Hello once again.... To Everyone
    I imagine that most of the readers already have some "???" over the head... Sorry for that.
    Long Story short :

    I am making some kind of form designer in visual basic (I'm a VB noob) and I want to resize the picture box that I dynamically add on one other picture box.... 
    I already add a Handler that moves the picture box...
    Already searching for a while on the internet for a solution that is close to my problem !!Nothing!! sometimes I have the feeling that VB is dying slowly :-( 
    and I don't exactly know why but I like VB and I keep working on it to do my small baby projects.... 

    The problem is that I can't find a way to resize the picture box with the mouse for example on the border of the picture box or with some reference point on it.

    I'd imagine something like the IDE form edit of visual studio for the picture box.

    Is that possible? 
    I imagine that by the moment that the IDE has that functionality it must be a way to do it with the picture box.

    For example, I already made some test with a form that is in the form that is resizable... (MDI Form)

    Am I too far from the solution?


    Thanks in advance to everyone!

    • Edited by PaoloCiD Thursday, November 2, 2017 12:14 PM
    Thursday, November 2, 2017 10:26 AM

Answers

  •  Perhaps this will be close to what you are trying to do.  However,  i did notice that there seems to be a problem with using the FixedSingle border style but,  the None style and Fixed3D styles seem to work alright.  When sizing it smaller and using the FixedSingle border style, it will only let you go just so small.  It seems to have some correlation to the image size.

     It is a bit more code than just using the CreateParams as Castorix31 showed but,  this PictureBoxEx class does not have the thick white border around it like Castorix31's does.  This makes it better for doing something like shown in the animated image posted after the example code.

     You can test it in a new form project by adding all the code below,  then running the app and closing the app,  or just building the project.  Then you will see the PictureBoxEx control in the top of the Toolbox and you can add one to the Form.  For the example below,  i add a standard picturebox to the form which shows the water.  The fish is in the PictureBoxEx control.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBoxEx1.Parent = PictureBox1 'here i set the PicterBoxEx1 control as a child of PictureBox1 so that the Transparent BackColor of the PictureBoxEx1 works overtop of PictureBox1
        End Sub
    End Class
    
    
    Public Class PictureBoxEx
        Inherits PictureBox
    
        Private Const WM_MOUSEMOVE As Integer = &H200
        Private Const WM_LBUTTONDOWN As Integer = &H201
        Private Const MK_LBUTTON As Integer = &H1
        Private Const WM_NCHITTEST As Integer = &H84
        Private Const WM_NCLBUTTONDOWN As Integer = &HA1
        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 OffSet As Point = Point.Empty
        Private Selected As Boolean = False
    
        Public Sub New()
            Me.Cursor = Cursors.SizeAll
            Me.SizeMode = PictureBoxSizeMode.StretchImage
            Me.BackColor = Color.Transparent
        End Sub
    
        Protected Overrides Sub OnPaint(pe As PaintEventArgs)
            MyBase.OnPaint(pe)
            If Me.Capture Then
                Using bp As New Pen(Color.White, 1)
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                    bp.DashStyle = Drawing2D.DashStyle.DashDot
                    bp.Color = Color.Black
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                End Using
            End If
        End Sub
    
        Protected Overrides Sub OnMouseCaptureChanged(e As EventArgs)
            MyBase.OnMouseCaptureChanged(e)
            Me.Invalidate(False)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_NCLBUTTONDOWN Then
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_NCHITTEST Then
                Dim loc As Point = Me.PointToClient(MousePosition)
                Dim bTop As Boolean = (loc.Y < 6)
                Dim bLeft As Boolean = (loc.X < 6)
                Dim bRight As Boolean = (loc.X > Me.Width - 7)
                Dim bBottom As Boolean = (loc.Y > Me.Height - 7)
    
                If bTop AndAlso bLeft Then
                    m.Result = CType(HTTOPLEFT, IntPtr)
                    Return
                ElseIf bTop AndAlso bRight Then
                    m.Result = CType(HTTOPRIGHT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bLeft Then
                    m.Result = CType(HTBOTTOMLEFT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bRight Then
                    m.Result = CType(HTBOTTOMRIGHT, IntPtr)
                    Return
                ElseIf bLeft Then
                    m.Result = CType(HTLEFT, IntPtr)
                    Return
                ElseIf bTop Then
                    m.Result = CType(HTTOP, IntPtr)
                    Return
                ElseIf bRight Then
                    m.Result = CType(HTRIGHT, IntPtr)
                    Return
                ElseIf bBottom Then
                    m.Result = CType(HTBOTTOM, IntPtr)
                    Return
                End If
            ElseIf m.Msg = WM_LBUTTONDOWN Then
                OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y)
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then
                Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y)
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
     

     

     

     If you have more controls that you want to make resizable and movable,  then the link that Acamar showed you will do that.  I also have a class that i made for making different types of controls resizable and movable too but,  won't show it unless that is what you actually want to do.  I would have to find it too.   8)


    If you say it can`t be done then i`ll try it


    • Edited by IronRazerz Thursday, November 2, 2017 11:00 PM
    • Marked as answer by PaoloCiD Friday, November 3, 2017 7:41 AM
    Thursday, November 2, 2017 10:55 PM
  •  Here is a quick example i threw together of the same PictureBoxEx control class which i have added a few public properties to for drawing the text.  I added LabelText,  LabelTextFont,  LabelTextForeColor,  and  LabelTextBackColor property and used the TextRenderer.DrawText method to draw the text.  After adding the class and building or running your application,  you will see these properties in the Properties tab in the Designer window as shown here...

     

     Here is the modified class code...

    Public Class PictureBoxEx
        Inherits PictureBox
    
        Private Const WM_MOUSEMOVE As Integer = &H200
        Private Const WM_LBUTTONDOWN As Integer = &H201
        Private Const MK_LBUTTON As Integer = &H1
        Private Const WM_NCHITTEST As Integer = &H84
        Private Const WM_NCLBUTTONDOWN As Integer = &HA1
        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 OffSet As Point = Point.Empty
        Private Selected As Boolean = False
    
        Public Sub New()
            Me.Cursor = Cursors.SizeAll
            Me.SizeMode = PictureBoxSizeMode.StretchImage
            Me.BackColor = Color.Transparent
        End Sub
    
        Private _LabelText As String = ""
        Public Property LabelText As String
            Get
                Return _LabelText
            End Get
            Set(value As String)
                _LabelText = value
                Me.Refresh()
            End Set
        End Property
    
        Private _LabelTextFont As Font = New Font("Microsoft Sans Serif", 8.25)
        Public Property LabelTextFont As Font
            Get
                Return _LabelTextFont
            End Get
            Set(value As Font)
                _LabelTextFont = value
                Me.Refresh()
            End Set
        End Property
    
        Private _LabelTextForeColor As Color = Color.Black
        Public Property LabelTextForeColor As Color
            Get
                Return _LabelTextForeColor
            End Get
            Set(value As Color)
                _LabelTextForeColor = value
                Me.Refresh()
            End Set
        End Property
    
        Private _LabelTextBackColor As Color = Color.Transparent
        Public Property LabelTextBackColor As Color
            Get
                Return _LabelTextBackColor
            End Get
            Set(value As Color)
                _LabelTextBackColor = value
                Me.Refresh()
            End Set
        End Property
    
        Protected Overrides Sub OnPaint(pe As PaintEventArgs)
            MyBase.OnPaint(pe)
    
            If _LabelText <> "" Then
                TextRenderer.DrawText(pe.Graphics, _LabelText, _LabelTextFont, New Rectangle(1, 1, Me.ClientSize.Width - 2, Me.ClientSize.Height - 2), _LabelTextForeColor, _LabelTextBackColor, TextFormatFlags.Top Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.WordBreak)
            End If
    
            If Me.Capture Then 'draws a 'dashed' border if mouse is captured
                Using bp As New Pen(Color.White, 1)
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                    bp.DashStyle = Drawing2D.DashStyle.DashDot
                    bp.Color = Color.Black
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                End Using
    
            Else 'draws a 'single line' border if mouse is not captured
                pe.Graphics.DrawRectangle(Pens.Black, 0, 0, Me.Width - 1, Me.Height - 1)
            End If
        End Sub
    
        Protected Overrides Sub OnMouseCaptureChanged(e As EventArgs)
            MyBase.OnMouseCaptureChanged(e)
            Me.Invalidate(False)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_NCLBUTTONDOWN Then
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_NCHITTEST Then
                Dim loc As Point = Me.PointToClient(MousePosition)
                Dim bTop As Boolean = (loc.Y < 6)
                Dim bLeft As Boolean = (loc.X < 6)
                Dim bRight As Boolean = (loc.X > Me.Width - 7)
                Dim bBottom As Boolean = (loc.Y > Me.Height - 7)
    
                If bTop AndAlso bLeft Then
                    m.Result = CType(HTTOPLEFT, IntPtr)
                    Return
                ElseIf bTop AndAlso bRight Then
                    m.Result = CType(HTTOPRIGHT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bLeft Then
                    m.Result = CType(HTBOTTOMLEFT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bRight Then
                    m.Result = CType(HTBOTTOMRIGHT, IntPtr)
                    Return
                ElseIf bLeft Then
                    m.Result = CType(HTLEFT, IntPtr)
                    Return
                ElseIf bTop Then
                    m.Result = CType(HTTOP, IntPtr)
                    Return
                ElseIf bRight Then
                    m.Result = CType(HTRIGHT, IntPtr)
                    Return
                ElseIf bBottom Then
                    m.Result = CType(HTBOTTOM, IntPtr)
                    Return
                End If
            ElseIf m.Msg = WM_LBUTTONDOWN Then
                OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y)
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then
                Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y)
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
    
     

     And here is an example of some text in the control at run time.  Notice the text wraps when the control is sized narrower that the text.  You can modify the Alignment of the text in the last parameter of the TextRenderer.DrawText method.  I set it to the Top,  Horizontal Center, and to use Word Breaks.  The WordBreak is to wrap the text on whole words.


    If you say it can`t be done then i`ll try it

    • Marked as answer by PaoloCiD Wednesday, November 29, 2017 9:11 PM
    Wednesday, November 29, 2017 8:28 PM
  •  Well,  without knowing the details of your code and seeing an image showing the problem i can only guess.  Perhaps you need to set the Label's Location to (1, 1) or (2, 2) so it is not covering the border that is drawn on the outer edges of the PictureBoxEx control.

     The best solution would be to draw the text on the PictureBoxEx control in it's OnPaint sub,  the same sub the Border is drawn in.  You can do that using one of the overloads of the Graphics.DrawString Method or one of the overloads of the TextRenderer.DrawText Method.


    If you say it can`t be done then i`ll try it

    • Marked as answer by PaoloCiD Wednesday, November 29, 2017 7:37 PM
    Wednesday, November 29, 2017 7:11 PM

All replies

  • I am making some kind of form designer in visual basic (I'm a VB noob) and I want to resize the picture box that I dynamically add on one other picture box....

    The size of the picture box control is set with the Size property.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.size(v=vs.110).aspx

    You should show the code you at using to set the values for this property, and indicate why it isn't working the way that you want.

    Thursday, November 2, 2017 11:00 AM
  • Thank You for the help

    Sorry for the confusion I'm aware of the size properties.
    The problem is how is possible to change the size with the mouse event.... 

    Thursday, November 2, 2017 11:09 AM
  • The problem is how is possible to change the size with the mouse event.... 

    What mouse event are you talking about?  You have a click event available, which would use some information elsewhere such as in a text box or a numeric up-down, or some calculation based on window size etc.  But there is also a drag event, which you might use in a process of dragging the borders of a control.
    https://www.codeproject.com/Tips/709121/Move-and-Resize-Controls-on-a-Form-at-Runtime-With

    It's just a matter of defining how you want the user to specify the values you will use for the Size property.


    • Edited by Acamar Thursday, November 2, 2017 11:52 AM sp
    Thursday, November 2, 2017 11:52 AM
  • Thank you very much for your help and code snippet...
    (i don't really read or understand  C# )
    The confusion is my mistake not giving enough information.
    I will Edit the question and I will give more information about the subject.
    Thursday, November 2, 2017 12:05 PM
  • The problem is how is possible to change the size with the mouse event.... 

    To make a control resizable with minimum code, you can do :

    Partial Class MyPictureBox
        Inherits PictureBox
        Protected Overrides ReadOnly Property CreateParams() As CreateParams
            Get
                Dim params As CreateParams = MyBase.CreateParams
                params.Style += &H40000L
                Return params
            End Get
        End Property
    End Class

    Thursday, November 2, 2017 1:52 PM
  • I will Edit the question and I will give more information about the subject.

    I think you are saying that the procedure in that example is a bit like what you want to do.  You can use the description provided with the example to work out how it was done, or you can convert the code to VB using any of the available converters, such as  http://converter.telerik.com/.  There are many other similar examples that follow the same pattern.

    But most of the code is framework classes and functions, so knowledge of C# isn't really required.

    Thursday, November 2, 2017 8:56 PM
  •  Perhaps this will be close to what you are trying to do.  However,  i did notice that there seems to be a problem with using the FixedSingle border style but,  the None style and Fixed3D styles seem to work alright.  When sizing it smaller and using the FixedSingle border style, it will only let you go just so small.  It seems to have some correlation to the image size.

     It is a bit more code than just using the CreateParams as Castorix31 showed but,  this PictureBoxEx class does not have the thick white border around it like Castorix31's does.  This makes it better for doing something like shown in the animated image posted after the example code.

     You can test it in a new form project by adding all the code below,  then running the app and closing the app,  or just building the project.  Then you will see the PictureBoxEx control in the top of the Toolbox and you can add one to the Form.  For the example below,  i add a standard picturebox to the form which shows the water.  The fish is in the PictureBoxEx control.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBoxEx1.Parent = PictureBox1 'here i set the PicterBoxEx1 control as a child of PictureBox1 so that the Transparent BackColor of the PictureBoxEx1 works overtop of PictureBox1
        End Sub
    End Class
    
    
    Public Class PictureBoxEx
        Inherits PictureBox
    
        Private Const WM_MOUSEMOVE As Integer = &H200
        Private Const WM_LBUTTONDOWN As Integer = &H201
        Private Const MK_LBUTTON As Integer = &H1
        Private Const WM_NCHITTEST As Integer = &H84
        Private Const WM_NCLBUTTONDOWN As Integer = &HA1
        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 OffSet As Point = Point.Empty
        Private Selected As Boolean = False
    
        Public Sub New()
            Me.Cursor = Cursors.SizeAll
            Me.SizeMode = PictureBoxSizeMode.StretchImage
            Me.BackColor = Color.Transparent
        End Sub
    
        Protected Overrides Sub OnPaint(pe As PaintEventArgs)
            MyBase.OnPaint(pe)
            If Me.Capture Then
                Using bp As New Pen(Color.White, 1)
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                    bp.DashStyle = Drawing2D.DashStyle.DashDot
                    bp.Color = Color.Black
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                End Using
            End If
        End Sub
    
        Protected Overrides Sub OnMouseCaptureChanged(e As EventArgs)
            MyBase.OnMouseCaptureChanged(e)
            Me.Invalidate(False)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_NCLBUTTONDOWN Then
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_NCHITTEST Then
                Dim loc As Point = Me.PointToClient(MousePosition)
                Dim bTop As Boolean = (loc.Y < 6)
                Dim bLeft As Boolean = (loc.X < 6)
                Dim bRight As Boolean = (loc.X > Me.Width - 7)
                Dim bBottom As Boolean = (loc.Y > Me.Height - 7)
    
                If bTop AndAlso bLeft Then
                    m.Result = CType(HTTOPLEFT, IntPtr)
                    Return
                ElseIf bTop AndAlso bRight Then
                    m.Result = CType(HTTOPRIGHT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bLeft Then
                    m.Result = CType(HTBOTTOMLEFT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bRight Then
                    m.Result = CType(HTBOTTOMRIGHT, IntPtr)
                    Return
                ElseIf bLeft Then
                    m.Result = CType(HTLEFT, IntPtr)
                    Return
                ElseIf bTop Then
                    m.Result = CType(HTTOP, IntPtr)
                    Return
                ElseIf bRight Then
                    m.Result = CType(HTRIGHT, IntPtr)
                    Return
                ElseIf bBottom Then
                    m.Result = CType(HTBOTTOM, IntPtr)
                    Return
                End If
            ElseIf m.Msg = WM_LBUTTONDOWN Then
                OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y)
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then
                Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y)
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
     

     

     

     If you have more controls that you want to make resizable and movable,  then the link that Acamar showed you will do that.  I also have a class that i made for making different types of controls resizable and movable too but,  won't show it unless that is what you actually want to do.  I would have to find it too.   8)


    If you say it can`t be done then i`ll try it


    • Edited by IronRazerz Thursday, November 2, 2017 11:00 PM
    • Marked as answer by PaoloCiD Friday, November 3, 2017 7:41 AM
    Thursday, November 2, 2017 10:55 PM
  • Hello and thank IronRazerz
    First of all, I'm pretty sure that all the other answer was good too but sometimes is not only a meter of good to the point answer but an answer that will be understood from the level of the person that made it.
    Thanks to everyone and sorry trouble.

    Now the answer was more than expected and very good.
    When I saw the PictureBoxEx on the toolbar was like 0_0 and then slowly I read the code again and again in order to realize what happened.
    In a matter of fact, I need the BorderStyle to be fixed Single and I saw that correctly like you said it cannot become too small, but for that, I was thinking to draw a rectangle based on the bounds ( -1) each time that I resize the picture box. 
    What do you think?  <---(plural)

    Thanks Again!
    Friday, November 3, 2017 8:05 AM
  • I will reply to myself :-)
    Indeed was not working with border style, I tried to find out the reason with the minimum size it was !not. 
    I removed the border style and I tried to redraw a rectangle in the picture box but anytime I was having a problem drawing it on almost any event. 
    I did try to do it in the form for example on PictureBoxEx change size ... nothing!
    I did change the approach and I try to do it on the class of the PictureBoxEx again the same exact problem except for the event mouse leave...  :-/ 
    So after that, I decided to redraw the rectangle in the PictureBoxEX in the WndProc Routine at the end...
    With this small routine...

        Private Sub borderRec()
            Dim newrec As New Rectangle
            Dim blackpen As New Drawing.Pen(Color.Black)
            Dim g As Graphics
            g = Me.CreateGraphics
    
            newrec = Me.Bounds
    
            newrec.X = 0
            newrec.Y = 0
            newrec.Width = newrec.Width - 1
            newrec.Height = newrec.Height - 1
    
    
            g.DrawRectangle(blackpen, newrec)
            g.Dispose()
    
        End Sub

    And that finally worked like a charm.... ;-)
    Thank You once again!

    I like VB and I hope I will be really good one day!


    • Edited by PaoloCiD Friday, November 3, 2017 1:19 PM
    Friday, November 3, 2017 1:16 PM
  •  You can just use the OnPaint override sub in the PictureBoxEx class to draw a solid single line border.  Try changing it to this....

        Protected Overrides Sub OnPaint(pe As PaintEventArgs)
            MyBase.OnPaint(pe)
            If Me.Capture Then 'if it has captured the mouse, then draw the black-n-white dashed line.
                Using bp As New Pen(Color.White, 1)
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                    bp.DashStyle = Drawing2D.DashStyle.DashDot
                    bp.Color = Color.Black
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                End Using
            Else 'if it has not captured the mouse,  draw a solid black single line border
                pe.Graphics.DrawRectangle(Pens.Black, 0, 0, Me.Width - 1, Me.Height - 1)
            End If
        End Sub
    

     

     


    If you say it can`t be done then i`ll try it

    Friday, November 3, 2017 9:14 PM
  • I have a small question that is the expansion of the question here...
    I add a label to the "pictureboxEx" that but when the PictureBox become small and the label, for example, is on the left upside the I can't see the borders of the PictureBox.
    I already try to make the back color of the label Transparent but nothing ...
    Have you any idea why this happens?
    Or any other suggestion?
    Thanks in advance! 
    Wednesday, November 29, 2017 6:27 PM
  •  Well,  without knowing the details of your code and seeing an image showing the problem i can only guess.  Perhaps you need to set the Label's Location to (1, 1) or (2, 2) so it is not covering the border that is drawn on the outer edges of the PictureBoxEx control.

     The best solution would be to draw the text on the PictureBoxEx control in it's OnPaint sub,  the same sub the Border is drawn in.  You can do that using one of the overloads of the Graphics.DrawString Method or one of the overloads of the TextRenderer.DrawText Method.


    If you say it can`t be done then i`ll try it

    • Marked as answer by PaoloCiD Wednesday, November 29, 2017 7:37 PM
    Wednesday, November 29, 2017 7:11 PM
  • Thank you for your immediate Answer!
    Of course is difficult to understand what I mean you are completely right!
    The difficult part is that I can't send a block of the code because I am a noob programmer and my code is SPAGHETTI! I prefer to send all the project to you and see it that do something pointless.
    I already try to draw the text on the PictureBox but the resolution is pretty low for some reason even if I try high resolution on the drow.
    If you need the code just ask!

    Thanks again!

    Wednesday, November 29, 2017 7:36 PM
  •  Here is a quick example i threw together of the same PictureBoxEx control class which i have added a few public properties to for drawing the text.  I added LabelText,  LabelTextFont,  LabelTextForeColor,  and  LabelTextBackColor property and used the TextRenderer.DrawText method to draw the text.  After adding the class and building or running your application,  you will see these properties in the Properties tab in the Designer window as shown here...

     

     Here is the modified class code...

    Public Class PictureBoxEx
        Inherits PictureBox
    
        Private Const WM_MOUSEMOVE As Integer = &H200
        Private Const WM_LBUTTONDOWN As Integer = &H201
        Private Const MK_LBUTTON As Integer = &H1
        Private Const WM_NCHITTEST As Integer = &H84
        Private Const WM_NCLBUTTONDOWN As Integer = &HA1
        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 OffSet As Point = Point.Empty
        Private Selected As Boolean = False
    
        Public Sub New()
            Me.Cursor = Cursors.SizeAll
            Me.SizeMode = PictureBoxSizeMode.StretchImage
            Me.BackColor = Color.Transparent
        End Sub
    
        Private _LabelText As String = ""
        Public Property LabelText As String
            Get
                Return _LabelText
            End Get
            Set(value As String)
                _LabelText = value
                Me.Refresh()
            End Set
        End Property
    
        Private _LabelTextFont As Font = New Font("Microsoft Sans Serif", 8.25)
        Public Property LabelTextFont As Font
            Get
                Return _LabelTextFont
            End Get
            Set(value As Font)
                _LabelTextFont = value
                Me.Refresh()
            End Set
        End Property
    
        Private _LabelTextForeColor As Color = Color.Black
        Public Property LabelTextForeColor As Color
            Get
                Return _LabelTextForeColor
            End Get
            Set(value As Color)
                _LabelTextForeColor = value
                Me.Refresh()
            End Set
        End Property
    
        Private _LabelTextBackColor As Color = Color.Transparent
        Public Property LabelTextBackColor As Color
            Get
                Return _LabelTextBackColor
            End Get
            Set(value As Color)
                _LabelTextBackColor = value
                Me.Refresh()
            End Set
        End Property
    
        Protected Overrides Sub OnPaint(pe As PaintEventArgs)
            MyBase.OnPaint(pe)
    
            If _LabelText <> "" Then
                TextRenderer.DrawText(pe.Graphics, _LabelText, _LabelTextFont, New Rectangle(1, 1, Me.ClientSize.Width - 2, Me.ClientSize.Height - 2), _LabelTextForeColor, _LabelTextBackColor, TextFormatFlags.Top Or TextFormatFlags.HorizontalCenter Or TextFormatFlags.WordBreak)
            End If
    
            If Me.Capture Then 'draws a 'dashed' border if mouse is captured
                Using bp As New Pen(Color.White, 1)
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                    bp.DashStyle = Drawing2D.DashStyle.DashDot
                    bp.Color = Color.Black
                    pe.Graphics.DrawRectangle(bp, 0, 0, Me.Width - 1, Me.Height - 1)
                End Using
    
            Else 'draws a 'single line' border if mouse is not captured
                pe.Graphics.DrawRectangle(Pens.Black, 0, 0, Me.Width - 1, Me.Height - 1)
            End If
        End Sub
    
        Protected Overrides Sub OnMouseCaptureChanged(e As EventArgs)
            MyBase.OnMouseCaptureChanged(e)
            Me.Invalidate(False)
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_NCLBUTTONDOWN Then
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_NCHITTEST Then
                Dim loc As Point = Me.PointToClient(MousePosition)
                Dim bTop As Boolean = (loc.Y < 6)
                Dim bLeft As Boolean = (loc.X < 6)
                Dim bRight As Boolean = (loc.X > Me.Width - 7)
                Dim bBottom As Boolean = (loc.Y > Me.Height - 7)
    
                If bTop AndAlso bLeft Then
                    m.Result = CType(HTTOPLEFT, IntPtr)
                    Return
                ElseIf bTop AndAlso bRight Then
                    m.Result = CType(HTTOPRIGHT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bLeft Then
                    m.Result = CType(HTBOTTOMLEFT, IntPtr)
                    Return
                ElseIf bBottom AndAlso bRight Then
                    m.Result = CType(HTBOTTOMRIGHT, IntPtr)
                    Return
                ElseIf bLeft Then
                    m.Result = CType(HTLEFT, IntPtr)
                    Return
                ElseIf bTop Then
                    m.Result = CType(HTTOP, IntPtr)
                    Return
                ElseIf bRight Then
                    m.Result = CType(HTRIGHT, IntPtr)
                    Return
                ElseIf bBottom Then
                    m.Result = CType(HTBOTTOM, IntPtr)
                    Return
                End If
            ElseIf m.Msg = WM_LBUTTONDOWN Then
                OffSet = New Point(MousePosition.X - Me.Location.X, MousePosition.Y - Me.Location.Y)
                OnMouseCaptureChanged(EventArgs.Empty)
            ElseIf m.Msg = WM_MOUSEMOVE AndAlso m.WParam.ToInt32 = MK_LBUTTON Then
                Me.Location = New Point(MousePosition.X - OffSet.X, MousePosition.Y - OffSet.Y)
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
    
     

     And here is an example of some text in the control at run time.  Notice the text wraps when the control is sized narrower that the text.  You can modify the Alignment of the text in the last parameter of the TextRenderer.DrawText method.  I set it to the Top,  Horizontal Center, and to use Word Breaks.  The WordBreak is to wrap the text on whole words.


    If you say it can`t be done then i`ll try it

    • Marked as answer by PaoloCiD Wednesday, November 29, 2017 9:11 PM
    Wednesday, November 29, 2017 8:28 PM