winform打印控件鼠标滚轴问题

2019-05-15  本文已影响0人  Q轩哥

我们发现本身c#自带打印预览窗口可以预览,但是鼠标滚轴滑动无法跟随,固实现自定义打印预览控件来实现,废话不多说,上代码。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace login.UI.common
{
public partial class CustomPrintPreviewDialog: PrintPreviewDialog
{
private bool isMouseDown = false;
private Type _t;
private FieldInfo m_Position;
private MethodInfo m_SetPositionMethod;
private Point startPosition;
private Point endPosition;
private Point curPos;
public CustomPrintPreviewDialog()
{
_t = typeof(System.Windows.Forms.PrintPreviewControl);
m_Position = _t.GetField("position", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
m_SetPositionMethod = _t.GetMethod("SetPositionNoInvalidate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
this.PrintPreviewControl.MouseWheel += new MouseEventHandler(preview_MouseWheel);
this.PrintPreviewControl.Click += new EventHandler(preview_Click);
this.PrintPreviewControl.MouseDown += new MouseEventHandler(preview_MouseDown);
this.PrintPreviewControl.MouseUp += new MouseEventHandler(preview_MouseUp);
this.PrintPreviewControl.MouseMove += new MouseEventHandler(preview_MouseMove);
this.PrintPreviewControl.MouseWheel += new MouseEventHandler(preview_MouseWheel);
}
public void preview_MouseWheel(object sender, MouseEventArgs e)
{
if (!SystemInformation.MouseWheelPresent)
{
return;
}
int scrollAmount;
float amount = Math.Abs(e.Delta) / SystemInformation.MouseWheelScrollDelta;
amount *= SystemInformation.MouseWheelScrollLines;
amount *= 12;
amount *= (float)this.PrintPreviewControl.Zoom;
if (e.Delta < 0)
{
scrollAmount = (int)amount;
}
else
{
scrollAmount = -(int)amount;
}
Point curPos = (Point)(m_Position.GetValue(this.PrintPreviewControl));
m_SetPositionMethod.Invoke(this.PrintPreviewControl, new object[] { new Point(curPos.X + 0, curPos.Y + scrollAmount) });
}
/// <summary>
/// 鼠标在控件上点击时,需要处理获得焦点,因为默认不会获得焦点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void preview_Click(object sender, EventArgs e)
{
this.PrintPreviewControl.Select();
this.PrintPreviewControl.Focus();
}
/// <summary>
/// 鼠标按下,开始拖动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void preview_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
startPosition = new Point(e.X, e.Y);
curPos = (Point)(m_Position.GetValue(this.PrintPreviewControl));
}
/// <summary>
/// 鼠标释放,完成拖动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void preview_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
endPosition = new Point(e.X, e.Y);
m_SetPositionMethod.Invoke(this.PrintPreviewControl, new object[] { new Point(curPos.X + (startPosition.X - endPosition.X), curPos.Y + (startPosition.Y - endPosition.Y)) });
}
/// <summary>
/// 鼠标移动,拖动中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void preview_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown == true)
{
endPosition = new Point(e.X, e.Y);
m_SetPositionMethod.Invoke(this.PrintPreviewControl, new object[] { new Point(curPos.X + (startPosition.X - endPosition.X), curPos.Y + (startPosition.Y - endPosition.Y)) });
}
}
}
}

上一篇下一篇

猜你喜欢

热点阅读