使用鼠标滚轮滚动窗口

问题描述 投票:0回答:1

我正在使用 pdf Sharp 版本(1.52)在 C# 中创建报告。

我想要鼠标滚轮滚动报告。

我有一个表单,我正在调用另一个表单并在其中显示图形

pdfsharps 页面预览。我想使用鼠标滚轮实现滚动。

但只有事件触发而不是实际滚动

我正在使用 pdf Sharp 版本(1.52)在 C# 中创建报告。

我想要鼠标滚轮滚动报告。

我有一个表单,我正在调用另一个表单并在其中显示图形

pdfsharps 页面预览。我想使用鼠标滚轮实现滚动。

但只有事件触发而不是实际滚动

我的代码附在下面

ReportOptionsForm.cs

private ReportGenerator reportGenerator;
PreviewReportForm previewForm = new PreviewReportForm(reportGenerator);

ReportGenerator 是我的课程之一。

PreviewReportForm.cs

public PreviewReportForm(ReportGenerator rg)
{
    InitializeComponent();

    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(957, 739);

    this.Text = Language.Strings.PreviewReport;
    this.ShowInTaskbar = true;
    this.ControlBox = true;    // right corner control symbols, ???
    this.FormBorderStyle = FormBorderStyle.FixedDialog;
    this.BackColor = System.Drawing.Color.FromArgb(60, 60, 60);
    this.StartPosition = FormStartPosition.CenterParent;

    reportGenerator = rg;


    
    InitializeControls();
    
    this.ResumeLayout(false);

}


private void InitializeControls()
{
    numberOfPages = reportGenerator.TotalPages;

    pageSplitBarHeight = 6;

    reportPagePreview = new PagePreview();
    reportPagePreview.TabIndex = 4;
    reportPagePreview.Parent = this;
    reportPagePreview.PageSize = new XSize(612, 792 * numberOfPages + pageSplitBarHeight * (numberOfPages - 1));
    reportPagePreview.ZoomPercent = 180;
    reportPagePreview.Size = new Size(957, 673);
    reportPagePreview.Location = new Point(0, actionToolStrip.Bottom);
    reportPagePreview.PageColor = System.Drawing.Color.White;
    reportPagePreview.Zoom = Zoom.BestFit;
    reportPagePreview.ZoomPercent = 100;

    reportPagePreview.MouseWheel += ReportPagePreview_MouseWheel;
 
    reportRenderer = new ReportRenderer(reportGenerator, reportPagePreview.DesktopColor, pageSplitBarHeight);
    reportPagePreview.SetRenderFunction(reportRenderer.Render);


    UpdateStatusStrip();
}

private void ReportPagePreview_MouseWheel(object sender, MouseEventArgs e)
{
    if (e.Delta != 0)
    {

        int delta = e.Delta > 0 ? -120 : 120; 


        int currentScrollPosition = reportPagePreview.VerticalScroll.Value;


        int newScrollPosition = Math.Max(reportPagePreview.VerticalScroll.Minimum,
                                          Math.Min(reportPagePreview.VerticalScroll.Maximum,
                                          currentScrollPosition + delta));

        // Set the new scroll position
        reportPagePreview.VerticalScroll.Value = newScrollPosition;
    }
    reportPagePreview.Invalidate();
}
c# windows-forms-designer mousewheel pdfsharp
1个回答
0
投票

因此,您应该调整文档的显示部分以响应滚轮事件,而不是操作 VerticalScroll 属性。

你可以试试这个:

private void ReportPagePreview_MouseWheel(object sender, MouseEventArgs e)
{
    if (e.Delta != 0)
    {
        int delta = e.Delta > 0 ? -120 : 120;

        int newVerticalOffset = reportPagePreview.VerticalOffset + delta;

        // Ensure that the new offset doesn't exceed the bounds
        newVerticalOffset = Math.Max(0, Math.Min(newVerticalOffset, reportPagePreview.DocumentHeight - reportPagePreview.ClientSize.Height));

        // Update the vertical offset
        reportPagePreview.VerticalOffset = newVerticalOffset;
    }
}

让我知道这是否有效:D

© www.soinside.com 2019 - 2024. All rights reserved.