tags:

views:

390

answers:

3

I'm looking for a nice way to render overlays over a native Windows form not owned by myself preferably using a library available for .NET (WinForms, GTK#, ...). Precisely, I'd like to display a few labels or text boxes at a given location on the window.

Device Context & System.Drawing: What I'm currently doing is drawing directly onto the other windows' device context, which causes flickering, as parts of the other application are redrawn in unpredictable intervals. I therefore would have to catch its WM_PAINT event using hook magic, but that's actually not as far down as I'd like to go unless there's no simpler way.

Transparent window overlay with visible child labels: another technique I tried was creating a Windows.Forms.Form with the other windows' size, using TransparencyKey to make only the children visible. This seems pretty hard to get correct, as I don't want the window to be the upper-most one but only exactly one Z-level above the foreign window. The upside would be, that I could add more behaviour to it, as I could actually handle click events, etc.

How would you implement it / deal with the problems in the two techniques described above?

+2  A: 

Definitely go with the transparent window approach as that should be simpler to implement. Creating a transparent form is very easy. You already know how to use the TransparenyKey to get the background to not drawn. Also remove the border from the window and remove the min/max/close buttons so you do not have any chrome showing.

Create your window as owned by the window of interest and it will always be on top of the target and act like a modeless dialog. I.e. it is visible only when the owning window is visible.

Phil Wright
A: 

Thanks for your answer, but I'm still a little confused. How exactly would you set the owner of a window to anything not owned by your own application? I guess that's not even possible when the other application is unmanaged, is it?

Edit:

I now got a little step closer. The example code is in Boo.

[DllImport("user32.dll", SetLastError: true, CharSet: CharSet.Auto)]
public def SetParent(child as IntPtr, parent as IntPtr):
    pass

def createAttachedForm(parentHandle as IntPtr):
  f = Form()
  f.Text = "My overlay"
  f.Show()
  SetParent(f.Handle, parentHandle)
  Application.Run(f)

Now only the TransparencyKey thing doesn't seem to work. Instead the form is completely invisible when the value is set.

bo
A: 

Setting the owner of a form can be done with

Form.Show(IWin32Window w)

where w exposes the Handle to the parent window.

Anybody know how to easily move the form when the parent window is moved ?