如何解决CEFsharp 鼠标单击在浏览器中不起作用
我的要求是显示一个没有边框的 Win Form。我想允许无边框拖动控件。所以我从这个 https://csharp.hotexamples.com/examples/CefSharp.WinForms.Example/ChromeWidgetMessageInterceptor/-/php-chromewidgetmessageinterceptor-class-examples.html 实现了 ChromeWidgetMessageInterceptor。但是在实现这个之后它可以被拖动。浏览器有 X 按钮。当我点击它时它不起作用。当我点击网页元素时,它应该自动响应。没有任何鼠标点击事件在浏览器中起作用。此外,如果我将边框放在 Form 上。在浏览器内单击鼠标可以正常工作。 JS 或 HTML 文件没有问题。 我尝试了简单的 google.com 但不起作用,如果我点击搜索按钮。 下面是代码。
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/#System.Windows.Forms/winforms/Managed/System/WinForms/ToolStripManager.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
}
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。