Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.net Form BackGroundImage not showing in borderless customized form

Hi

In this code, which resides in the much larger piece of code shown below it I am building a bordeless moveable custom form but the background image does not show. Not sure how to remedy this.

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        FormBorderStyle = FormBorderStyle.None

        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        BackColor = Color.Transparent

        BackgroundImage = My.Resources.BackBlue

        DoubleBuffered = True
        ' Opacity = 0.9
    End Sub




Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Class Form1
    ' Set the thickness of the virtual form border that should catch the resizing
    ' If below 1, the resize function does not work!
    Private VIRTUALBORDER As Integer = 4
    Private SHOWVIRTUALBORDERS As Boolean = True

    ' Declare the limits for the form size
    Private MINHEIGHT As Integer = 10
    Private MAXHEIGHT As Integer = 300
    Private MINWIDTH As Integer = 20
    Private MAXWIDTH As Integer = 600


    Private RESIZESTART As Point
    Private RESIZEDESTINATION As Point
    Private MOUSEPOS As Point

    ' Define Rectangles & Booleans for all 9 + 1 areas of the Form.
    Private R0 As Rectangle
    Private R1 As Rectangle
    Private R2 As Rectangle
    Private R3 As Rectangle
    Private R4 As Rectangle
    Private R5 As Rectangle
    Private R6 As Rectangle
    Private R7 As Rectangle
    Private R8 As Rectangle
    Private R9 As Rectangle


    ' Bool to determine if the form is being moved (True when the form is clicked in the center area (R5))
    Private ISMOVING As Boolean

    ' Bool to determine if the form is being rezised (True when the form is clicked in all areas except the center (R5))
    Private ISREZISING As Boolean

    ' Bool's to determine in which direction the form is moving
    Private ISRESIZINGLEFT As Boolean
    Private ISRESIZINGRIGHT As Boolean

    Private ISRESIZINGTOP As Boolean
    Private ISRESIZINGBOTTOM As Boolean

    Private ISRESIZINGTOPRIGHT As Boolean
    Private ISRESIZINGTOPLEFT As Boolean

    Private ISRESIZINGBOTTOMRIGHT As Boolean
    Private ISRESIZINGBOTTOMLEFT As Boolean


    '
    '        
    '          
    '          
    '                Y
    '          
    '                |
    '                |
    '                |
    '                |    
    '                |                                                                          
    '   X   ---------|------------------------------------------------------------------------   X
    '                |                                                                          
    '                |    <-------------------------- R0 ----------------------------->
    '                |
    '                |
    '                |    |-----------------------------------------------------------|  
    '                |    | R1 |                      R2                         | R3 |
    '                |    |-----------------------------------------------------------|
    '                |    |    |                                                 |    |
    '                |    |    |                                                 |    |
    '                |    | R4 |                      R5                         | R6 |
    '                |    |    |                                                 |    |
    '                |    |    |                                                 |    |
    '                |    |-----------------------------------------------------------|  
    '                |    | R7 |                      R8                         | R9 |
    '                |    |-----------------------------------------------------------|
    '                |
    '                |
    '                |
    '                |
    '          
    '                Y                                                                                                                          

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        FormBorderStyle = FormBorderStyle.None

        Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
        BackColor = Color.Transparent

        BackgroundImage = My.Resources.BackBlue

        DoubleBuffered = True
        ' Opacity = 0.9
    End Sub


    Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Select Case e.Button
            Case MouseButtons.Left

                If R1.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGTOPLEFT = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R2.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGTOP = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R3.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGTOPRIGHT = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R4.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGLEFT = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R5.Contains(MOUSEPOS) Then
                    ' If the center area of the form is pressed (R5), then we should be able to move the form.
                    ISMOVING = True
                    ISREZISING = False
                    MOUSEPOS = New Point(e.X, e.Y)
                    Cursor = Cursors.SizeAll
                End If
                If R6.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGRIGHT = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R7.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGBOTTOMLEFT = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R8.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGBOTTOM = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                If R9.Contains(MOUSEPOS) Then
                    ISREZISING = True
                    ISRESIZINGBOTTOMRIGHT = True
                    RESIZESTART = PointToScreen(New Point(e.X, e.Y))
                End If
                Exit Select
        End Select
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        RESIZEDESTINATION = PointToScreen(New Point(e.X, e.Y))
        R0 = Bounds

        ' If the form has captured the mouse...
        If Capture Then
            If ISMOVING = True Then
                ISREZISING = False
                ' ISMOVING is true if the R5 rectangle is pressed. Allow the form to be moved around the screen.
                Location = New Point(MousePosition.X - MOUSEPOS.X, MousePosition.Y - MOUSEPOS.Y)
            End If
            If ISREZISING = True Then
                ISMOVING = False

                If ISRESIZINGTOPLEFT Then
                    Bounds = New Rectangle(R0.X + RESIZEDESTINATION.X - RESIZESTART.X, R0.Y + RESIZEDESTINATION.Y - RESIZESTART.Y, R0.Width - RESIZEDESTINATION.X + RESIZESTART.X, R0.Height - RESIZEDESTINATION.Y + RESIZESTART.Y)
                End If
                If ISRESIZINGTOP Then
                    Bounds = New Rectangle(R0.X, R0.Y + RESIZEDESTINATION.Y - RESIZESTART.Y, R0.Width, R0.Height - RESIZEDESTINATION.Y + RESIZESTART.Y)
                End If
                If ISRESIZINGTOPRIGHT Then
                    Bounds = New Rectangle(R0.X, R0.Y + RESIZEDESTINATION.Y - RESIZESTART.Y, R0.Width + RESIZEDESTINATION.X - RESIZESTART.X, R0.Height - RESIZEDESTINATION.Y + RESIZESTART.Y)
                End If
                If ISRESIZINGLEFT Then
                    Bounds = New Rectangle(R0.X + RESIZEDESTINATION.X - RESIZESTART.X, R0.Y, R0.Width - RESIZEDESTINATION.X + RESIZESTART.X, R0.Height)
                End If
                If ISRESIZINGRIGHT Then
                    Bounds = New Rectangle(R0.X, R0.Y, R0.Width + RESIZEDESTINATION.X - RESIZESTART.X, R0.Height)
                End If
                If ISRESIZINGBOTTOMLEFT Then
                    Bounds = New Rectangle(R0.X + RESIZEDESTINATION.X - RESIZESTART.X, R0.Y, R0.Width - RESIZEDESTINATION.X + RESIZESTART.X, R0.Height + RESIZEDESTINATION.Y - RESIZESTART.Y)
                End If
                If ISRESIZINGBOTTOM Then
                    Bounds = New Rectangle(R0.X, R0.Y, R0.Width, R0.Height + RESIZEDESTINATION.Y - RESIZESTART.Y)
                End If
                If ISRESIZINGBOTTOMRIGHT Then
                    Bounds = New Rectangle(R0.X, R0.Y, R0.Width + RESIZEDESTINATION.X - RESIZESTART.X, R0.Height + RESIZEDESTINATION.Y - RESIZESTART.Y)
                End If

                RESIZESTART = RESIZEDESTINATION
                Invalidate()
            End If
        Else

            ' If the form has not captured the mouse; the mouse is just hovering the form...
            MOUSEPOS = New Point(e.X, e.Y)

            ' Changes Cursor depending where the mousepointer is at the form...
            If R1.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeNWSE
            End If
            If R2.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeNS
            End If
            If R3.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeNESW
            End If
            If R4.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeWE
            End If
            If R5.Contains(MOUSEPOS) Then
                Cursor = Cursors.[Default]
            End If
            If R6.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeWE
            End If
            If R7.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeNESW
            End If
            If R8.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeNS
            End If
            If R9.Contains(MOUSEPOS) Then
                Cursor = Cursors.SizeNWSE
            End If
        End If
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        ISMOVING = False
        ISREZISING = False

        ISRESIZINGLEFT = False
        ISRESIZINGRIGHT = False

        ISRESIZINGTOP = False
        ISRESIZINGBOTTOM = False

        ISRESIZINGTOPRIGHT = False
        ISRESIZINGTOPLEFT = False

        ISRESIZINGBOTTOMRIGHT = False
        ISRESIZINGBOTTOMLEFT = False

        Invalidate()
    End Sub


    Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        ' Every time the form paints, set the location and size of the form areas...
        '#Region "DIVIDE THE FORM INTO 9 SUB AREAS"
        R1 = New Rectangle(New Point(ClientRectangle.X, ClientRectangle.Y), New Size(VIRTUALBORDER, VIRTUALBORDER))
        R2 = New Rectangle(New Point(ClientRectangle.X + R1.Width, ClientRectangle.Y), New Size(ClientRectangle.Width - (R1.Width * 2), R1.Height))
        R3 = New Rectangle(New Point(ClientRectangle.X + R1.Width + R2.Width, ClientRectangle.Y), New Size(VIRTUALBORDER, VIRTUALBORDER))

        R4 = New Rectangle(New Point(ClientRectangle.X, ClientRectangle.Y + R1.Height), New Size(R1.Width, ClientRectangle.Height - (R1.Width * 2)))
        R5 = New Rectangle(New Point(ClientRectangle.X + R4.Width, ClientRectangle.Y + R1.Height), New Size(R2.Width, R4.Height))
        R6 = New Rectangle(New Point(ClientRectangle.X + R4.Width + R5.Width, ClientRectangle.Y + R1.Height), New Size(R3.Width, R4.Height))

        R7 = New Rectangle(New Point(ClientRectangle.X, ClientRectangle.Y + R1.Height + R4.Height), New Size(VIRTUALBORDER, VIRTUALBORDER))
        R8 = New Rectangle(New Point(ClientRectangle.X + R7.Width, ClientRectangle.Y + R1.Height + R4.Height), New Size(ClientRectangle.Width - (R7.Width * 2), R7.Height))
        R9 = New Rectangle(New Point(ClientRectangle.X + R7.Width + R8.Width, ClientRectangle.Y + R1.Height + R4.Height), New Size(VIRTUALBORDER, VIRTUALBORDER))
        '#End Region


        '#Region "SET FILL COLORS FOR THE VIRTUAL BORDER"
        If SHOWVIRTUALBORDERS Then
            Dim GFX As Graphics = e.Graphics
            GFX.FillRectangle(Brushes.White, R5)

            GFX.FillRectangle(Brushes.Gold, R1)
            GFX.FillRectangle(Brushes.Gold, R3)
            GFX.FillRectangle(Brushes.Gold, R7)
            GFX.FillRectangle(Brushes.Gold, R9)

            GFX.FillRectangle(Brushes.Blue, R2)
            GFX.FillRectangle(Brushes.Blue, R8)
            GFX.FillRectangle(Brushes.Blue, R4)
            GFX.FillRectangle(Brushes.Blue, R6)
        End If
        '#End Region
    End Sub

    Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        'CODE TO LIMIT THE SIZE OF THE FORM
        If Height > MAXHEIGHT Then
            Capture = False
            Height = MAXHEIGHT
        End If
        If Height < MINHEIGHT Then
            Capture = False
            Height = MINHEIGHT
        End If
        If Width > MAXWIDTH Then
            Capture = False
            Width = MAXWIDTH
        End If
        If Width < MINWIDTH Then
            Capture = False
            Width = MINWIDTH
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

Thanks very much