右键菜单和鼠标拖动冲突,有鼠标拖动就会使右键菜单失灵

zhanglong1997 2019-04-17 02:10:57
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;
}
}

}
}
...全文
170 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
stherix 2019-04-17
  • 打赏
  • 举报
回复
或者处理WM_NCLBUTTONUP消息 在这里面手动弹出右键菜单
stherix 2019-04-17
  • 打赏
  • 举报
回复
消息处理的时候,判断如果是右键点击,则直接base.WndProc(ref m);

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