Custom FileOpen Dialog

FileOpen dialog with a Messagebox hook


Imports System.Runtime.InteropServices

Public Class NativeFileDialog

    <DllImport("comdlg32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Private Shared Function GetOpenFileName(ByRef ofn As OPENFILENAME) As Boolean
    End Function

    <DllImport("comdlg32.dll")>
    Private Shared Function CommDlgExtendedError() As UInteger
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function GetDlgItem(hDlg As IntPtr, nIDDlgItem As Integer) As IntPtr
    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function SetWindowText(hWnd As IntPtr, lpString As String) As Boolean
    End Function

    Public Delegate Function FileOpenDlgProc(hdlg As IntPtr, msg As UInteger, wParam As IntPtr, lParam As IntPtr) As Integer

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)>
    Public Structure OPENFILENAME
        Public lStructSize As Integer
        Public hwndOwner As IntPtr
        Public hInstance As IntPtr
        Public lpstrFilter As String
        Public lpstrCustomFilter As String
        Public nMaxCustFilter As Integer
        Public nFilterIndex As Integer
        Public lpstrFile As String
        Public nMaxFile As Integer
        Public lpstrFileTitle As String
        Public nMaxFileTitle As Integer
        Public lpstrInitialDir As String
        Public lpstrTitle As String
        Public Flags As Integer
        Public nFileOffset As Short
        Public nFileExtension As Short
        Public lpstrDefExt As String
        Public lCustData As IntPtr
        Public lpfnHook As FileOpenDlgProc
        Public lpTemplateName As String
        Public pvReserved As IntPtr
        Public dwReserved As Integer
        Public FlagsEx As Integer
    End Structure

    Private Const OFN_EXPLORER As Integer = &H80000
    Private Const OFN_FILEMUSTEXIST As Integer = &H1000
    Private Const WM_INITDIALOG As UInteger = &H110
    Private Const WM_NOTIFY As UInteger = &H4E
    Private Const CDN_INITDONE As Integer = -601
    Private Const IDOK As Integer = 1

    Public Shared Sub ShowOpenDialog()
        Dim ofn As New OPENFILENAME()
        Dim fileBuffer As New String(Chr(0), 260)
        Dim fileTitleBuffer As New String(Chr(0), 260)

        ' Set required fields
        ofn.lpstrFilter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        ofn.lpstrFile = fileBuffer
        ofn.nMaxFile = 260
        ofn.lpstrFileTitle = fileTitleBuffer
        ofn.nMaxFileTitle = 260
        ofn.lpstrTitle = "Open a File"
        ofn.lpstrInitialDir = "C:\"
        ofn.Flags = OFN_EXPLORER Or OFN_FILEMUSTEXIST
        ofn.lpfnHook = AddressOf MyHook
        ofn.Flags = OFN_EXPLORER Or OFN_ENABLEHOOK Or OFN_FILEMUSTEXIST

        ' ✅ Manually set correct struct size
        If IntPtr.Size = 8 Then
            ofn.lStructSize = 152 ' 64-bit
        Else
            ofn.lStructSize = 76  ' 32-bit
        End If

        If ofn.lpstrFilter.IndexOf(Chr(0)) > -1 Then
            ofn.lpstrFilter = ofn.lpstrFilter.Substring(0, ofn.lpstrFilter.IndexOf(Chr(0)))
        Else
            ofn.lpstrFilter = ofn.lpstrFilter
        End If

        If GetOpenFileName(ofn) Then
            MessageBox.Show("Selected file: " & ofn.lpstrFile.ToString())
        Else
            Dim err = CommDlgExtendedError()
            MessageBox.Show(If(err = 0, "User canceled dialog.", "Failed. Error: " & err))
        End If
    End Sub

    Private Const OFN_ENABLEHOOK As Integer = &H20

    Private Shared Function MyHook(hdlg As IntPtr, msg As UInteger, wParam As IntPtr, lParam As IntPtr) As Integer
        Select Case msg
            Case WM_INITDIALOG
                ' Dialog is being initialized
                MsgBox("WM_INITDIALOG received")
        End Select
        Return 0
    End Function
End Class

Download 'Custom Fileopen Dialog.vb':

📥 Download custom-fileopen-dialog.vb