Click here to Skip to main content
16,005,236 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Image Reconstruction Using Genetic algorithm for Minimize the Number of projection and Maximize the Image Quality in C/C++/Matlab Pin
Dave Kreskowiak3-Aug-11 2:06
mveDave Kreskowiak3-Aug-11 2:06 
Questionresizing form with a RTB inside Pin
hang_em2-Aug-11 23:58
hang_em2-Aug-11 23:58 
AnswerRe: resizing form with a RTB inside Pin
Dave Kreskowiak3-Aug-11 2:05
mveDave Kreskowiak3-Aug-11 2:05 
GeneralRe: resizing form with a RTB inside Pin
hang_em3-Aug-11 18:01
hang_em3-Aug-11 18:01 
GeneralRe: resizing form with a RTB inside Pin
Dave Kreskowiak3-Aug-11 18:39
mveDave Kreskowiak3-Aug-11 18:39 
GeneralRe: resizing form with a RTB inside Pin
hang_em4-Aug-11 3:24
hang_em4-Aug-11 3:24 
GeneralRe: resizing form with a RTB inside Pin
Dave Kreskowiak4-Aug-11 13:55
mveDave Kreskowiak4-Aug-11 13:55 
QuestionCallbackOnCollectedDelegate was detected with LowLevelMouserProc Pin
Member 78319591-Aug-11 11:46
Member 78319591-Aug-11 11:46 
I am writing a simple mouse/keyboard global logger. This part is the Mouse event class that uses a LowLevelMouseHook to drive events. I am getting a CallbackOnCollectedDelegate was detected run-time error that I cannot figure out. I am having trouble even trying to determine where the error is happening.

Any suggestions or clues would be welcomed.

Namespace nsMouseHook
    Public Delegate Function LowLevelMouseHookProc(ByVal nCode As Integer, ByVal wParam As UInteger, ByRef lParam As MSLLHOOKSTRUCT) As Integer

    Public Structure MSLLHOOKSTRUCT
        Public pt As API_POINT
        Public mouseData As UInteger
        Public flags As UInteger
        Public time As UInteger
        Public dwExtraInfo As IntPtr
    End Structure

    Public Structure API_POINT
        Public x As Integer
        Public y As Integer
    End Structure
End Namespace


