using ahk to close a pop up dialogue within visual

2019-02-28 00:51发布

问题:

I've remapped a few keys, which has been working fine; however, I'm having a difficult time trying to get rid of a pop up dialogue within visual studio:

Here's what I have tried:

WinWaitActive, Microsoft Visual Studio
If WinActive("Microsoft Visual Studio")
{
  WinGetText, HayStack
  Needle = "A network-related or instance-specific error"
  IfInString, Haystack, %Needle%
  {
      MsgBox, The string was found.
      return
  }
  else
      Sleep, 1
}
return

However, I get no response whatsoever.

The entire script including a few key remappings is below:

SetTitleMatchMode 2
!z::Send, !{F4}
!x::Send, !fc
!c::Send, 23481241240910324
^d::Send {End}{Shift Down}{Up}{End}{Shift Up}{Backspace}
!a::
If WinActive("Microsoft Visual Studio")
{

}
else
{
  Send !{F4}n
}
return
WinWaitActive, Microsoft Visual Studio
If WinActive("Microsoft Visual Studio")
{
  WinGetText, HayStack
  Needle = "A network-related or instance-specific error"
  IfInString, Haystack, %Needle%
  {
      MsgBox, The string was found.
      return
  }
  else
      Sleep, 1
}
return

What am I doing wrong? How do I get rid of this dialogue?

回答1:

Make sure to get the window title and text using the window spy utility from your autohotkey install folder.
There are several ways to close a window, sometimes you have to be a bit more forcefully. It's even possible that you need to run your script with admin privileges. You also don't need to define a hotkey which has to be pressed to get rid of the window. You can completely automate the whole process. Give this a try:

#Persistent ;Ensure the script doesn't exit immediately
If not A_IsAdmin ;force the script to run as admin
{
    Run *RunAs "%A_ScriptFullPath%"
    ExitApp
}

;Enter the correct win title and a correct substring of the win text and maybe also the ClassNN of the button whoch closes it
windowTitle = Microsoft Visual Studio
windowText = A network-related or instance-specific error
closeButton = Button1

;Call CloseWindow periodically passing the contents of above variables as arguments:
CloseWindowWithBoundArgument := Func("CloseWindow").bind(windowTitle, windowText, closeButton)
SetTimer, %CloseWindowWithBoundArgument%

;Wait for a window to exist, then close it:
CloseWindow(winTitle,winText,buttonClassNN) {
    WinWait, %winTitle%, %winText% ;wait until the window exists
    ;There are multiple methods to close a window:

    ;Close window method 1 (similar to pressing alt+F4)
    PostMessage, 0x112, 0xF060,,, %winTitle%, %winText%

    ;Close window method 2 (sends a WM_CLOSE message; somewhat forcefully)
    WinClose, %winTitle%, %winText%

    ;Close window method 3 (forcefully closing a window using server different methods internally)
    WinKill, %winTitle%, %winText%

    ;Close window method 4 (clicking a button to close the window)
    ControlClick, %buttonClassNN%, %winTitle%, %winText%
}


回答2:

What am I doing wrong?

  • Needle = "A network-related or instance-specific error" - String assignments don't work like that in AHK. You either use Needle := "A network..." or Needle = A network....

  • What's the point of your ALT+A-Hotkey? If the window pops up, why don't you simply press Enter??

  • If WinActive("Microsoft Visual Studio")
     {
    
     }
     else
     {
       Send !{F4}n
     }
    

    So, close the window when the popup is not active??

  • return

    Everything after this keyword doesn't get executed. Your script will never reach WinWaitActive, Microsoft Visual Studio.

btw cool name bro