I've managed to add a button to another application, in this case its notepad for testing purposes.
var w1 = NativeMethods.GetShellWindow();
if (w1 != IntPtr.Zero)
{
var hWndNotepad = TopLevelWindowUtils.FindWindow(wh => wh.GetWindowText().Contains("Notepad"));
IntPtr hWndEdit = NativeMethods.FindWindowEx(hWndNotepad.RawPtr, IntPtr.Zero, "Edit", null);
var button = new Button { Text = "Click Me!", Left = 5, Top = 5, Width = 75, Height = 75 };
button.Click += (o, args) => { MessageBox.Show("You've clicked me"); };
NativeMethods.SetParent(button.Handle, hWndEdit);
btHandle = button.Handle;
btns.Add(button);
Timer timer = new Timer();
timer.Interval = (1 * 1000); // 10 secs
timer.Tick += Timer_Tick;
timer.Start();
}
This fires fine and I see the button in notepad:
However when I lose focus of the window, minimize etc, the button disappears. How do I keep it there?
I've tried to SendMessage on a timer to repaint, but it doesnt work:
private void Timer_Tick(object sender, EventArgs e)
{
var w1 = NativeMethods.GetShellWindow();
if (w1 != IntPtr.Zero)
{
var hWndNotepad = TopLevelWindowUtils.FindWindow(wh => wh.GetWindowText().Contains("Notepad"));
IntPtr hWndEdit = NativeMethods.FindWindowEx(hWndNotepad.RawPtr, IntPtr.Zero, "Edit", null);
NativeMethods.SendMessage(btHandle, NativeMethods.WM_PAINT, IntPtr.Zero, IntPtr.Zero);
}
}
Can you help?
PS. im trying to run this as a windows service so that when the service is running, it always has the button in the other application - in this case notepad.