这个程序调用Notepad2,用来快速切换查看代码。
程序首先用shell启动一个进程,模式为vbHide,然后findWindow找到窗口句柄,然后setParent把它放到PictureBox里。另外还用到了注册表存储路径。
问题是当上次Notepad2关闭时是最大化的,那么新启动的Notepad2会瞬间最大化覆盖屏幕(不管shell的模式),然后才被放到PictureBox里。
如果上次Notepad关闭时不是最大化的,那么接下来所有新Notepad2进程的显示都比较完美(没有闪烁)。
请问这个问题应该怎么解决?
Option Explicit
' Second Version
' Open folder and save the folder in reg table
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPrivate Const SW_MAXIMIZE = 3
Private Const WM_QUIT = &H12Private WindowHwnd As Long
Private CurrentFile As String
Private CurrentFolder As String' the test function
Private Function FindDir(strPath As String) As Boolean
    If strPath = "" Then
        FindDir = False
    ElseIf Dir(strPath, vbDirectory + vbHidden + vbNormal) <> "" Then
        FindDir = True
    Else
        FindDir = False
    End If
End FunctionPrivate Sub Command1_Click()
    On Error Resume Next
    CurrentFolder = InputBox("输入目录", "CodeViewer")
    If Err.Number = 0 Then
        If FindDir(CurrentFolder) = True Then
            File1.Path = CurrentFolder
            Call SaveSetting("CodeViewer", "Path", "Path1", CurrentFolder)
        Else
            MsgBox "无效的目录!", vbCritical, "输入错误"
        End If
    End If
    
End SubPrivate Sub File1_Click()
    'Close existing ones
    If CurrentFile <> "" Then
        Call PostMessage(WindowHwnd, WM_QUIT, 0, 0&)
    End If
    
    'Open new ones
    CurrentFile = File1.Path & "\" & File1.FileName    Call Shell("notepad.exe " & CurrentFile, vbHide)
    WindowHwnd = FindWindow("notepad2", CurrentFile & " - Notepad2")
    Call SetParent(WindowHwnd, Picture1.hwnd)
    Call ShowWindow(WindowHwnd, SW_MAXIMIZE)
End SubPrivate Sub Form_Load()
    CurrentFile = ""
    
    CurrentFolder = GetSetting("CodeViewer", "Path", "Path1", "")
    If FindDir(CurrentFolder) Then
        File1.Path = CurrentFolder
    Else
        File1.Path = "."
    End If
   
End SubPrivate Sub Form_Resize()
    File1.Left = 10
    File1.Top = 10
    File1.Width = 100
    File1.Height = Form1.ScaleHeight - 60
    Picture1.Left = 120
    Picture1.Top = 10
    Picture1.Height = Form1.ScaleHeight - 60
    Picture1.Width = Form1.ScaleWidth - 130
    Command1.Top = Form1.ScaleHeight - 40
    Command1.Left = Form1.ScaleWidth - 120
    Command1.Width = 110
    Command1.Height = 30
End Sub