Wednesday, May 19, 2010

Tips dan Trik Visual Basic

Membuat Form ke Tengah

Jika anda membuat anak form (form child) pada MDI form, maka defaultnya form tersebut tidak akan ketengah. Untuk membuat form tersebut ketengah anda menggunakan kode dibawah ini:

Public Sub CenterForm(frm As Form)
frm.Top = (Screen.Height - frm.Height) / 2
frm.Left = (Screen.Width - frm.Width) / 2
End Sub

Pindah Antar Kontrol Dengan Menggunakan Enter

Defaultnya untuk berpindah antar kontrol kita menggunakan tombol tab. Namun kita bisa merubah tombol tersebut dengan menggunakan fungsi SendKeys. Cara kerjanya adalah dengan mendeteksi agar ketika tombol enter ditekan kita mengirim tombol tab kepada program.

Anda dapat mendeteksinya dengan menaruh potongan program tersebut pada KeyPress event procedure dengan mengecheck apakah nilai dari KeyAscii parameter = 13.

Code

Sub Text1_KeyPress (KeyAscii As Integer)
If KeyAscii = 13 Then
SendKeys "{tab}"
KeyAscii = 0
End If
End Sub

Membuat Disable Tombol X Pada ControlBox

Pada module ketikan kode berikut ini:

Declare Function GetSystemMenu Lib "User32" _
(ByVal hWnd As Integer, ByVal bRevert _
As Integer) As Integer

Declare Function RemoveMenu Lib "User32" _
(ByVal hMenu As Integer, ByVal nPosition _
As Integer, ByVal wFlags As Integer) As Integer

Global Const MF_BYPOSITION = &H400

Lalu pada load event form1 ketikan:

Private Sub Form_Load()
SystemMenu% = GetSystemMenu(hWnd, 0)
Res% = RemoveMenu(SystemMenu%, 6, MF_BYPOSITION)
End Sub

Membuat Form Transparent

Module Code

Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_TRANSPARENT = &H20&
Public Const SWP_FRAMECHANGED = &H20
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWME = SWP_FRAMECHANGED Or _
SWP_NOMOVE Or SWP_NOSIZE
Public Const HWND_NOTOPMOST = -2

Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long

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

Private Sub Command1_Click()
SetWindowLong Me.hwnd, GWL_EXSTYLE, _
WS_EX_TRANSPARENT
SetWindowPos Me.hwnd, HWND_NOTOPMOST, _
0&, 0&, 0&, 0&, SWP_SHOWME
End Sub

Pada ShowInTaskbar property ubah menjadi False dan BorderStyle menjadi 0-None.

Membatasi Gerakan Kursor Dalam Form

Private Type RECT
left As Integer
top As Integer
right As Integer
bottom As Integer
End Type

Private Type POINT
x As Long
y As Long
End Type

Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
Private Declare Sub GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT)
Private Declare Sub ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINT)
Private Declare Sub OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long)

Private Sub Form_Load()
Command1.Caption = "Limit Cursor Movement"
Command2.Caption = "Release Limit"
End Sub

Private Sub Command1_Click()
'membatasi pergerakan kursor sesuai batas Form
Dim client As RECT
Dim upperleft As POINT

GetClientRect Me.hWnd, client
upperleft.x = client.left
upperleft.y = client.top
ClientToScreen Me.hWnd, upperleft
OffsetRect client, upperleft.x, upperleft.y
ClipCursor client
End Sub

Private Sub Command2_Click()
'
mengembalikan pembatas kursor
ClipCursor ByVal 0&
End Sub

Private Sub Form_Unload(Cancel As Integer)
'mengembalikan pembatas kursor
ClipCursor ByVal 0&
End Sub


No comments:

Post a Comment

selamat datang

Page Rank Button