I have a usercontrol which acts as a button. When hovered on it a frame (which is the buton so to say) will show a border line. I would like to remove that border line once the mouse has moved past the user control!!?
OK. Here is the code you need. The following code should be placed in the code window of the user control, not in the form that contains it. (as you wished).
___
[tt]
Option Explicit
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetCapture Lib "user32" () As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim R As RECT, P As POINTAPI
GetWindowRect UserControl.hwnd, R
GetCursorPos P
If PtInRect(R, P.x, P.y) Then
If GetCapture <> UserControl.hwnd Then
SetCapture UserControl.hwnd
'Mouse enters the user control area
'Turn the user control to red color
UserControl.BackColor = vbRed
End If
Else
ReleaseCapture
'Mouse leaves the user control area
'Turn the user control to green color
UserControl.BackColor = vbGreen
End If
End Sub[/tt]
___
Here the back color of the user control is toggled to indicate when the mouse enters or leaves the user control. You should insert the appropriate code to hide or show the border line instead of changing the back color.
Well, it works very smooth for me. I checked it on two XP boxes.
Make sure that you have placed right code in the right place (for enter and leave). Also check the above code "as-is" without modification and see if the user control changes its color.
It works great the problem is that now i'm lossing control ont eh mouse_down and mouse_up event!! is there a way i can get these to work together with your flawless code!!
Once again, I can't understand your problem clearly. The code does not create any problem with any mouse event including the MouseDown and MouseUp events. I receive these events properly in the user control class.
Add the following lines to the user control code window along with the above code and see the response in the debug window when you click on the user control.
___
[tt]
Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "MouseDown", Button, Shift, X, Y
End Sub
Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Debug.Print "MouseUp", Button, Shift, X, Y
End Sub[/tt]
___
If you want to raise these events in the external interface of the user control available to the container of the control (i.e. the form) then you have to declare these events and raise them from your user control's code, when you receive these events in your the user control class (like the normal practice.)
The problem im having is relativley easy...maybe i cannot explain very good (most propbably!!)
After i started using your code (the focus and lose focus one) i couldn't get the mouse_down and mouse_up events to work!!
I have a picture box on the user control which takes the whole size of the user control and when the user presses on the picture box it raises an event ...which after using the code its not!
This site does not encourage the use of e-mails between the participants, (faq222-2244, para 12) but, anyhow you may send me your code at hypetia@yahoo.com because this problem does not seem to solve this way.
Hope we'll post the solution back here, if found.
Make sure you send the complete code, the user control project files and form which hosts this user control as well, don't miss anything.
I think i fixed it by making a timer control checking for the bouderies! so i'll test it and will forward u the code if the need be! Sorry i didn't know about the email i wouldn't have asked u about it if i knew..on the other hand if u don't wish that i send u email just say so!!
Its not a matter of liking or disliking, but there are some agreed-upon rules which are developed by the mutual understanding of the members of this forum over the passage of time.
These dos and don'ts are summarized in FAQ222-2244, which also addresses the e-mail communication.
However, although e-mailing is avoided, it is not totally banned (I think so). Otherwise I wouldn't have provided my e-mail above.
You may e-mail me at the above address (most welcome) but first, try your best to solve the problem yourself. As you said, you have some success using the timer control. So that we do not violate the rules of this forum.
Notice that you cant trigger usercontrols' mousemove, mousedown or mouseup events after covering it with the picture box. Hypetias' code is a simple demonstration of what has to be done. You have to improve it if just in case you need more functionality for it.
My suggestion will be to subclass the window and handle mouseout messages by code.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.