opentk / GLControl

WinForms control for OpenTK 4.x.
https://opentk.net
Other
54 stars 24 forks source link

How to overlay a control with transparency on GLControl. #37

Closed liungkejin closed 5 months ago

liungkejin commented 7 months ago

Hi! I have a GLControl for displaying the camera feed, and I want to place controls such as PictureBox, Button, or Label on this GLControl. I set the background of these controls to Transparent, expecting them to have a transparent background and show the rendered image of the GLControl. However, in reality, the background displayed by these overlaid controls is the background color of the GLControl, not the rendered image. So, my question is, what should I do to achieve the desired effect?

NogginBops commented 5 months ago

This issue is because when doing OpenGL rendering in WinForms the normal controls have no data on what the actual color of the control underneath has as that color data is stored on the GPU and not on the CPU where most of the WinForms rendering is done.

It would maybe be possible for us to implement a switch where we could read back the rendered data from OpenGL and expose it to WinForms, but this would significantly lower the performance of the application as you would have to sync with the GPU for every frame.

liungkejin commented 5 months ago

I achieved my desired effect by rendering controls to a bitmap, uploading it to a GL texture, and then displaying it using a GL shader.

And I disabled event capturing for the GLControl and placed the controls that need to be displayed on top of it underneath it. This way, mouse events will be passed to the controls underneath, and I only need to draw the controls in the correct position.

    public partial class OpenTKGLView : GLControl
    {
        public OpenTKGLView()
        {
        }

        // 不捕获任何的鼠标事件
        protected override void WndProc(ref Message m)
        {
            const int WM_NCHITTEST = 0x0084;
            const int HTTRANSPARENT = (-1);

            if (m.Msg == WM_NCHITTEST)
            {
                m.Result = (IntPtr)HTTRANSPARENT;
            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }

屏幕截图 2024-03-15 135555

NogginBops commented 5 months ago

Creative workaround. Good work! If you are happy with this workaround I think we should close this issue. Feel free to reopen this issue if your workaround has issues.