フォーム上のPDFがアクティブにならない
投稿者 KOZ  (社会人)
投稿日時
2023/11/24 06:14:23
こんな感じです。
Option Strict On
Option Explicit On
Imports System.Text
Imports System.Runtime.InteropServices
Friend Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AcroPDF1.LoadFile("z:\test.pdf")
End Sub
Private Const ZOOM_IN As Keys = Keys.Control Or Keys.Add
Private Const ZOOM_OUT As Keys = Keys.Control Or Keys.Subtract
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData
Case ZOOM_IN, ZOOM_OUT
If msg.HWnd <> AcroPDF1.Handle Then
PostMessage(GetAVPageView(AcroPDF1), msg.Msg, msg.WParam, msg.LParam)
Return True
End If
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Function GetAVPageView(view As AxAcroPDFLib.AxAcroPDF) As IntPtr
Dim LParam As IntPtr = CType(1, IntPtr)
EnumChildWindows(view.Handle, AddressOf EnumChildProc, LParam)
Return LParam
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function PostMessage(hWnd As IntPtr, Msg As Integer,
wParam As IntPtr, lParam As IntPtr) As Boolean
End Function
Private Function EnumChildProc(ByVal hwnd As IntPtr, ByRef LParam As IntPtr) As Boolean
If GetWindowText(hwnd) = "AVPageView" Then
LParam = hwnd
Return False
End If
Return True
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function EnumChildWindows(hWndParent As IntPtr, lpEnumFunc As EnumChildProcDelegate, ByRef lParam As IntPtr) As Boolean
End Function
Private Delegate Function EnumChildProcDelegate(hwnd As IntPtr, ByRef Lparam As IntPtr) As Boolean
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function GetWindowTextLength(hWnd As IntPtr) As Integer
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)>
Private Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
End Function
Private Function GetWindowText(hwnd As IntPtr) As String
Dim sb As New StringBuilder(256)
GetWindowText(hwnd, sb, 255)
Return sb.ToString()
End Function
End Class
やりたいことはキーボードでPDFのショートカットキーが入力されたら
PDFが拡大、縮小する等を行いたいのですが問題があります。
キーボードで拡大、縮小のショートカットキーを入れても
PDFにフォーカスがあたってないと反応しません。
キーを押したらフォーカスがあたってなくてもPDFを操作できるようにしたいです。