作为标题,我想在另一个应用程序的窗口中添加一个按钮并处理它。
细节是:
我正在使用C#。
非常感谢
答案 0 :(得分:8)
试试这个(使用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++查找窗口className