Public Class GlobalMouseHook
    Implements IDisposable

    Private hhook As IntPtr = IntPtr.Zero
    Private disposedValue As Boolean = False        ' To detect redundant calls

    Public Event MouseDown As MouseEventHandler
    Public Event MouseUp As MouseEventHandler
    Public Event MouseMove As MouseEventHandler
    Private Const WM_MOUSEMOVE As UInteger = &H200
    Private Const WM_LBUTTONDOWN As UInteger = &H201
    Private Const WM_LBUTTONUP As UInteger = &H202
    Private Const WM_MBUTTONDOWN As UInteger = &H207
    Private Const WM_MBUTTONUP As UInteger = &H208
    Private Const WM_RBUTTONDOWN As UInteger = &H204
    Private Const WM_RBUTTONUP As UInteger = &H205
    Private Const WH_MOUSE_LL As Integer = 14
   
    Private Declare Auto Function LoadLibrary Lib "kernel32" (ByVal lpFileName As String) As IntPtr
    Private Declare Auto Function SetWindowsHookEx Lib "user32.dll" (ByVal idHook As Integer, ByVal lpfn As nsMouseHook.LowLevelMouseHookProc, ByVal hInstance As IntPtr, ByVal dwThreadId As UInteger) As IntPtr
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hhk As IntPtr, ByVal nCode As Integer, ByVal wParam As UInteger, ByRef lParam As nsMouseHook.MSLLHOOKSTRUCT) As Integer
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As IntPtr) As Boolean

    Public Sub New()
             MouseHook()
            GC.KeepAlive(Me)
      End Sub

    Private Sub MouseHook()
      Dim hInstance As IntPtr = LoadLibrary("User32")
             hhook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf Me.MouseHookProc, hInstance, 0) ' AddressOf Me.HookProc,
       End Sub

    Private Sub MouseUnhook()
             UnhookWindowsHookEx(hhook)
      End Sub

    Private Function MouseHookProc(ByVal nCode As Integer, ByVal wParam As UInteger, ByRef lParam As nsMouseHook.MSLLHOOKSTRUCT) As Integer
        If nCode >= 0 Then
            Select Case wParam
                Case WM_LBUTTONDOWN
                    RaiseEvent MouseDown(Me, New MouseEventArgs(MouseButtons.Left, 0, lParam.pt.x, lParam.pt.y, 0))
                Case WM_RBUTTONDOWN
                    RaiseEvent MouseDown(Me, New MouseEventArgs(MouseButtons.Right, 0, lParam.pt.x, lParam.pt.y, 0))
                Case WM_MBUTTONDOWN
                    RaiseEvent MouseDown(Me, New MouseEventArgs(MouseButtons.Middle, 0, lParam.pt.x, lParam.pt.y, 0))
                Case WM_LBUTTONUP
                    RaiseEvent MouseUp(Me, New MouseEventArgs(MouseButtons.Left, 0, lParam.pt.x, lParam.pt.y, 0))
                Case WM_RBUTTONUP
                    RaiseEvent MouseUp(Me, New MouseEventArgs(MouseButtons.Right, 0, lParam.pt.x, lParam.pt.y, 0))
                Case WM_MBUTTONUP
                    RaiseEvent MouseUp(Me, New MouseEventArgs(MouseButtons.Middle, 0, lParam.pt.x, lParam.pt.y, 0))
                Case WM_MOUSEMOVE
                    RaiseEvent MouseMove(Me, New MouseEventArgs(WM_MOUSEMOVE, 0, lParam.pt.x, lParam.pt.y, 0))
                Case Else
                    Console.WriteLine(wParam)
            End Select
        End If
            Return CallNextHookEx(hhook, nCode, wParam, lParam)
       End Function

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
             If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: free other state (managed objects).
                End If
                MouseUnhook()
            End If
            Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
        GC.SuppressFinalize(Me)
        Dispose(True)
    End Sub

End Class


Option Strict On
Option Explicit On
Option Infer Off

Imports System.Runtime.InteropServices
Imports System.IO

Public Class MainForm
    Private intTimeOfLastEvent As Double = 0    ' Time of the last recorded event
    Private intTimeRecording As Double = 0      ' Recording time elaspesd
    Private intTimePlaying As Double = 0        ' Timer value  for playback
   
    Private WithEvents gmh As GlobalMouseHook

    Public Sub New()
        InitializeComponent()
    End Sub

    ' Form overrides dispose to clean up the component list.
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Catch ex As Exception
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    Private Sub MainForm_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If gmh IsNot Nothing Then gmh.Dispose()
    End Sub

    ''' <summary>Called when recording is started</summary>
    ''' <remarks></remarks>
    Private Sub startRecording()
        butPlay.Enabled = False
        tRecordTime.Start()
        bolRecording = True
        gmh = New GlobalMouseHook
    End Sub

    ''' <summary>Called when recording is stopped</summary>
    ''' <remarks></remarks>
    Private Sub stopRecording()
        tRecordTime.Stop()
        bolRecording = False
        gmh.Dispose()
    End Sub

    Private Sub gmh_MouseClickDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gmh.MouseDown
        lbLog.Items.Add(String.Format("Time:{0}", (intTimeRecording - intTimeOfLastEvent) / 1000))
        lbLog.Items.Add(String.Format("Mouse Click Down:{0}" & vbTab & "X:{1}" & vbTab & "Y:{2}", e.Button, e.X, e.Y))
        lbLog.SelectedIndex = lbLog.Items.Count - 1
        intTimeOfLastEvent = intTimeRecording
    End Sub

    Private Sub gmh_MouseClickUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gmh.MouseUp
        lbLog.Items.Add(String.Format("Time:{0}", (intTimeRecording - intTimeOfLastEvent) / 1000))
        lbLog.Items.Add(String.Format("Mouse Click Up:{0}" & vbTab & "X:{1}" & vbTab & "Y:{2}", e.Button, e.X, e.Y))
        intTimeOfLastEvent = intTimeRecording
        lbLog.SelectedIndex = lbLog.Items.Count - 1
    End Sub

    Private Sub gmh_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles gmh.MouseMove
        lblMousePosition.Text = e.X & ", " & e.Y
        lbLog.Items.Add("Move")
        lbLog.SelectedIndex = lbLog.Items.Count - 1
    End Sub

    Private Sub butRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butRecord.Click
        If butRecord.Text = "Record" Then
            butRecord.Text = "Stop Recording"
            startRecording()
        ElseIf butRecord.Text = "Stop Recording" Then
            butRecord.Text = "Record"
            stopRecording()
        End If
    End Sub

    Private Sub tRunTime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tRecordTime.Tick
        intTimeRecording += tRecordTime.Interval
        lblRecordTime.Text = (intTimeRecording / 1000).ToString("###.##")
        lblRecordTime.Text = String.Format("Record Time:{0:###.##}", (intTimeRecording / 1000))
    End Sub

    Private Sub butExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butExit.Click, ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

