如何使用C#将按钮添加到其他应用程序的窗口中

Võ *_*Hòa -1 c# window add

作为标题,我想在另一个应用程序的窗口添加一个按钮并处理它.

细节是:

  • 我编写了一个在MS窗口上运行的应用程序,当它运行时,它会找到应用程序"Skype"的主窗口.(原因,此任务仅在运行Skype时运行)
  • 如果找到,它将在Skype的主窗口中添加一个按钮.我的应用程序将在用户按下时执行某些操作.

我正在使用C#.

非常感谢

Iro*_*eek 9

试试这个(使用Skype for Windows Desktop版本6.11.0.102测试)

public void AttachButtonToSkype() {
  // find skype main window (className = tSkMainForm)
  var mainHandle = NativeMethods.FindWindow("tSkMainForm", null);

  if (mainHandle != IntPtr.Zero) {
    // find child window to inject (className = TMyselfControl)
    var parentHandle = NativeMethods.FindWindowEx(mainHandle, IntPtr.Zero, "TMyselfControl", null);

    if (parentHandle != IntPtr.Zero)
    {          
      var button = new Button { Text = "Click Me!", Left = 150, Top = 5, Width = 75, Height = 25 };
      button.Click += (o, args) => { MessageBox.Show("You've clicked me"); };

      NativeMethods.SetParent(button.Handle, parentHandle);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

NativeMethods

internal class NativeMethods {
  [DllImport("User32.dll", SetLastError = true)]
  public static extern IntPtr FindWindow(string ClassN, string WindN);

  [DllImport("user32.dll", SetLastError = true)]
  public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

  [DllImport("user32.dll", SetLastError = true)]
  public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
}
Run Code Online (Sandbox Code Playgroud)

提示:使用Spy ++查找window className