使用 vba 在辅助显示器上打开 pdf 文件

问题描述 投票:0回答:1

我正在尝试制作一个程序,在双显示器设置的辅助显示器上打开 pdf 文件。我有一个打开文件的工作程序,但它总是在主显示器上打开。我希望 pdf 在辅助显示器上自动打开。这可能吗?

感谢您的所有帮助。

vba pdf multiple-monitors
1个回答
0
投票

我问了chatgpt并修改了他的答案,它起作用了

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hWnd As LongPtr, ByVal hWndInsertAfter As LongPtr, ByVal X As LongPtr, ByVal Y As LongPtr, ByVal cx As LongPtr, ByVal cy As LongPtr, ByVal wFlags As LongPtr) As LongPtr
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hWnd As LongPtr, ByVal nCmdShow As LongPtr) As LongPtr

Const SWP_NOZORDER As Long = &H4

Sub openPDFsecondscreen()
    Dim pathPDF As String
    Dim hWnd As LongPtr
    Dim posX As Long
    Dim posY As Long
    Dim largeur As Long
    Dim hauteur As Long

    ' define the path of the file
    pathPDF = "C:\Users\olivi\Downloads\2024_10_BP_octobre.pdf"

    ' open the pdf with default app(Adobe Reader ou autre)
    Shell "explorer.exe " & pathPDF , vbNormalFocus

    ' wait a little
    'Wait

    ' get the handle of the Adobe windows 
    hWnd = FindWindow("AcrobatSDIWindow", vbNullString)
    If hWnd = 0 Then
        MsgBox "Impossible de trouver la fenêtre PDF."
        Exit Sub
    End If

    ' define the coordinates for the 2d screen
    
    posX = -1300  ' x Position of the 2d screen
    posY = -1000        ' y Position of the 2d screen
    largeur = 1200  ' width of window
    hauteur = 2000   ' height of window

    ' move the window to the 2d screen
    SetWindowPos hWnd, 0, posX, posY, largeur, hauteur, SWP_NOZORDER
    hWnd = FindWindow("AcrobatSDIWindow", vbNullString)
    ShowWindow hWnd, 2
    ShowWindow hWnd, 3
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.