End Class

QuestionFont ComboBox add to Toolstrip Pin
nyt197231-Jul-11 3:13
professionalnyt197231-Jul-11 3:13 
AnswerRe: Font ComboBox add to Toolstrip Pin
Dave Kreskowiak31-Jul-11 4:24
mveDave Kreskowiak31-Jul-11 4:24 
GeneralRe: Font ComboBox add to Toolstrip Pin
nyt197231-Jul-11 18:36
professionalnyt197231-Jul-11 18:36 
GeneralRe: Font ComboBox add to Toolstrip Pin
Dave Kreskowiak1-Aug-11 3:49
mveDave Kreskowiak1-Aug-11 3:49 
GeneralRe: Font ComboBox add to Toolstrip Pin
nyt19722-Aug-11 1:42
professionalnyt19722-Aug-11 1:42 
GeneralRe: Font ComboBox add to Toolstrip Pin
Dave Kreskowiak2-Aug-11 3:47
mveDave Kreskowiak2-Aug-11 3:47 
GeneralRe: Font ComboBox add to Toolstrip Pin
nyt19722-Aug-11 21:21
professionalnyt19722-Aug-11 21:21 
QuestionConnection string in with sqlserver 2005 Pin
nazimghori31-Jul-11 0:30
nazimghori31-Jul-11 0:30 
AnswerRe: Connection string in with sqlserver 2005 Pin
Shameel31-Jul-11 0:53
professionalShameel31-Jul-11 0:53 
AnswerRe: Connection string in with sqlserver 2005 Pin
Eddy Vluggen31-Jul-11 2:56
professionalEddy Vluggen31-Jul-11 2:56 
AnswerRe: Connection string in with sqlserver 2005 Pin
Dave Kreskowiak31-Jul-11 4:18
mveDave Kreskowiak31-Jul-11 4:18 
GeneralRe: Connection string in with sqlserver 2005 [modified] Pin
nazimghori31-Jul-11 6:19
nazimghori31-Jul-11 6:19 
GeneralRe: Connection string in with sqlserver 2005 Pin
Dave Kreskowiak31-Jul-11 13:57
mveDave Kreskowiak31-Jul-11 13:57 
AnswerRe: Connection string in with sqlserver 2005 Pin
thatraja31-Jul-11 6:05
professionalthatraja31-Jul-11 6:05 
GeneralRe: Connection string in with sqlserver 2005 Pin
Shameel31-Jul-11 7:30
professionalShameel31-Jul-11 7:30 
GeneralRe: Connection string in with sqlserver 2005 Pin
thatraja31-Jul-11 15:37
professionalthatraja31-Jul-11 15:37 
QuestionUse MS Windows Theme Pin
ivo7530-Jul-11 21:14
ivo7530-Jul-11 21:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.