Your Products have been synced, click here to refresh
Imports System.Text Imports System.Runtime.InteropServices Imports System.ComponentModel Public Class PcdHelper #Region "Global Vars" 'Globals Private ParentHandle As IntPtr Private pcdTask As Integer Public PcdWindows() As String = { "Auto Feature",'00 "Alignment Utilities",'01 "Probe Utilities",'02 "CAD Transform",'03 "CAD Assembly",'04 "CAD and Graphic Setup",'05 "Angle",'06 "Angularity",'07 "Circularity",'08 "Coaxiality",'09 "Concentricity",'10 "Cylindricity",'11 "Distance",'12 "Flatness",'13 "Feature Location",'14 "Parallelism",'15 "Perpendicularity",'16 "Position",'17 "Position In-Between",'18 "Line Profile",'19 "Surface Profile",'20 "Circular Runout",'21 "Total Runout",'22 "Straightness",'23 "Symmetry",'24 "Pattern Offset",'25 "Hide and Show Graphics",'26 "Dimensional Keyin",'27 "No Task Found" '28 Must remain last and must not change! } Enum KeyModifier None = 0 Alt = &H1 Control = &H2 Shift = &H4 Winkey = &H8 End Enum 'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc. Private Const BM_CLICK As Integer = &HF5 Public Const WM_HOTKEY As Integer = &H312 'Find virtual key codes here: https://docs.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes using decimal values use this http://www.bluesock.org/~willg/dev/ascii.html Public Const VK_ESCAPE As Integer = 27 'Escape Key Public Const VK_RETURN As Integer = 13 ' Return Key #End Region
#Region "WinAPI Declarations" <DllImport("user32.dll")> Private Shared Function _ EnumWindows( ByVal ewp As Callback, ByVal lParam As Integer ) As Integer End Function <DllImport("user32.dll")> Private Shared Function _ IsWindowVisible( ByVal hWnd As Integer ) As Boolean End Function <DllImport("user32.dll")> Private Shared Function _ GetWindowText( ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer ) As Integer End Function Public Delegate Function _ Callback( ByVal hwnd As Integer, ByVal lParam As Integer ) As Boolean <DllImport("user32.dll", EntryPoint:="SendMessageW")> Private Shared Function _ SendMessageW( ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr ) As Integer End Function <DllImport("user32.dll")> Private Shared Function _ FindWindowEx( ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String ) As IntPtr End Function Private Declare Function _ RegisterHotKey Lib "user32" ( ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer ) As Integer Private Declare Function _ UnregisterHotKey Lib "user32" ( ByVal hwnd As IntPtr, ByVal id As Integer ) As Integer #End Region Private Sub btn_TestRun_Click(sender As Object, e As EventArgs) Handles btn_TestRun.Click 'Moved to handlehotkeyevent 'FindPcdWindows() 'If Not pcdTask = 28 Then 'No Task Found (last index of PcdWindows()) ' PcdDoTask() 'End If End Sub Public Sub FindPcdWindows() 'Check if PcDmis is running, else do nothing For Each p As Process In Process.GetProcesses() If (p.MainWindowTitle.StartsWith("PC-DMIS")) Then 'Find windows and loads ParentHandle EnumWindows(AddressOf FnBoolVentanasListar, 0) Exit For End If Next p End Sub Public Sub PcdDoTask(Optional ByVal taskID As Integer = 1) Select Case pcdTask Case 0 AutoFeatWin(taskID) Case Else CloseOtherWin() End Select End Sub #Region "Window Finder" Function FnBoolVentanasListar(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean Dim strTitle As New StringBuilder Dim pstrTitle As String Dim intTexto As Integer Dim isVis As Boolean On Error GoTo ErrFNBoolVentanasListar 'Clear var strTitle.Clear() 'Add space strTitle.Append(Space$(1024)) If Not isVis = IsWindowVisible(hwnd) Then intTexto = GetWindowText(hwnd, strTitle, 1024) pstrTitle = strTitle.ToString 'If (strTitle.ToString.Contains("Auto Feature")) Then If (PcdWindows.Contains(pstrTitle, StringComparer.CurrentCultureIgnoreCase)) Then ParentHandle = hwnd pcdTask = Array.IndexOf(PcdWindows, pstrTitle) Return False Else 'If (strTitle.ToString.Contains("Auto Feature")) Then For i = 0 To PcdWindows.Length - 2 '2 instead of 1 to ensure last string is omitted If pstrTitle <> "" Then If pstrTitle.Contains(PcdWindows(i)) Then ParentHandle = hwnd pcdTask = i Return False Exit For End If End If Next i pcdTask = PcdWindows.Length - 1 'No window found. Assign last array index. End If End If ErrFNBoolVentanasListar: Return True End Function #End Region #Region "PCD Window Actions" Sub AutoFeatWin(Optional ByVal taskID As Integer = 1) Dim ChWnd As IntPtr = IntPtr.Zero Dim ChWnd1 As IntPtr = IntPtr.Zero Dim ChWnd2 As IntPtr = IntPtr.Zero Dim BhWnd As IntPtr = IntPtr.Zero Dim btnSwitcher As String = "Close" If Not taskID = 1 Then btnSwitcher = "OK" End If ChWnd = FindWindowEx(' First Child Handle ParentHandle, Nothing, "AfxControlBar110u", Nothing) 'was "Auto Feature [CIR8]" If ChWnd.Equals(IntPtr.Zero) Then Exit Sub End If ChWnd1 = FindWindowEx(' Second Child Handle ChWnd, Nothing, "Afx:0000000140000000:8:0000000000010005:00000000011000A8:0000000000000000", Nothing) 'was "Auto Feature [CIR8]" If ChWnd1.Equals(IntPtr.Zero) Then Exit Sub End If ChWnd2 = FindWindowEx(' Third Child Handle ChWnd1, Nothing, "Static", "") If ChWnd1.Equals(IntPtr.Zero) Then Exit Sub End If BhWnd = FindWindowEx(' Button Handle ChWnd2, Nothing, "Button", btnSwitcher) If BhWnd.Equals(IntPtr.Zero) Then BhWnd = FindWindowEx(' Button Handle ChWnd2, Nothing, "Button", "Cancel") If BhWnd.Equals(IntPtr.Zero) Then Exit Sub End If End If 'Click the 'close' button SendMessageW(BhWnd, BM_CLICK, 0, 0) End Sub Sub CloseOtherWin() Dim BhWnd As IntPtr = IntPtr.Zero Dim btnAction As String = "" Select Case pcdTask Case 1 To 5, 25, 26 btnAction = "Cancel" Case Else btnAction = "Close" End Select BhWnd = FindWindowEx(' Button Handle ParentHandle, Nothing, "Button", btnAction) If BhWnd.Equals(IntPtr.Zero) Then MsgBox("Didn't find Dim Window.") Exit Sub End If 'Click the 'close' button SendMessageW(BhWnd, BM_CLICK, 0, 0) End Sub #End Region #Region "Hotkey registration, unregistration and handling" Public Shared Sub registerhotkey(ByRef sourceform As Form, ByVal hotkeyID As Integer, ByVal triggerkey As UInteger, ByVal modifier As KeyModifier) RegisterHotKey(sourceform.Handle, hotkeyID, modifier, triggerkey) 'asc(triggerkey.toupper)) End Sub Public Shared Sub unregisterhotkeys(ByRef sourceform As Form) UnregisterHotKey(sourceform.Handle, 1) 'remember to call unregisterhotkeys() when closing your application. UnregisterHotKey(sourceform.Handle, 2) End Sub Public Sub handlehotkeyevent(ByVal hotkeyID As IntPtr) Select Case hotkeyID Case 1 'MsgBox("the hotkey was pressed " & hotkeyid.ToString) FindPcdWindows() If Not pcdTask = 28 Then 'No Task Found (last index of PcdWindows()) PcdDoTask(hotkeyID) End If Case 2 FindPcdWindows() If Not pcdTask = 28 Then 'No Task Found (last index of PcdWindows()) PcdDoTask(hotkeyID) End If End Select End Sub Protected Overrides Sub wndproc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_HOTKEY Then handlehotkeyevent(m.WParam) End If MyBase.WndProc(m) End Sub 'system wide hotkey event handling Private Sub pcdhelper_load(sender As Object, e As EventArgs) Handles MyBase.Load registerhotkey(Me, 1, VK_ESCAPE, KeyModifier.None) registerhotkey(Me, 2, VK_RETURN, KeyModifier.[COLOR=#FF0000]Alt[/COLOR]) End Sub Private Sub pcdhelper_closing(sender As Object, e As CancelEventArgs) Handles Me.Closing unregisterhotkeys(Me) End Sub #End Region End Class
© 2024 Hexagon AB and/or its subsidiaries. | Privacy Policy | Cloud Services Agreement |