Home > Visual Basic > Custom MsgBox Function

Custom MsgBox Function

Fungsi MsgBox digunakan untuk menampilkan pesan/informasi kepada user dan fungsi MsgBox sendiri mempunyai beberapa style yang mana style ini berpengaruh terhadap pilihan tombol yang ditampilkan.

Kita ambil contoh beberapa style yang sering digunakan, lihat gambar :

Kemudian kita akan mencoba mengganti pesan YesNo -> YaTidak dan OkCancel -> OkBatal :

Penasaran dengan kodenya seperti apa, langsung saja tambahkan sebuah modul kemudian copy paste kode berikut :

Option Explicit

Public Const IDOK = 1
Public Const IDCANCEL = 2
Public Const IDYES = 6
Public Const IDNO = 7

Private Const IDPROMPT = &HFFFF&

Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5

Private Type MSGBOX_HOOK_PARAMS
    hwndOwner   As Long
    hHook       As Long
End Type

Private MSGHOOK As MSGBOX_HOOK_PARAMS

Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long

Private Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If uMsg = HCBT_ACTIVATE Then
        SetWindowText wParam, "Konfirmasi"

        SetDlgItemText wParam, IDYES, "Ya"
        SetDlgItemText wParam, IDNO, "Tidak"

        SetDlgItemText wParam, IDOK, "Oke"
        SetDlgItemText wParam, IDCANCEL, "Batal"

        SetDlgItemText wParam, IDPROMPT, "Apakah Anda ingin keluar ?"

        UnhookWindowsHookEx MSGHOOK.hHook
    End If

    MsgBoxHookProc = False
End Function

Public Function MessageBoxH(ByVal hwndThreadOwner As Long, ByVal hwndOwner As Long, ByVal button As VbMsgBoxStyle) As Long
   Dim hInstance As Long
   Dim hThreadId As Long

   hInstance = GetWindowLong(hwndThreadOwner, GWL_HINSTANCE)
   hThreadId = GetCurrentThreadId()

   With MSGHOOK
      .hwndOwner = hwndOwner
      .hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
   End With

   MessageBoxH = MessageBox(hwndOwner, Space$(40), Space$(40), vbQuestion + button)
End Function

Untuk contoh penggunaan tambahkan objek CommandButton, kemudian ketik kode berikut :

Private Sub cmdDemoCustomeMsgBox_Click()
    Select Case MessageBoxH(Me.hwnd, Me.hwnd, vbOKCancel)
        Case IDYES: Debug.Print "Anda mengklik tombol Ya"
        Case IDNO: Debug.Print "Anda mengklik tombol Tidak"
        Case IDOK: Debug.Print "Anda mengklik tombol Oke"
        Case IDCANCEL: Debug.Print "Anda mengklik tombol Batal"
    End Select
End Sub

Referensi : http://vbnet.mvps.org

Selamat mencoba :)

About these ads
  1. May 2, 2010 at 2:32 am | #1

    makasih mas!!!

  2. May 2, 2010 at 2:45 am | #2

    Oke sama2x

  3. mahendra
    March 13, 2011 at 6:55 am | #3

    MAS KENAPA PAS AKU COPY PASTE KE VB.MODULE KOK ADA TULISAN BERWARNA MERAH TERUS JADI ERROR

    • March 13, 2011 at 8:36 am | #4

      Emang om cara ngopy sourcenya gimana? Main blok text trus klik kanan copy ?

      Sebaik tinggal klik icon copy yg ada dipojok kanan atas source codenya :

      Lihat gambar

  4. October 15, 2012 at 7:27 pm | #5

    Produk Baru kami Activex EcoCollection.dll
    jika ingin mencoba silakan download GRAtis
    disini
    https://ekojimmyy.wordpress.com/2012/09/20/activex-ecocollection-untuk-vb6/

    semoga bermanfaat

  5. Ahmad
    November 23, 2012 at 3:02 pm | #6

    maaf pak….mau tanya. apa bisa ya saat cursor mouse digerakkan ke atas tombol command button, mouse iconnya bisa berubah jadi gambar tangan ? kalau bisa apakah lewat coding yang diposting pak admin di atas bisa membuat mouse iconnya berubah ? Makasih atas pencerahannya pak.. :-)

    • November 23, 2012 at 3:30 pm | #7

      Tinggal atur aja kok om nilai properties (via desain) dari objek tombolnya.

      Command1.MousePointer = 99 - Custome
      Command1.MouseIcon = lokasi icon
      
      • Ahmad
        November 23, 2012 at 3:39 pm | #8

        maaf pak…maksud saya command button di messagebox
        hehe….ralat :-)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: