hexagon logo

Wish Escape key 'worked'?

JUST REALIZED THIS IS IN THE WRONG FORUM. IT SHOULD BE IN CODE SAMPLES.


A little project I've been working on to handle my biggest pet peeve about PcDmis. The Escape key doesn't close every window like I wish it did, like in most other software I use. I am huge on only using the keyboard when I'm programming (as much as possible of course), I am too lazy to reach over to the mouse hehe I'm probably going to hear it but let's face it...it slows you down. PCDMIS looses focus to dialogue windows like if the developers did it on purpose. I'll stop knocking on my fav CMM software now before there are threats of switching over to something 'better' from my management.

Anyway, paste this code in a VB.NET project and please take it further and touch back with your improvements. I will keep furthering this little project until I have everything taken care of. Right now it 'should' handle all windows when the ESCAPE key is pressed it closes the dialogue. When RETURN or ENTER key is pressed it accepts or 'OK' the dialogue (Auto Feature only at the moment give me some time, I'll to the rest).



AGAIN please share your code don't be [....].

Thanks,
KP61dude!

EDIT:

Reached maximum text limit, see next post for source code.
Parents
  • #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
    
Reply
  • #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
    
Children
No Data