右键菜单和鼠标拖动冲突,有鼠标拖动就会使右键菜单失灵
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FloatForm
{
public partial class frmTopMost : Form
{
private ContextMenuStrip docMenu;
public frmTopMost()
{
InitializeComponent();
}
private void frmTopMost_Load(object sender, EventArgs e)
{
this.Width =100;
this.Height =100;
this.Left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - this.Width -18;
this.Top = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height - this.Height -42;
this.ShowInTaskbar = false;
//右键菜单
docMenu = new ContextMenuStrip();
ToolStripMenuItem SesLable = new ToolStripMenuItem();
SesLable.Text = "百度";
SesLable.Click += new EventHandler(SesLable_Click);
ToolStripMenuItem EpLable = new ToolStripMenuItem();
EpLable.Text = "Hao123";
EpLable.Click += new EventHandler(EpLable_Click);
docMenu.Items.AddRange(new ToolStripMenuItem[] { SesLable, EpLable });
this.ContextMenuStrip = docMenu;
}
private void SesLable_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("https://www.baidu.com");
}
private void EpLable_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process.Start("https://www.hao123.com");
}
private void frmTopMost_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
//鼠标拖动窗口
const int WM_NCHITTEST = 0x0084;
const int HTCLIENT = 0x0001;
const int HTCAPTION = 0x0002;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result == (IntPtr)HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if (m.Result == (IntPtr)HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
break;
default:
base.WndProc(ref m);
break;
}
}
}
}