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:
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.
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).