Cara Agar Form Tetap Diatas (Always On Top)–VB6

Thursday, October 9, 2014

Ketika kita klik sebuah form dan kita ingin form yang lainnya tetap di atas tanpa tertindih oleh yang tadi kita klik adalah menggunakan cara Always On Top, bagaimana caranya? Berikut langkah-langkahnya:

- Buka VB6 dan buatlah sebuah form

- Masukan 2 buah Command button ubah caption masing-masing dengan On Top dan No. Seperti gambar dibawah ini:

image

- Kemudian buat sebuah Module dan masukan sintak dibawah ini:

Option Explicit
      Public Const SWP_NOMOVE = 2
      Public Const SWP_NOSIZE = 1
      Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
      Public Const HWND_TOPMOST = -1
      Public Const HWND_NOTOPMOST = -2

      Declare Function SetWindowPos Lib "user32" _
            (ByVal hwnd As Long, _
            ByVal hWndInsertAfter As Long, _
            ByVal x As Long, _
            ByVal y As Long, _
            ByVal cx As Long, _
            ByVal cy As Long, _
            ByVal wFlags As Long) As Long

      Public Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) _
         As Long

         If Topmost = True Then 'Make the window topmost
            SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, _
               0, FLAGS)
         Else
            SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, _
               0, 0, FLAGS)
            SetTopMostWindow = False
         End If
      End Function

- Setelah itu masukan sintak berikut pada form:

Option Explicit
Private Sub Command1_Click() 
    Dim lR As Long
    lR = SetTopMostWindow(Form1.hwnd, True)
   
End Sub

Private Sub Command2_Click()
    Dim lR As Long
    lR = SetTopMostWindow(Form1.hwnd, False)
   
End Sub

- Setelah itu jalankan, lalu klik On Top maka ketika kita klik jendela lain maka form akan tetap di atas, dan klik no untuk membuat form kembali seperti semula.

Sekian tutorialnya semoga bermanfaat.

0 comments:

Post a Comment