CEF sharp mouse click dont work with borderless draggable UI

Having problems with building or using the CefSharp .NET binding? Ask your CEF-related questions here. Please ask general usage questions on StackOverflow.

Moderator: amaitland

CEF sharp mouse click dont work with borderless draggable UI

Postby abhikris27 » Thu Jun 10, 2021 10:12 am

My requirement is to Show a Win Form without a border. I want to Allow Dragging of the control without border. So i implemented ChromeWidgetMessageInterceptor from this https://csharp.hotexamples.com/examples ... mples.html. But after implementing this it can be dragged. The browser has X button .When I click it doesn't work .When I click on webpage elements it should respond automatically. None of the mouse click events work inside browser. Also if I put the border on the Form .The mouse click inside the browser works perfectly fine. There is no issue with JS or HTML files. I tried simple google.com but doesn't work, if I click on search button. Below is the code.

public class Form1{
private ChromiumWebBrowser mBrowserView;
private int mMinWidth = 250;
private int mMinHeight = 350;
private bool formClosed;
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
private bool isDragging;
ChromeWidgetMessageInterceptor chromeWidgetMessageInterceptor;
private IntPtr browserHandle;
private ILog mLog;

/// <summary>
/// Sends the message.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="Msg">The MSG.</param>
/// <param name="wParam">The w parameter.</param>
/// <param name="lParam">The l parameter.</param>
/// <returns></returns>
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
/// <summary>
/// Releases the capture.
/// </summary>
/// <returns></returns>
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

/// <summary>
/// Posts the message.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="Msg">The MSG.</param>
/// <param name="wParam">The w parameter.</param>
/// <param name="lParam">The l parameter.</param>
/// <returns></returns>
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

/// <summary>
/// Initializes a new instance of the <see cref="PopUpControl"/> class.
/// </summary>
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}


/// <summary>
/// Displays the pop up control.
/// </summary>
/// <param name="url">The URL.</param>
public void DisplayBrowser()
{

if (mBrowserView == null)
{
mBrowserView = new ChromiumWebBrowser(url);
mBrowserView.Dock = DockStyle.Fill;
this.Controls.Add(mBrowserView);
mBrowserView.HandleCreated += MBrowserView_HandleCreated;
mBrowserView.IsBrowserInitializedChanged += MBrowserView_IsBrowserInitializedChanged; ;
mBrowserView.MouseDown += MBrowserView_MouseDown; ;
mBrowserView.MouseUp += MBrowserView_MouseUp;
mBrowserView.MouseMove += MBrowserView_MouseMove;
}
else
{
mBrowserView.Load(url);
}
}

/// <summary>
/// Handles the MouseMove event of the MBrowserView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void MBrowserView_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
this.Location = Cursor.Position;
}

/// <summary>
/// Handles the MouseUp event of the MBrowserView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
private void MBrowserView_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;

}

/// <summary>
/// Handles the MouseDown event of the MBrowserView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="MouseEventArgs"/> instance containing the event data.</param>
private void MBrowserView_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
}

/// <summary>
/// Handles the HandleCreated event of the MBrowserView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void MBrowserView_HandleCreated(object sender, EventArgs e)
{
browserHandle = ((ChromiumWebBrowser)mBrowserView).Handle;
}

/// <summary>
/// Handles the IsBrowserInitializedChanged event of the MBrowserView control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void MBrowserView_IsBrowserInitializedChanged(object sender, EventArgs e)
{
if (mBrowserView.IsBrowserInitialized)
{
SetupMessageInterceptor();
mBrowserView.ShowDevTools();
}
}



/// <summary>
/// Setups the message interceptor.
/// </summary>
private void SetupMessageInterceptor()
{
if (chromeWidgetMessageInterceptor != null)
{
chromeWidgetMessageInterceptor.ReleaseHandle();
chromeWidgetMessageInterceptor = null;
}

Task.Run(async () =>
{
try
{
while (true)
{
IntPtr chromeWidgetHostHandle;
if (ChromeWidgetHandleFinder.TryFindHandle(browserHandle, out chromeWidgetHostHandle))
{
chromeWidgetMessageInterceptor = new ChromeWidgetMessageInterceptor((Control)mBrowserView, chromeWidgetHostHandle, message =>
{
const int WM_MOUSEACTIVATE = 0x0021;
const int WM_NCLBUTTONDOWN = 0x00A1;
const int WM_DESTROY = 0x0002;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
// Render process switch happened, need to find the new handle
if (message.Msg == WM_DESTROY)
{
SetupMessageInterceptor();
return;
}

if (message.Msg == WM_MOUSEACTIVATE)
{
// The default processing of WM_MOUSEACTIVATE results in MA_NOACTIVATE,
// and the subsequent mouse click is eaten by Chrome.
// This means any .NET ToolStrip or ContextMenuStrip does not get closed.
// By posting a WM_NCLBUTTONDOWN message to a harmless co-ordinate of the
// top-level window, we rely on the ToolStripManager's message handling
// to close any open dropdowns:
// http://referencesource.microsoft.com/#S ... er.cs,1249
var topLevelWindowHandle = message.WParam;
PostMessage(topLevelWindowHandle, WM_NCLBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
}
//Forward mouse button down message to browser control
else
{
PostMessage(browserHandle, (uint)message.Msg, message.WParam, message.LParam);
mLog.Error("Message when clicked WM_LBUTTONDOWN");
}


// The ChromiumWebBrowserControl does not fire MouseEnter/Move/Leave events, because Chromium handles these.
// However we can hook into Chromium's messaging window to receive the events.
//
const int WM_MOUSEMOVE = 0x0200;
const int WM_MOUSELEAVE = 0x02A3;

switch (message.Msg)
{
case WM_MOUSEMOVE:
mLog.Error("Message when clicked Mouse move WM_MOUSEMOVE");
break;
case WM_MOUSELEAVE:
mLog.Error("Message when clicked Mouse WM_MOUSELEAVE");
break;
default:
mLog.Error("Message when clicked not falling in any case"+message.Msg.ToString());
break;

}
});

break;
}
else
{
// Chrome hasn't yet set up its message-loop window.
await Task.Delay(10);
}
}
}
catch
{
// Errors are likely to occur if browser is disposed, and no good way to check from another thread
}
});
}
}
abhikris27
Newbie
 
Posts: 1
Joined: Thu Jun 10, 2021 10:08 am

Re: CEF sharp mouse click dont work with borderless draggabl

Postby amaitland » Thu Jun 10, 2021 2:14 pm

Already asked at https://stackoverflow.com/questions/679 ... de-browser

Please just ask your question once and wait patiently for a reply.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1290
Joined: Wed Jan 14, 2015 2:35 am


Return to CefSharp Forum

Who is online

Users browsing this forum: No registered users and 1 guest