Results 1 to 4 of 4

Thread: Bringing transparent control to front (over video)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    20

    Question Bringing transparent control to front (over video)

    Hi all, I'm trying to bring a transparent user control (being used as a drawing layer) to the front, over a page that will be playing video. Me.BringToFront() didn't seem to work, and when I tried Me.SetTopLevel(true) in the load, this gave me an InvalidOperationException. Here's what I have so far, sorry for the commented lines.. lots of trial and error (I'm very new, just an intern at the moment)

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class ucDrawingSurface
        Dim myBrush As New SolidBrush(Color.Blue)
        Dim myPen As New Pen(myBrush, 8)
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim g As Graphics = e.Graphics
            g.DrawLine(myPen, 0, 0, Me.Height, Me.Width)
            ' Me.BringToFront()
        End Sub
    
        Private Const WS_EX_TRANSPARENT As Int32 = &H20
    
        Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dx As Int32, ByVal dy As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32)
    
    
        Private Const MOUSEEVENTF_LEFTDOWN As Integer = &H2
        Private Const MOUSEEVENTF_LEFTUP As Integer = &H4
        Private Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8
        Private Const MOUSEEVENTF_RIGHTUP As Integer = &H10
    
        Public Sub DoMouseClick()
            Const zero As Integer = 0
            Const one As Integer = 1
            Dim x As Int32 = Cursor.Position.X * 65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width
            Dim y As Int32 = Cursor.Position.Y * 65535 / System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height
            mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, x, y, zero, one)
        End Sub
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
    
            ' This allows the control to be transparent
            Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            Me.UpdateStyles()
            Me.BackColor = Color.Transparent
    
        End Sub
    
        Protected Overrides ReadOnly Property CreateParams() As CreateParams
            Get
                Dim cp As CreateParams = MyBase.CreateParams
                cp.ExStyle = cp.ExStyle Or WS_EX_TRANSPARENT
    
                Return cp
            End Get
        End Property
    
        Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
    
        End Sub
    
        Private Sub ucDrawingSurface_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'Control.CheckForIllegalCrossThreadCalls = False
            ' Me.SetTopLevel(True)
        End Sub
    
        Private Sub ucDrawingSurface_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
            Me.Hide()
            DoMouseClick()
            'Me.Show()
            'Me.Enabled = True
            bgw.RunWorkerAsync(Me)
            'bgw.ReportProgress(0, "Show")
        End Sub
    
        Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
            ' CType(e.Argument, ucDrawingSurface).Show()
            'CType(e.Argument, ucDrawingSurface).Focus()
            Threading.Thread.Sleep("10")
            showMe()
        End Sub
    
        Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
            If Not e.UserState = Nothing Then
                If e.UserState = "Show" Then
                    Me.Show()
                End If
            End If
        End Sub
    
        Private Sub showMe()
            If Me.InvokeRequired = True Then
                Me.Invoke(New MethodInvoker(AddressOf showMe))
            Else
                Me.Show()
            End If
        End Sub
    End Class

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    20

    Re: Bringing transparent control to front (over video)

    Also: The brush being used is just me drawing a test line over the user control. I needed to make the control so that you can click through pure transparency (to click a button, etc) and so that you may not click through something that you have drawn on the transparent surface (clicking on a drawn object will allow the user to edit / move the drawn object they've clicked on.)

  3. #3

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2010
    Posts
    20

    Re: Bringing transparent control to front (over video)

    I looked over that thread but it didn't really seem to have the answer I'm looking for. I've been at this road block for a while and in narrowing down possibilities for a solution, people keep pointing to WPF, though i'm not sure if that is a valid way of doing what I'm trying to do. Any new suggestions?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width