C#自带报表展示控件的右键菜单的设置,隐藏右键的所有菜单
作者:互联网
原文链接:https://www.cnblogs.com/unforget/archive/2010/04/22/1718215.html
转载自:https://www.cnblogs.com/unforget/archive/2010/04/22/1718215.html
微软的ReportViewer功能很强大。特别是Visual Studio 2010 带的ReportViewer 10.0导出Excel功能堪称完美,以前版本的不能导出子报表的问题也解决了。
ReportViewer提供了一系列的属性用于控制右键菜单,例如showPrintButton设置为false,右键菜单里面的Print项就自动被隐藏了。
不过,不知道是我找的原因还是什么,常规的方法都用遍了都没法隐藏右键菜单的Page Setup项。下午用IL看了下ReportViewer,终于找到了一种方法能修改右键菜单的那个异常顽固的项了。
共享给大家,提供的这种方式,经修改,可以完全自定义右键菜单。
/// <summary>
/// 禁用掉右键菜单的 print setup 选项
/// </summary>
/// <param name="rv">ReportViewer</param>
private void DisableContextMenuStrip(ref ReportViewer rv)
{
BindingFlags bf = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
//get winRSviewer
FieldInfo fiwinRSviewer = rv.GetType().GetField("winRSviewer", bf);
object objwinRSviewer = fiwinRSviewer.GetValue(rv);
//get m_reportPanel
FieldInfo fireportPanel = objwinRSviewer.GetType().GetField("m_reportPanel", bf);
object objreportPanel = fireportPanel.GetValue(objwinRSviewer);
//get m_contextMenu
FieldInfo ficontextMenuStrip = objreportPanel.GetType().GetField("m_contextMenu", bf);
ContextMenuStrip cms = ficontextMenuStrip.GetValue(objreportPanel) as ContextMenuStrip;
//
if (cms == null)
{
return;
}
//replace "Page Setup"
int nIndex = -1;
for (int i = 0; i < cms.Items.Count; i++)
{
nIndex ++;
if (cms.Items[i].Text == "P&age Setup")
{
break;
}
}
//create new item
ToolStripMenuItem tsmiNewPS = new ToolStripMenuItem("P&age Setup");
tsmiNewPS.Click += new EventHandler(tsmiNewPS_Click);
//add item
cms.Items.RemoveAt(nIndex);
cms.Items.Add(tsmiNewPS);
}
/// <summary>
/// 自定义item响应
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void tsmiNewPS_Click(object sender, EventArgs e)
{
MessageBox.Show("自定义的item响应!!");
}
标签:控件,菜单,ReportViewer,tsmiNewPS,item,右键,cms 来源: https://blog.csdn.net/m0_37528968/article/details/101455779