add new clipboard watcher class

This commit is contained in:
Jonathan Jenne 2019-10-22 16:08:33 +02:00
parent 42a3aad5e2
commit be7e314b32
5 changed files with 146 additions and 100 deletions

View File

@ -0,0 +1,75 @@
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class ClassClipboardWatcher : Inherits NativeWindow : Implements IDisposable
<DllImport("user32", EntryPoint:="AddClipboardFormatListener")>
Private Shared Function AddClipboardFormatListener(ByVal hWnd As IntPtr) As Boolean
End Function
<DllImport("user32", EntryPoint:="RemoveClipboardFormatListener")>
Private Shared Function RemoveClipboardFormatListener(ByVal hWnd As IntPtr) As Boolean
End Function
Public Event Changed As EventHandler(Of String)
Private ReadOnly _Handle As IntPtr
Private _IsDisposed As Boolean = False
'nur eine globale Instanz zulassen
Public Shared Singleton As New ClassClipboardWatcher
Const WM_CLIPBOARDUPDATE As Integer = &H31D
Private Sub New()
MyBase.CreateHandle(New CreateParams)
_Handle = AddClipboardFormatListener(Handle)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_CLIPBOARDUPDATE
Dim oWindow = ClassWindowAPI.GetWindowInfo()
If IsNothing(oWindow.ProcessName) Then
Exit Sub
End If
If oWindow.ProcessName.StartsWith("DD_Clipboard_Watcher") Then
Exit Sub
End If
Dim oText = Clipboard.GetText
RaiseEvent Changed(Me, oText)
End Select
MyBase.WndProc(m)
End Sub
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not _IsDisposed Then
If disposing Then
' TODO: Verwaltete Ressourcen freigeben, wenn sie explizit
' aufgerufen werden
End If
MyBase.DestroyHandle()
RemoveClipboardFormatListener(_Handle)
End If
_IsDisposed = True
End Sub
' Dieser Code wird von Visual Basic hinzugefügt, um das Dispose-Muster
' richtig zu implementieren.
Public Sub Dispose() Implements IDisposable.Dispose
' Sie sollten diesen Code nicht ändern, sondern stattdessen ihren
' Bereinigungscode oben in
' Dispose(ByVal disposing As Boolean) einfügen.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
MyBase.Finalize()
Dispose(False)
End Sub
End Class

View File

@ -149,6 +149,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ClassAutomation.vb" />
<Compile Include="ClassClipboardWatcher.vb" />
<Compile Include="ClassWindow.vb" />
<Compile Include="ClassConfig.vb" />
<Compile Include="ClassConstants.vb" />

View File

