Draggable borderless window in CefSharp

删除回忆录丶 提交于 2019-12-01 01:45:59
qwqaq

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!