Save dan Open File tanpa Common Dialog Control VB6

Pada umumnya setiap kali kita akan membuka dan menampilkan file kita dihadapkan dengan jendela dialog control khususnya pada Visual Basic 6.0 komponen ocx yang digunakan bernama Microsoft Windows Common Dialog Control. Sekarang kita akan mencoba menampilkan dialog control yaitu Save dan Open file tanpa menambahkan komponen ocx tersebut.

Save dan Open File tanpa Common Dialog Control VB6

Buatlah sebuah project pada Microsoft Visual Basic 6.0 selanjutnya buat form dan tambahkan 1 buah module kemudian tambahkan 2 CommandButton pada form. Copykan Syntak dibawah ini ke module
Option Explicit

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Public Const OFN_READONLY = &H1
Public Const OFN_OVERWRITEPROMPT = &H2
Public Const OFN_HIDEREADONLY = &H4
Public Const OFN_NOCHANGEDIR = &H8
Public Const OFN_SHOWHELP = &H10
Public Const OFN_ENABLEHOOK = &H20
Public Const OFN_ENABLETEMPLATE = &H40
Public Const OFN_ENABLETEMPLATEHANDLE = &H80
Public Const OFN_NOVALIDATE = &H100
Public Const OFN_ALLOWMULTISELECT = &H200
Public Const OFN_EXTENSIONDIFFERENT = &H400
Public Const OFN_PATHMUSTEXIST = &H800
Public Const OFN_FILEMUSTEXIST = &H1000
Public Const OFN_CREATEPROMPT = &H2000
Public Const OFN_SHAREAWARE = &H4000
Public Const OFN_NOREADONLYRETURN = &H8000
Public Const OFN_NOTESTFILECREATE = &H10000
Public Const OFN_NONETWORKBUTTON = &H20000
Public Const OFN_NOLONGNAMES = &H40000
Public Const OFN_EXPLORER = &H80000
Public Const OFN_NODEREFERENCELINKS = &H100000
Public Const OFN_LONGNAMES = &H200000

Public Const OFN_SHAREFALLTHROUGH = 2
Public Const OFN_SHARENOWARN = 1
Public Const OFN_SHAREWARN = 0

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long

Function SaveDialog(Form1 As Form, Filter As String, Title As String, InitDir As String) As String
 Dim ofn As OPENFILENAME
    Dim A As Long
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = Form1.hWnd
    ofn.hInstance = App.hInstance
    If Right$(Filter, 1) <> "|" Then Filter = Filter + "|"
    For A = 1 To Len(Filter)
        If Mid$(Filter, A, 1) = "|" Then Mid$(Filter, A, 1) = Chr$(0)
    Next
    ofn.lpstrFilter = Filter
        ofn.lpstrFile = Space$(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space$(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = InitDir
        ofn.lpstrTitle = Title
        ofn.flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_CREATEPROMPT
        A = GetSaveFileName(ofn)

        If (A) Then
            SaveDialog = Trim$(ofn.lpstrFile)
        Else
            SaveDialog = ""
        End If
End Function

Function OpenDialog(Form1 As Form, Filter As String, Title As String, InitDir As String) As String
Dim ofn As OPENFILENAME
    Dim A As Long
    ofn.lStructSize = Len(ofn)
    ofn.hwndOwner = Form1.hWnd
    ofn.hInstance = App.hInstance
    If Right$(Filter, 1) <> "|" Then Filter = Filter + "|"
    For A = 1 To Len(Filter)
        If Mid$(Filter, A, 1) = "|" Then Mid$(Filter, A, 1) = Chr$(0)
    Next
    ofn.lpstrFilter = Filter
        ofn.lpstrFile = Space$(254)
        ofn.nMaxFile = 255
        ofn.lpstrFileTitle = Space$(254)
        ofn.nMaxFileTitle = 255
        ofn.lpstrInitialDir = InitDir
        ofn.lpstrTitle = Title
        ofn.flags = OFN_HIDEREADONLY Or OFN_FILEMUSTEXIST
        A = GetOpenFileName(ofn)

        If (A) Then
            OpenDialog = Trim$(ofn.lpstrFile)
        Else
            OpenDialog = ""
        End If
End Function
Untuk selanjutnya copykan juga syntak dibawah ini dengan memperhatikan nama komponen yang terdapat pada form.
Public A As String
Private Sub cmdCommand1_Click()
A = OpenDialog(Me, "*.jpeg|*.jpeg*.jpg|*.jpg|*.bmp|*.bmp|*.ico|*.ico", "Load Gambar", App.Path)
If A <> vbNullString Then
   MsgBox "Anda memilih file : " & A, 64, "informasi"
End If
End Sub

Private Sub cmdCommand2_Click()
A = SaveDialog(Me, "*.txt|*.txt", "Simpan File", App.Path)
If A <> vbNullString Then
   MsgBox "Anda menyimpan file di : " & A, 64, "informasi"
End If
End Sub
Jika telah selesai simpan project dan kemudian RUN/F5. Klik Commandbutton dengan Caption Open maka akan tampil dialog Open File. Begitu juga dengan CommandButton Caption Save akan keluar dialog Save file. Untuk lebih detailnya sourcode diatas bisa didownload di LINK INI

Semoga bermanfaat

Related

Visual Basic Classic 8842976371482131813

Post a Comment

Perhatian !!
- Berkomentarlah dengan menggunakan bahasa yang baik dan sopan dan sesuai topik pembahasan
- Dilarang menjadikan referensi artikel web ini tanpa menyertakan sumbernya

emo-but-icon

Terbaru

Random Artikel

Memuat...

Contact Us

Name

Email *

Message *

item