Friday, July 19, 2013

VB6 Code: Membuat DWord dari HiWord + LoWord

Posting ini diambil dari Microsoft KB mengenai cara membuat fungsi return DWord dengan menggabungkan HiWord dan LoWord. Adapun fungsi yang dimaksud adalah sebagai berikut:
Function MakeDWord(LoWord As Integer, HiWord As Integer) As Long 
    MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF&) 
End Function 
Sedangkan untuk memecah DWord (32 bits) menjadi LoWord (16 bits) dan HiWord (16 bits) adalah sebagai berikut:
Function LoWord(DWord As Long) As Integer 
    If DWord And &H8000& Then ' &H8000& = &H00008000 
        LoWord = DWord Or &HFFFF0000 
    Else 
        LoWord = DWord And &HFFFF& 
    End If 
End Function 
 
Function HiWord(DWord As Long) As Integer 
    HiWord = (DWord And &HFFFF0000) \ &H10000 
End Function 
Sedangkan contoh dari fungsi MakeDword (menggabungkan LoWord dan HiWord) adalah sebagai berikut:
Option Explicit 
 
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 
 
Private Const MK_LBUTTON = &H1 
Private Const WM_LBUTTONDOWN = &H201 
 
Function MakeDWord(LoWord As Integer, HiWord As Integer) As Long 
    MakeDWord = (HiWord * &H10000) Or (LoWord And &HFFFF&) 
End Function 
 
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 
    Form1.Cls 
    Form1.Print "Button Click Event Fired" 
    Form1.Print "Position X:" & Str$(X / Screen.TwipsPerPixelX) 
    Form1.Print "Position Y:" & Str$(Y / Screen.TwipsPerPixelY) 
End Sub 
 
Private Sub Command1_Click() 
    Dim nMousePosition As Long 
    ' nMousePosition stores the x (hiword) and y (loword) values 
    ' of the mouse cursor as measured in pixels. 
 
    Let nMousePosition = MakeDWord(16, 18) 
    Call SendMessage(Me.hwnd, WM_LBUTTONDOWN, MK_LBUTTON, nMousePosition) 
End Sub 
 
Semoga bermanfaat.