Draggable borderless window in CefSharp

后端 未结 2 938
我在风中等你
我在风中等你 2021-01-07 09:24

I want to implement borderless window with drag logic on some HTML element. I found some working examples (like frameless window for Chrome) and this is what I\'ve tried:

相关标签:
2条回答
  • 2021-01-07 09:37

    UPDATE2. I DID IT. This is what I've added to my form code:

    IntPtr DragableRegionNative = Native.CreateRectRgn(0, 0, 0, 0);
    
        void RegionsChangedCallback(DraggableRegion[] Regions)
        {
    
            Native.SetRectRgn(DragableRegionNative, 0, 0, 0, 0);
    
            if (Regions == null)
                return;
    
            foreach (var Region in Regions)
            {
                var RegionNative = Native.CreateRectRgn(
                    Region.X, Region.Y,
                    Region.X + Region.Width,
                    Region.Y + Region.Height);
    
                Native.CombineRgn(DragableRegionNative, DragableRegionNative, RegionNative,
                    Region.Draggable ? (int)Native.CombineRgnStyles.RGN_OR : (int)Native.CombineRgnStyles.RGN_DIFF);
    
                Native.DeleteObject(RegionNative);
            }
        }
    
    
        Point dragOffset = new Point();
    
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
    
            if (e.Button == MouseButtons.Left)
            {
                dragOffset = this.PointToScreen(e.Location);
                dragOffset.X -= Location.X;
                dragOffset.Y -= Location.Y;
            }
        }
    
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
    
            if (e.Button == MouseButtons.Left)
            {
                Point newLocation = this.PointToScreen(e.Location);
    
                newLocation.X -= dragOffset.X;
                newLocation.Y -= dragOffset.Y;
    
                Location = newLocation;
            }
        }
    
        void chromewb_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e)
        {
            if (chromewb.IsBrowserInitialized)
            {
                ChromeWidgetMessageInterceptor.SetupLoop(chromewb, (m) =>
                {
                    if (m.Msg == (int)Native.WM.WM_LBUTTONDOWN)
                    {
                        var point = Native.ParsePoint(m.LParam.ToInt32());
    
                        if (Native.PtInRegion(DragableRegionNative, point.X, point.Y))
                            this.InvokeEx(() => Native.PostMessage(this.Handle, (uint)m.Msg, m.WParam, m.LParam));
    
                    }
                });
            }
        }
    

    As you can see, it is enough to intercept WM_LBUTTONDOWN event from chrome browser, then check if mouse point belongs to a title region and, if so, send this message to main form. As soon as form will get WM_LBUTTONDOWN event, build-in form methods OnMouseDown and OnMouseMove do the other work.

    0 讨论(0)
  • 2021-01-07 09:53

    Take a look at: https://github.com/qwqcode/CefSharpDraggableRegion

    GIF

    This you can specify -webkit-app-region: drag in CSS to tell CefSharp which regions are draggable (like the OS's standard titlebar).

    0 讨论(0)
提交回复
热议问题