@ -26,18 +26,13 @@ Partial Class frmControlCapture
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(frmControlCapture))
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.Label3 = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.btnOK = New System.Windows.Forms.Button()
Me.btnCancel = New System.Windows.Forms.Button()
Me.rbControlName = New System.Windows.Forms.RadioButton()
Me.rbControlPosition = New System.Windows.Forms.RadioButton()
Me.gbControlName = New System.Windows.Forms.GroupBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtControlName = New System.Windows.Forms.TextBox()
Me.gbControlPosition = New System.Windows.Forms.GroupBox()
Me.txtControlBounds = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.gbControlName.SuspendLayout()
Me.gbControlPosition.SuspendLayout()
Me.SuspendLayout()
'
'Timer1
@ -49,38 +44,38 @@ Partial Class frmControlCapture
'
Me.Label3.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label3.Font = New System.Drawing.Font("Tahoma", 9.75!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(12, 269)
Me.Label3.Font = New System.Drawing.Font("Tahoma", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(9, 141)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(383, 27)
Me.Label3.TabIndex = 11
Me.Label3.Text = "Auswertung von aktivem Feld läuft!" & Global.Microsoft.VisualBasic.ChrW(13) & Global.Microsoft.VisualBasic.ChrW(10)
'
'Button1
'btnOK
'
Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.Button1.DialogResult = System.Windows.Forms.DialogResult.OK
Me.Button1.Image = Global.DD_Clipboard_Watcher.My.Resources.Resources.add
Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.Button1.Location = New System.Drawing.Point(12, 299)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(159, 34)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Feld zuordnen"
Me.Button1.UseVisualStyleBackColor = True
Me.btnOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnOK.Image = Global.DD_Clipboard_Watcher.My.Resources.Resources.add
Me.btnOK.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.btnOK.Location = New System.Drawing.Point(12, 190)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(159, 34)
Me.btnOK.TabIndex = 0
Me.btnOK.Text = "Feld zuordnen"
Me.btnOK.UseVisualStyleBackColor = True
'
'Button2
'btnCancel
'
Me.Button2.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button2.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.Button2.Image = Global.DD_Clipboard_Watcher.My.Resources.Resources.cancel
Me.Button2.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.Button2.Location = New System.Drawing.Point(236, 299)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(159, 34)
Me.Button2.TabIndex = 0
Me.Button2.Text = "Abbruch"
Me.Button2.UseVisualStyleBackColor = True
Me.btnCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Image = Global.DD_Clipboard_Watcher.My.Resources.Resources.cancel
Me.btnCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.btnCancel.Location = New System.Drawing.Point(236, 190)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(159, 34)
Me.btnCancel.TabIndex = 0
Me.btnCancel.Text = "Abbruch"
Me.btnCancel.UseVisualStyleBackColor = True
'
'rbControlName
'
@ -97,106 +92,74 @@ Partial Class frmControlCapture
'rbControlPosition
'
Me.rbControlPosition.AutoSize = True
Me.rbControlPosition.Location = New System.Drawing.Point(12, 118)
Me.rbControlPosition.Location = New System.Drawing.Point(12, 36)
Me.rbControlPosition.Name = "rbControlPosition"
Me.rbControlPosition.Size = New System.Drawing.Size(100, 17)
Me.rbControlPosition.TabIndex = 14
Me.rbControlPosition.Text = "Control Position"
Me.rbControlPosition.UseVisualStyleBackColor = True
'
'gbControlName
'
Me.gbControlName.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.gbControlName.Controls.Add(Me.Label2)
Me.gbControlName.Controls.Add(Me.txtControlName)
Me.gbControlName.Location = New System.Drawing.Point(140, 12)
Me.gbControlName.Name = "gbControlName"
Me.gbControlName.Size = New System.Drawing.Size(255, 100)
Me.gbControlName.TabIndex = 15
Me.gbControlName.TabStop = False
Me.gbControlName.Text = "Control Name"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(6, 49)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(243, 48)
Me.Label2.TabIndex = 0
Me.Label2.Text = "Markieren Sie Text in einem Feld und Kopieren Sie Text"
'
'txtControlName
'
Me.txtControlName.Location = New System.Drawing.Point(6, 20)
Me.txtControlName.Location = New System.Drawing.Point(152, 8)
Me.txtControlName.Name = "txtControlName"
Me.txtControlName.ReadOnly = True
Me.txtControlName.Size = New System.Drawing.Size(243, 21)
Me.txtControlName.TabIndex = 0
'
'gbControlPosition
'
Me.gbControlPosition.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.gbControlPosition.Controls.Add(Me.txtControlBounds)
Me.gbControlPosition.Controls.Add(Me.Label1)
Me.gbControlPosition.Location = New System.Drawing.Point(140, 118)
Me.gbControlPosition.Name = "gbControlPosition"
Me.gbControlPosition.Size = New System.Drawing.Size(255, 137)
Me.gbControlPosition.TabIndex = 15
Me.gbControlPosition.TabStop = False
Me.gbControlPosition.Text = "Control Position"
'
'txtControlBounds
'
Me.txtControlBounds.Location = New System.Drawing.Point(9, 20)
Me.txtControlBounds.Location = New System.Drawing.Point(152, 35)
Me.txtControlBounds.Multiline = True
Me.txtControlBounds.Name = "txtControlBounds"
Me.txtControlBounds.Size = New System.Drawing.Size(240, 77)
Me.txtControlBounds.ReadOnly = True
Me.txtControlBounds.Size = New System.Drawing.Size(240, 88)
Me.txtControlBounds.TabIndex = 1
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(6, 100)
Me.Label1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(9, 168)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(243, 34)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Wechseln Sie durch die aktiven Anwendungen und klicken Sie in ein Feld"
Me.Label1.Size = New System.Drawing.Size(298, 13)
Me.Label1.TabIndex = 15
Me.Label1.Text = "Markieren Sie Text in einem Feld und Kopieren Sie einen Text"
'
'frmControlCapture
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(407, 345)
Me.Controls.Add(Me.gbControlPosition)
Me.Controls.Add(Me.gbControlName)
Me.ClientSize = New System.Drawing.Size(407, 236)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtControlBounds)
Me.Controls.Add(Me.txtControlName)
Me.Controls.Add(Me.rbControlPosition)
Me.Controls.Add(Me.rbControlName)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.btnOK)
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "frmControlCapture"
Me.ShowIcon = False
Me.Text = "Feld Auswertung"
Me.TopMost = True
Me.gbControlName.ResumeLayout(False)
Me.gbControlName.PerformLayout()
Me.gbControlPosition.ResumeLayout(False)
Me.gbControlPosition.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Timer1 As Timer
Friend WithEvents Label3 As Label
Friend WithEvents Button1 As Button
Friend WithEvents Button2 As Button
Friend WithEvents btnOK As Button
Friend WithEvents btnCancel As Button
Friend WithEvents rbControlName As RadioButton
Friend WithEvents rbControlPosition As RadioButton
Friend WithEvents gbControlName As GroupBox
Friend WithEvents txtControlName As TextBox
Friend WithEvents gbControlPosition As GroupBox
Friend WithEvents Label2 As Label
Friend WithEvents Label1 As Label
Friend WithEvents txtControlBounds As TextBox
Friend WithEvents Label1 As Label
End Class

