views:

224

answers:

2

How do I turn on/off the WS_CLIPCHILDREN window style in a Windows Forms parent control?

I would like to display some text on top of the child control after it has painted. In my parent control, this is what I have:

class Parent : public Control {
    void Parent::OnPaint(PaintEventArgs ^e){
        Control::OnPaint(e);

        // parent draws here
        // some drawing should happen over the child windows
        // in other words, do not clip child window regions
    }
};

On checking with Spy++ I find that the parent has the WS_CLIPCHILDREN window style enabled by default. What is the Windows Forms way to turn this off?

Note: Sample code is in C++/CLI but I have tagged this C# for visibility... language is immaterial here. Feel free to translate the code to C#.

A: 

You can try removing the style directly using P/Invoke using the SetWindowLong function. Use Form.Handle property as the window handle.

logicnp
+1  A: 

You might be able to do this by overriding the CreateParams property of your parent control:

protected override CreateParams CreateParams {
    get {
        new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();

        // Modify the existing style.
        CreateParams cp = base.CreateParams;
        // Remove the WS_CLIPCHILDREN flag.
        cp.Style &= ~0x02000000; // WS_CLIPCHILDREN value.

        return cp;
    }
}
Mark