在printPreviewControl中显示要打印的文档是不支持鼠标滚轮响应的。解决方法:
一、在主窗体中加入printPreviewControl控件后需要的字段
[需要引用using System.Reflection命名空间]
#region 滚动需要的字段
private Type _t;
private FieldInfo _Position;
private MethodInfo _SetPositionMethod;
#endregion
二、 主窗体中的初始化
public PrintDialogMy()
{
#region 滚动私有方法提取,从printpreviewControl中提取
_t = typeof(System.Windows.Forms.PrintPreviewControl);
_Position = _t.GetField("position", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
_SetPositionMethod = _t.GetMethod("SetPositionNoInvalidate", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.ExactBinding);
#endregion
}
三、需要为printPreviewControl添加一个mousewheel事件事件中的处理如下
void printPreviewControl1_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)printPreviewControl1.Zoom;//缩放量
if (e.Delta < 0)
{
scrollAmount = (int)amount;
}
else
{
scrollAmount = -(int)amount;
}
Point curPos = (Point)(_Position.GetValue(printPreviewControl1));
_SetPositionMethod.Invoke(printPreviewControl1, new object[] { new
Point (curPos.X + 0, curPos.Y + scrollAmount) });
}