Win32's FindWindow() can find a particular window with the exact title, but what about “try.bat - Notepad”?

前端 未结 3 1871
情书的邮戳
情书的邮戳 2021-01-01 07:53

Win32\'s FindWindow() can find a window having the title of \"Untitled - Notepad\", but what if I just want to find a Notepad window but don\'t know whether it

相关标签:
3条回答
  • 2021-01-01 08:36

    I would follow Eric's advice to use EnumWindows. You can provide Ruby callbacks to Windows API functions through win32-api. Here's an example that was trivially modified from the sample in the win32-api README:

    require 'win32/api'
    include Win32
    
    # Callback example - Enumerate windows
    EnumWindows     = API.new('EnumWindows', 'KP', 'L', 'user32')
    GetWindowText   = API.new('GetWindowText', 'LPI', 'I', 'user32')
    EnumWindowsProc = API::Callback.new('LP', 'I'){ |handle, param|
      buf = "\0" * 200
      GetWindowText.call(handle, buf, 200);
    
      if (!buf.index(param).nil?)
        puts "window was found: handle #{handle}"
        0 # stop looking after we find it
      else
        1
      end
    }
    
    EnumWindows.call(EnumWindowsProc, 'Firefox')
    
    0 讨论(0)
  • 2021-01-01 08:43

    The 1st argument of FindWindow searches by class name, if you use "Notepad" (Notepad's main window class name) for this and a null title you would get the 1st matching handle irrespective of its caption.

    0 讨论(0)
  • 2021-01-01 08:46

    You almost certainly want to use the EnumWindows function; this function will call you back with a window handle, and then you can use GetWindowText to inspect the window title and find the one you want.

    Now, I have no idea how to write a callback function in Ruby, so you'll need some help there.

    0 讨论(0)
提交回复
热议问题