View File

@ -7,10 +7,11 @@ Public Class frmControlCapture
Public Property BottomLeft As RectangleInfo
Public Property BottomRight As RectangleInfo
Public Property ControlName As String
Public Property ControlName As String = String.Empty
Public Property ControlBounds As String
Private WithEvents Watcher As ClipboardWatcher = ClipboardWatcher.Singleton
'Private WithEvents Watcher As ClipboardWatcher = ClipboardWatcher.Singleton
Private WithEvents Watcher2 As ClassClipboardWatcher = ClassClipboardWatcher.Singleton
Private Window As Window
Private EditMode As Boolean = False
@ -41,10 +42,11 @@ Public Class frmControlCapture
txtControlBounds.Text = ControlBounds
End If
AddHandler Watcher.Changed, AddressOf Watcher_Changed
'AddHandler Watcher.Changed, AddressOf Watcher_Changed
AddHandler Watcher2.Changed, AddressOf Watcher_Changed
End Sub
Private Sub Watcher_Changed(sender As Object, e As EventArgs)
Private Sub Watcher_Changed(sender As Object, e As String)
Try
' === CONTROL NAME ===
Dim oControl As WindowInfo = Window.GetFocusedControl(Handle)
@ -120,16 +122,20 @@ Public Class frmControlCapture
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles rbControlName.CheckedChanged
gbControlName.Enabled = rbControlName.Checked
gbControlPosition.Enabled = Not rbControlName.Checked
txtControlName.Enabled = rbControlName.Checked
txtControlBounds.Enabled = Not rbControlName.Checked
btnOK.Enabled = rbControlName.Checked And ControlName <> String.Empty
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles rbControlPosition.CheckedChanged
gbControlName.Enabled = Not rbControlPosition.Checked
gbControlPosition.Enabled = rbControlPosition.Checked
txtControlBounds.Enabled = rbControlPosition.Checked
txtControlName.Enabled = Not rbControlPosition.Checked
btnOK.Enabled = rbControlPosition.Checked And TopLeft IsNot Nothing
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnOK.Click
If txtControlName.Text = String.Empty And txtControlBounds.Text = String.Empty Then
MsgBox("Kein Control gefunden!")
DialogResult = DialogResult.Cancel

View File

@ -8,7 +8,8 @@ Imports DigitalData.Modules.ClipboardWatcher
Public Class frmStart
Private WithEvents Hotkey As New ClassHotkey(Me)
Private WithEvents _Watcher As ClipboardWatcher = ClipboardWatcher.Singleton
'Private WithEvents _Watcher As ClipboardWatcher = ClipboardWatcher.Singleton
Private WithEvents _Watcher2 As ClassClipboardWatcher = ClassClipboardWatcher.Singleton
Private _MatchForm As frmMatch = Nothing
Public Sub New()
@ -97,10 +98,10 @@ Public Class frmStart
End Sub
Private Sub frmClipboardWatch_Disposed(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Disposed
_Watcher.Dispose()
_Watcher2.Dispose()
End Sub
Private Sub WatcherChanged_New(ByVal sender As Object, ByVal e As EventArgs) Handles _Watcher.Changed
Private Sub WatcherChanged_New(ByVal sender As Object, ByVal e As String) Handles _Watcher2.Changed
If MONITORING_ACTIVE = False Then
Logger.Info("Clipboard Watcher is inactive!")
'NotifyIconMain.ShowBalloonTip(20000, "Clipboard Watcher", "Clipboard-watcher is inactive.", ToolTipIcon.Info)