-
Sep 19th, 2008, 09:41 AM
#1
Thread Starter
PowerPoster
About PostMessage() and SendMessge() API functions
i try read some information about PostMessage() and SendMessage() API, but i still confuse.
can anyone explain to me these 2 functions?
and can anyone give 1 simple example with these 2 functions?
thanks
-
Sep 21st, 2008, 11:10 AM
#2
Re: About PostMessage() and SendMessge() API functions
Hai joaquim,
SendMessage Function
Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
msdn
I am not good at explain it correctly. but ill try to give my best. Also i am a big typo
all window management are done in winodws os using messages.
in winodws, a command button, a radio button, a check box, a scroll bar, a text box, a combo box, tool bar are all considered as window.
If you take notepad, note pad app along not a single window. insted, note pad itself have made of using many winodws. note pad menus are windows. notepad toolbar is a winodw. the ritch text box where we type in notpad is a winodw. futhrer if you take the calculator, each button in the calculator is a winodw. the text box that displays the numbers you type is a window.
windows start button is a winodws. the button that allow to show hide the system tray is a window. the open winodws toolbar is a window.
Window Handle
when this winodws are created, each winodow is assigned a unique id.
you can see that hwnd property in vb controls.
so until that winodw is destroyed, the window owns that unique id.
os uses this unique id (hwnd) to communicate with a particular window.
so now the os has a way to communicate and cordinate the operations of this windows. this is where the SendMessage or postMessage come to play.
Now this is how sendmessage / postmessage works.
if you click the start button, windows os sends the message WM_LBUTTONDOWN to the start button. so the click event fires.
so the code that use to do this as follows.
Code:
sendMessage (hwnd of startbutton,WM_LBUTTONDOWN,0,0)
* 0,0 is the parameters / arguments of the message WM_LBUTTONDOWN, But actually WM_LBUTTONDOWN here does not requir any parameter. Click here to see what WM_LBUTTONDOWN DOES.
* you can send a click using the above code though your app. but you need a way to obtain the start button handle. for this you use, findwindow() findwinodwex() etc.
Each control have its own type of messages as well in addition to the standed mesage like WM_LBUTTONDOWN. listview's have perticuler list view messages for it self. combo box have its perticuler messages.
here are the url to some of the winodw messages.
# Thease websites maintain a huge list of Window Messages Online:
# http://www.autohotkey.com/docs/misc/SendMessageList.htm
# http://wiki.winehq.org/List_Of_Windows_Messages
# http://vbnet.mvps.org/index.html?cod.../lvmembers.htm
Now, you have a way to monitor the messages send by os to each window using programs like spy++ or winspectorspy.
You just need to pick up a winodow and start montoring the message send that winodow by the os.
The main Diffrent between sendMessage and postmessage is, that postmessage does not return an the result of the message delivery. but SendMessage does.
Hope this will help you a little.
Last edited by Fazi; Oct 2nd, 2008 at 02:13 AM.
-
Sep 21st, 2008, 11:29 AM
#3
Thread Starter
PowerPoster
Re: About PostMessage() and SendMessge() API functions
hi Fazi....
yes, you help me so much....
thanks
-
Sep 27th, 2008, 11:44 AM
#4
Thread Starter
PowerPoster
Re: About PostMessage() and SendMessge() API functions
ok i'm trying do 1 thing:
in UC i have:
Code:
Event MouseScrolling(Scroll As MouseScroll)
Dim msScrolling As MouseScroll
Public Enum MouseScroll
NoScroll = -1
ScrollUp = 0
ScrollDown = 1
End Enum
in module i have:
Code:
SendMessage MouseOverControl, MouseScrolling, 1, 0
in api loop....
my question: what isn't right?
why VB6 is automatic closed?
(for now i'm trying use the sendmessage() api function)
thanks
-
Oct 5th, 2008, 01:31 PM
#5
Re: About PostMessage() and SendMessge() API functions
When using SendMessage/PostMessage, you must ensure the parameters you passed are what are expected and documented in MSDN. How did you declare SendMessage? ByRef lParam As Any, ByVal lParam As String, something else? What is the value of MouseOverControl & MouseScrolling?
You might also want to explain your loop. How/When does the loop start? How/When does the loop stop? Where is the loop, is it in your subclass procedure?
-
Oct 5th, 2008, 01:40 PM
#6
Thread Starter
PowerPoster
Re: About PostMessage() and SendMessge() API functions
Originally Posted by LaVolpe
When using SendMessage/PostMessage, you must ensure the parameters you passed are what are expected and documented in MSDN. How did you declare SendMessage? ByRef lParam As Any, ByVal lParam As String, something else? What is the value of MouseOverControl & MouseScrolling?
You might also want to explain your loop. How/When does the loop start? How/When does the loop stop? Where is the loop, is it in your subclass procedure?
"How did you declare SendMessage?"
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
the MouseOverControl variable is for see if the mouse is in my UC...
the MouseScrolling is for be the new event...
when the mouse is move in my UC and the scrolling wheel is moving, the MouseScrollin event must be activated.
these loop is in a module.
when the control is showed(the 1st time) these loop start, and when is destroyed these loop stop.
do you need my entire code?
thanks
Last edited by joaquim; Oct 5th, 2008 at 01:45 PM.
-
Oct 5th, 2008, 01:55 PM
#7
Re: About PostMessage() and SendMessge() API functions
No, I don't think we need to see the entire code, not yet.
MouseOverControl must be an hWnd. MouseScrolling must be a windows message, i.e., WM_VSCROLL or whatever.
Since you delcared SendMessage with lParam As Any, that means that lParam is ByRef because ByVal was not used. Depending on what MouseScrolling value is, I suspect that lParam is expected to be a long value, therefore, recommend calling this instead; including the & symbol which means a Long value.
SendMessage MouseOverControl, MouseScrolling, 1, ByVal 0&
-
Oct 5th, 2008, 01:59 PM
#8
Thread Starter
PowerPoster
Re: About PostMessage() and SendMessge() API functions
Originally Posted by LaVolpe
No, I don't think we need to see the entire code, not yet.
MouseOverControl must be an hWnd. MouseScrolling must be a windows message, i.e., WM_VSCROLL or whatever.
Since you delcared SendMessage with lParam As Any, that means that lParam is ByRef because ByVal was not used. Depending on what MouseScrolling value is, I suspect that lParam is expected to be a long value, therefore, recommend calling this instead; including the & symbol which means a Long value.
SendMessage MouseOverControl, MouseScrolling, 1, ByVal 0&
if must be a constant(MouseScrolling is my own event, not constant) how can i use SendMessage for my event, or i can't?
-
Oct 5th, 2008, 02:05 PM
#9
Re: About PostMessage() and SendMessge() API functions
If the SendMessage is to your own control, then MouseScrolling can be whatever constant/value you want it to be because your subclass procedure is testing the message value and processing/not processing based on that value. I do believe your crashes are related to declaring SendMessage's lParam as Any and not using the keyword ByVal when you are actually sending the message. If lParam is expected to be a Long in your subclass procedure then use ByVal 0&. If it is expected to be a String, then use ByVal strValue.
I wouldn't recommend this but you can declare SendMessage a couple different ways so it more precisely matches what you want to send. Then use the declaration version specific to your needs.
As Long: SendMessage_Long(....., ByVal lParam As Long)
As String: SendMessage_String(...., ByVal lParam As String)
As Any: SendMessage_Any(..., ByRef lParam As Any)
-
Oct 5th, 2008, 02:17 PM
#10
Thread Starter
PowerPoster
Re: About PostMessage() and SendMessge() API functions
i'm sorry, but i continue with problems.
heres the module:
Code:
Option Explicit
Private PrevWin2 As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_MOUSEWHEEL = &H20A
Public MouseOverControl As Long ' Handle for User Control (mouse move)
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Function Proc2(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' Pass mouse wheel as arrow keys to a window handle(user controls).
If Msg = WM_MOUSEWHEEL Then ' the mouse wheel rotated
If MouseOverControl > 0 Then
If MouseOverControl > 0 Then
If wParam / 65536 > 0 Then
'SendMessage MouseOverControl, WM_KEYDOWN, 2001, 0
'SendMessage MouseOverControl, WM_KEYUP, 2001, 0
SendMessage MouseOverControl, MouseScrolling, 0, 0
Else
'SendMessage MouseOverControl, WM_KEYDOWN, 2000, 0
'SendMessage MouseOverControl, WM_KEYUP, 2000, 0
SendMessage MouseOverControl, MouseScrolling, 1, 0
End If
End If
End If
End If
Proc2 = CallWindowProc(PrevWin2, hWnd, Msg, wParam, lParam)
End Function
Public Sub Hook2(handle As Long)
If PrevWin2 = 0 Then
PrevWin2 = SetWindowLong(handle, GWL_WNDPROC, AddressOf Proc2)
End If
End Sub
Public Sub Unhook2(handle As Long)
If PrevWin2 Then
Call SetWindowLong(handle, GWL_WNDPROC, PrevWin2)
PrevWin2 = 0
End If
End Sub
in UC:
Code:
Event MouseScrolling(Scroll As MouseScroll)
Dim msScrolling As MouseScroll
Public Enum MouseScroll
NoScroll = -1
ScrollUp = 0
ScrollDown = 1
End Enum
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If blnDestroyed = True Then Exit Sub
'when the mouse is moved then
MouseOverControl = UserControl.hWnd
MousePos.X = X
MousePos.Y = Y
mbMouseButton = Button
skShift = Shift
With UserControl
If GetCapture <> .hWnd Then SetCapture .hWnd
If ((X < 0 Or Y < 0 Or X > .ScaleWidth Or Y > .ScaleHeight) Or IIf(traTransparent = TransparentAutomatic Or traTransparent = TransparentManualy, GetPixel(UserControl.hdc, MousePos.X, MousePos.Y) = UserControl.BackColor, GetPixel(UserControl.hdc, MousePos.X, MousePos.Y) = -1)) Then ' we're off the form
'when the MouseExit is activated then
MouseOverControl = 0
ReleaseCapture
blnEnter = True
msScrolling = NoScroll
RaiseEvent MouseExit(mbMouseButton, skShift, MousePos.X, MousePos.Y)
DoEvents
Else
If blnEnter = True Then
blnEnter = False
lngOldMouseX = MousePos.X
lngOldMouseY = MousePos.Y
RaiseEvent MouseEnter(mbMouseButton, skShift, MousePos.X, MousePos.Y)
DoEvents
End If
End If
End With
RaiseEvent MouseMove(mbMouseButton, skShift, X, Y)
DoEvents
End Sub
Private Sub UserControl_Show()
Hook2 UserControl.ParentControls(0).hWnd
end sub
Private Sub UserControl_Terminate()
Unhook2 UserControl.ParentControls(0).hWnd
end sub
-
Oct 5th, 2008, 02:28 PM
#11
Re: About PostMessage() and SendMessge() API functions
Try this, notice the bold
SendMessage MouseOverControl, MouseScrolling, 0, ByVal 0&
SendMessage MouseOverControl, MouseScrolling, 1, ByVal 0&
Also, I would not test to see if MouseOverControl>0, rather that MouseOverControl<>0.
-
Oct 5th, 2008, 02:34 PM
#12
Thread Starter
PowerPoster
Re: About PostMessage() and SendMessge() API functions
Originally Posted by LaVolpe
Try this, notice the bold
SendMessage MouseOverControl, MouseScrolling, 0, ByVal 0&
SendMessage MouseOverControl, MouseScrolling, 1, ByVal 0&
Also, I would not test to see if MouseOverControl>0, rather that MouseOverControl<>0.
the MouseOverContro is working perfectly(tested in other situation)....
i try do it... but i continue with same error... when i execute, the program and VB6 close... i don't know why
thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|