How do I send messages using vb. I tried sendmessage API function, but it didnot work
Printable View
How do I send messages using vb. I tried sendmessage API function, but it didnot work
'This project needs a ListBox, named List1 and a TextBox, named Text1
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long
Const LB_FINDSTRING = &H18F
Private Sub Form_Load()
'Add some items to the listbox
With List1
.AddItem "Computer"
.AddItem "Screen"
.AddItem "Modem"
.AddItem "Printer"
.AddItem "Scanner"
.AddItem "Sound Blaster"
.AddItem "Keyboard"
.AddItem "CD-Rom"
.AddItem "Mouse"
End With
End Sub
Private Sub Text1_Change()
'Retrieve the item's listindex
List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text))
End Sub
What message were you trying to send? Maybe you were using incorrect syntax.
Const EM_SETREADONLY=&HCF 'Edit Box Read Only Prop
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 sub Form_Load()
End Sub
Private sub Text1_Change()
End Sub
private sub Command1_Click()
dim lResult As Long
'Make Text1 Read Only
lResult=SendMessage((Text1.hWnd), _ EM_SETREADONLY,True,0&)
' To Allow Changes Repl. True With False Above.
End Sub
textboxes already have that property. if textbox is text1, then text1.locked = true will lock it.
as for your code, try replacing "true" with a number other than zero.
True is a constant for 1.
i didn't think it was a long. How about TRUE&
I want to send messages to move the mouse cursor and make it click on some other program file
Why don't you use SetCursorPos for moving the cursor. You can then use the SendMessgae to emulate mouse Button down.
Since you didn't declare any variable as a boolean, the API function will interpret it as '1' (long data type)Quote:
Originally posted by Lord Orwell
i didn't think it was a long. How about TRUE&
Thats the problem, I can move the cursor using SetCursorPos. But the more important thing in clicking the mouse button is what I don't know :(
send a mouse event, using the api call Mouse_event
Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As _
Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
notice the buttons parameter.
It is just there to throw you off. It is actually unused.
to simulate button presses, use the following values in place of the dwFlags (any or all of them)
MOUSEEVENTF_ABSOLUTE: dx and dy specify an absolute position in mouse coordinates which divide the screen into 65,535 units horizontally and vertically.
Public Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
Public Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up
To click the mouse:
Code:Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const MK_LBUTTON = &H1
Private Sub Command1_Click()
PostMessage hwnd_of_window, WM_LBUTTONDOWN, MK_LBUTTON, 0
PostMessage hwnd_of_window, WM_LBUTTONUP, MK_LBUTTON, 0
End Sub
Very good. If he knows the handle of the window. I had (foolishly perhaps :)) assumed he wanted to click on something without a handle, or was making a macro program, since he was talking about moving the mouse.
Thankyou all who have helped me above