AxAcroPdf加载后,做点什么

时间:2014-03-26 12:46:49

标签: vb.net winforms pdf wait axacropdf

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

看起来像这样

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

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

2 个答案:

答案 0 :(得分:1)

将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

答案 1 :(得分:0)

我创建了一个扩展方法来阻止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