【问题标题】:AxAcroPdf when loaded, do somethingAxAcroPdf 加载时,做一些事情
【发布时间】:2016-11-08 21:19:31
【问题描述】:

我有一个 winform,我将 PDF 加载到 AxAcroPDF。

看起来像这样

Public sub LoadSelectedPDF()
    PDF_Reader.Loadfile(TXT_BrowsePDF.Text) 'PDF_Reader is my AxAcroPDF
    TXT_Title.Focus()
End Sub

现在,当我运行它时,我可以看到它聚焦在另一个文本框上,但是在加载 PDF 时它失去了焦点(以及用于缩放 PDF 的小工具栏和所有淡入淡出的内容)。就像它刚刚开始加载,继续到下一行,当它实际加载时它会获得焦点。如何告诉它等待完成加载,然后专注于另一个文本框?

【问题讨论】:

    标签: vb.net winforms pdf wait axacropdf


    【解决方案1】:

    我创建了一个扩展方法来防止 AxAcroPDF 窃取代码,它应该这样使用:

    PDF_Reader.SuspendStealFocus()
    PDF_Reader.Loadfile(TXT_BrowsePDF.Text)
    

    原始C#源文件可以在here找到。我使用.NET Reflector将其转换为VB.NET(仅在Winforms中测试,它将数据存储在PDF_Reader.Tag中):

    <Extension> _
    Friend Class AxAcroPDFFocusExtensions
    
    
       <Extension> _
       Public Shared Sub SuspendStealFocus(ByVal pdfControl As AxAcroPDF)
          pdfControl.SuspendStealFocus(250)
       End Sub
    
       <Extension> _
       Public Shared Sub SuspendStealFocus(ByVal pdfControl As AxAcroPDF, ByVal timeoutInMilliSeconds As Integer)
          pdfControl.Enabled = False;
    
          Dim t As New Timer
          t.Interval = timeoutInMilliSeconds
          AddHandler t.Tick, New EventHandler(AddressOf AxAcroPDFFocusExtensions.t_Tick)
          t.Start
          pdfControl.Tag = Guid.NewGuid
          t.Tag = New TimerTag(pdfControl, pdfControl.Tag)
       End Sub
    
       <Extension> _
       Public Shared Sub SuspendStealFocus(ByVal pdfControl As AxAcroPDF, ByVal timeSpan As TimeSpan)
          pdfControl.SuspendStealFocus(CInt(timeSpan.TotalMilliseconds))
       End Sub
    
       Private Shared Sub t_Tick(ByVal sender As Object, ByVal e As EventArgs)
          Dim timer As Timer = DirectCast(sender, Timer)
          timer.Stop
          timer.Dispose
          Dim t As TimerTag = DirectCast(timer.Tag, TimerTag)
          If Object.ReferenceEquals(t.Control.Tag, t.ControlTag) Then
                t.Control.Enabled = True
          End If
       End Sub
    
    
    
       <StructLayout(LayoutKind.Sequential)> _
       Private Structure TimerTag
          Public ControlTag As Object
          Public Control As AxAcroPDF
          Public Sub New(ByVal control As AxAcroPDF, ByVal controlTag As Object)
                Me.Control = control
                Me.ControlTag = controlTag
          End Sub
       End Structure
    End Class
    

    【讨论】:

      【解决方案2】:

      将 AxAcroPDF 放入面板中,然后:

      Public sub LoadSelectedPDF()
          PDF_Reader.Loadfile(TXT_BrowsePDF.Text) 'PDF_Reader is my AxAcroPDF
          panel_pdf.Enabled = False
          TXT_Title.Focus()    
      End Sub
      

      在 TXT_Title 输入事件中:

      System.Threading.Thread.Sleep(500)
      panel_pdf.Enabled = True
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 1970-01-01
        • 1970-01-01
        • 2020-10-06
        相关资源
        最近更新 更多