Last active
August 29, 2015 14:01
-
-
Save bertvansteen/ec180b645fd6bcfd179e to your computer and use it in GitHub Desktop.
Prevent that AxAcroPDF steals focus when it loads a new file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Extensions for the Acrobat ActiveX control | |
/// /* Copyright (c) 2014 Bert Van Steen */ | |
/// <example> | |
/// axAcroPDF1.SuspendStealFocus(TimeSpan.FromMilliseconds(300)); | |
/// axAcroPDF1.LoadFile(scanFilename); | |
/// </example> | |
/// </summary> | |
static class AxAcroPDFFocusExtensions | |
{ | |
struct TimerTag | |
{ | |
public TimerTag(AxAcroPDF control, object controlTag) | |
{ | |
Control = control; | |
ControlTag = controlTag; | |
} | |
public object ControlTag; | |
public AxAcroPDF Control; | |
} | |
/// <summary> | |
/// Prevent Adobe AxAcroPDF from stealing focus when you change the src. | |
/// The control will be disabled for 250 ms | |
/// /// <param name="pdfControl">The acrobat ActiveX control</param> | |
/// </summary> | |
public static void SuspendStealFocus(this AxAcroPDF pdfControl) | |
{ | |
SuspendStealFocus(pdfControl, 250); | |
} | |
/// <summary> | |
/// Prevent Adobe AxAcroPDF from stealing focus when you change the src. | |
/// </summary> | |
/// <param name="pdfControl">The acrobat ActiveX control</param> | |
/// <param name="timeSpan">Time the ActiveX will be inaccessible (and not grabbing focus)</param> | |
public static void SuspendStealFocus(this AxAcroPDF pdfControl, TimeSpan timeSpan) | |
{ | |
SuspendStealFocus(pdfControl, (int)timeSpan.TotalMilliseconds); | |
} | |
/// <summary> | |
/// Prevent Adobe AxAcroPDF from stealing focus when you change the src. | |
/// </summary> | |
/// <param name="pdfControl">The acrobat ActiveX control</param> | |
//// <param name="timeoutInMilliSeconds">Number of milliseconds the ActiveX will be inaccessible (and not grabbing focus)</param> | |
public static void SuspendStealFocus(this AxAcroPDF pdfControl, int timeoutInMilliSeconds) | |
{ | |
pdfControl.Enabled = false; | |
Timer t = new Timer(); | |
t.Interval = timeoutInMilliSeconds; | |
t.Tick += t_Tick; | |
t.Start(); | |
pdfControl.Tag = Guid.NewGuid(); | |
t.Tag = new TimerTag(pdfControl, pdfControl.Tag); | |
} | |
static void t_Tick(object sender, EventArgs e) | |
{ | |
var timer = (Timer)sender; | |
timer.Stop(); | |
timer.Dispose(); | |
TimerTag t = (TimerTag) timer.Tag; | |
if (ReferenceEquals(t.Control.Tag, t.ControlTag)) | |
{ | |
t.Control.Enabled = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment