Wednesday, April 14, 2010

Advance Form Center - Bagian Dua

Advance form center - Ini merupakan fungsi untuk menyimpan form di tengah layar, adapun ditambah kata advance, karena ia memiliki beberapa keunggulan, yaitu:menjalankan form dan menempatkannya di tengah layar, ini hanya dilakukan sekali pada saat ia ditampilkan pertama kali, setelah itu form akan mengikuti nilai yang ada pada registry.
'simpan kode di bawah pada module 
Option Explicit

Private Declare Function
GetSystemMetrics Lib "user32" ByVal nIndex As Long) As Long
Private Const
SM_CXFULLSCREEN = 16
Private Const SM_CYFULLSCREEN = 17

Private Const
strKey As String = "HKEY_CURRENT_USER\Software\"

Public Function
SavePositionsInRegistry(frm As Form)

If
frm.WindowState = vbMaximized Or frm.WindowState = vbMinimized Then Exit Function

Dim
KeyReg As String, k As String

KeyReg = strKey & App.Title & "\" & frm.Name & "\"
RegWrite KeyReg & "FormLeft", frm.Left
RegWrite KeyReg & "FormTop", frm.Top
RegWrite KeyReg & "FormWidth", frm.Width
RegWrite KeyReg & "FormHeight", frm.Height

End Function

Public Function
GetPositionsFromRegistry(frm As Form)

If
frm.WindowState = vbMaximized Or frm.WindowState = vbMinimized Then Exit Function

Dim
KeyReg As String
Dim
ileft, itop, iwidth, iheight
Dim lCenterLeft As Long, lCenterTop As Long

GetFormCenter frm, lCenterLeft, lCenterTop
KeyReg = strKey & App.Title & "\" & frm.Name & "\"

ileft = IIf(IsEmpty(RegRead(KeyReg & "FormLeft")), lCenterLeft, RegRead(KeyReg & "FormLeft"))
itop = IIf(IsEmpty(RegRead(KeyReg & "FormTop")), lCenterTop, RegRead(KeyReg & "FormTop"))
iwidth = IIf(IsEmpty(RegRead(KeyReg & "FormWidth")), frm.Width, RegRead(KeyReg & "FormWidth"))
iheight = IIf(IsEmpty(RegRead(KeyReg & "FormHeight")), frm.Height, RegRead(KeyReg & "FormHeight"))

frm.Move ileft, itop, iwidth, iheight

End Function

Private Function
GetFormCenter(frm As Form, lLeft As Long, lTop As Long)
With frm
lLeft = Screen.TwipsPerPixelX * GetSystemMetrics(SM_CXFULLSCREEN) / 2)) - .Width / 2)
lTop = Screen.TwipsPerPixelY * GetSystemMetrics(SM_CYFULLSCREEN) / 2)) - .Height / 2)
End With
End Function