2014-03-26 56 views
1

我有一個winform,我將PDF加載到AxAcroPDF。AxAcroPdf當加載時,做點什麼

看起來像這樣

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

現在,當我運行它,我可以看到,它專注於其他的文本框,但它失去了當PDF加載的焦點(和變焦的豆蔻工具欄PDF和所有漸漸消失的)。這就像它剛剛開始加載,繼續下一行,並且當它實際加載它需要重點。我怎麼能告訴它等待完整的負載,然後專注於其他文本框?

回答

0

我創建的擴展方法,以防止AxAcroPDF竊取代碼,應當這樣使用:

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

原始C#源文件可以發現here。我用.net反射將其轉換爲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 
0

放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