2

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

看起来像这样

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

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

4

2 回答 2

1

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

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

可以在此处找到原始 C# 源文件。我使用 .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
于 2014-05-28T10:56:52.747 回答
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
于 2016-11-08T12:57:19.867 回答