AxAcroPDF swallowing keys, how to get it to stop?

前端 未结 4 424
自闭症患者
自闭症患者 2021-01-14 11:46

The AxAcroPDF swallows all key-related events as soon as it gets focus, including shortcuts, key presses, etc. I added a message filter, and it doesn\'t get any key-related

4条回答
  •  难免孤独
    2021-01-14 11:52

    Hans is correct, the Acrobat Reader spawns two child AcroRd32 processes which you have no direct access to from within your managed code.

    I have experimented with this and you have three viable options:

    1. You can create a global system hook, and then look for and filter out / respond to WM_SETFOCUS messages sent to your child AcroRd32 windows. You can accomplish some of this from within C# by using a wrapper library, such as the one here: http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx

      You also need to identify the correct processes as there may be more than one instance of your application, or other instances of AcroRd32. This is the most deterministic solution, but because your application will now be filtering messages sent to every single window in existence, I generally don't recommend this approach because then your program could negatively affect system stability.

    2. Find an alternate PDF viewing control. See this answer for a few commercial components: .net PDF Viewer control , or roll your own: http://www.codeproject.com/KB/applications/PDFViewerControl.aspx

    3. Find an acceptable hack. Depending on how robust your application needs to be, code such as the following may be suitable (it was suitable for my case):

      DateTime _lastRenav = DateTime.MinValue;
      
      public Form1()
      {
          InitializeComponent();
      
          listBox1.LostFocus += new EventHandler(listBox1_LostFocus);
      }
      
      private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
      {
          axAcroPDF1.src = "sample.pdf";  //this will cause adobe to take away the focus
          _lastRenav = DateTime.Now;
      }
      
      void listBox1_LostFocus(object sender, EventArgs e)
      {
          //restores focus if it were the result of a listbox navigation
          if ((DateTime.Now - _lastRenav).TotalSeconds < 1)
              listBox1.Focus();
      }
      

提交回复
热议问题