开发者

Can I draw on a Form, above all controls? [duplicate]

开发者 https://www.devze.com 2023-03-27 21:32 出处:网络
This question already has answers here: Draw semi transparent overlay image all over the windows form having some controls
This question already has answers here: Draw semi transparent overlay image all over the windows form having some controls (3 answers) 开发者_如何转开发 Closed 1 year ago.

Is it possible for me to draw ABOVE all controls on a form?
I have some controls (textboxes, buttons, COM objects) on my form, and I wish to draw ON them, overriding any pixels previously drawn by them.
I am using Windows Forms on C#.
NOTE: the Graphics class draws under the controls...


I know I'm way too late in the game, and there is nothing wrong with the accepted answer, but I found it too complicated and somehow hard to understand, so I come up with a "hack" for it.

1. Make a custom Panel with the following code.

public class TransparentPanel : Panel
{
    protected override void OnPaint(PaintEventArgs e)
    {
        
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

            return cp;
        }
    }
}

2. Create a TransparentPanel exactly the same size as the Form, over every control currently on the Form, bring it to the front, and set its BackColor to Transparent, Enable to false.

3. Now everything you draw within OnPaint will be drawn "above" any controls, and it will not "block" any interactable controls such as TextBox.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号