【问题标题】:How to add a button into a nother application's window using C# [closed]如何使用 C# 将按钮添加到另一个应用程序的窗口中 [关闭]
【发布时间】:2014-01-10 03:26:02
【问题描述】:

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

详情如下:

  • 我写了一个在MS窗口上运行的应用程序,当它运行时,它会找到应用程序“Skype”的主窗口。 (因为,这个任务只有在Skype运行时才会运行)
  • 如果找到,它将在Skype的主窗口中添加一个按钮。当用户按下它时,我的应用程序会做一些事情。

我正在使用 C#。

非常感谢

【问题讨论】:

  • 您的问题中没有足够的细节来回答这个问题。什么样的应用程序?
  • 所以您想将自己的一个按钮推送到另一个应用程序(如 Skype)中,并让它做事,与该应用程序交互?嗯..没有。
  • 如果我没记错的话,Skype 确实有一个 API,你可以为它编写一个扩展程序。
  • 致所有的关闭者:这有什么不清楚的地方?似乎是一个体面的问题和一个体面的答案。提名重新开放。

标签: c# window add


【解决方案1】:

试试这个(使用 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);
    }
  }
}

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);
}

提示:使用Spy++ 查找窗口类名

【讨论】:

  • 我已经测试并发现了这样的错误 WindowsFormsApplication1.exe 中发生了“System.DllNotFoundException”类型的未处理异常所以,我应该更改代码 [DllImport("User32.dll, SetLastError = true")] To [DllImport("user32.dll", EntryPoint = "FindWindow")] .....顺便说一句,我将您的答案投票为已接受。谢谢你。
  • 糟糕!我在那里放错了一个引号字符。应该是[DllImport("User32.dll", SetLastError = true)] 而不是[DllImport("User32.dll, SetLastError = true")]。感谢您接受它作为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多