-
Hello,
I'm developing an OCX (a simple Command Button) and
I'm in trouble to draw the focus on it
(EnterFocus/GotFocus properties).
This is my code:
Private Sub UserControl_EnterFocus()
UserControl.ScaleMode = 3
lpRect.Top = 3
lpRect.Left = 3
lpRect.Right = (UserControl.Width \ Screen.TwipsPerPixelX) - 3
lpRect.Bottom = (UserControl.Height \ Screen.TwipsPerPixelY) - 3
DrawFocusRect UserControl.hdc, lpRect
UserControl.ScaleMode = 1
End Sub
Private Sub UserControl_ExitFocus()
UserControl.ScaleMode = 3
lpRect.Top = 3
lpRect.Left = 3
lpRect.Right = (UserControl.Width \ Screen.TwipsPerPixelX) - 3
lpRect.Bottom = (UserControl.Height \ Screen.TwipsPerPixelY) - 3
DrawFocusRect UserControl.hdc, lpRect
UserControl.ScaleMode = 1
End Sub
Does anybody can show me the correct way to draw
the Focus?
Thanks a lot...
Michel Jr.
-
Hi Michel,
I sent you the actual project that shows the solution to your problem but I figured I'd post it in case anyone else is curious.
Code:
Option Explicit
Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lprect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Dim b_HasFocus As Boolean ' flag for paint event
Private Sub UserControl_EnterFocus()
b_HasFocus = True ' flag paint event to display focus rect
Call UserControl_Paint ' repaint object
End Sub
Private Sub ShowFocusRect()
Dim rctFocus As RECT
Dim ret As Long
'get dimensions of usercontrol
With rctFocus
.Top = 3
.Left = 3
.Right = (UserControl.Width \ Screen.TwipsPerPixelX) - 3
.Bottom = (UserControl.Height \ Screen.TwipsPerPixelY) - 3
End With
ret = DrawFocusRect(UserControl.hdc, rctFocus) ' display focus rect
End Sub
Private Sub UserControl_ExitFocus()
b_HasFocus = False 'flag paint even NOT to draw focus rect
Call UserControl.Cls ' clear graphical contents of UserControl to remove focus rect
Call UserControl_Paint ' repaint object
End Sub
Private Sub UserControl_Initialize()
b_HasFocus = False ' initialize focus flag to false (if control gets focus first b_hasfocus will be set to True by EnterFocus event)
End Sub
Private Sub UserControl_Paint()
' Anything & Everything that you draw on a usercontrol at runtime has to be in the paint event _
or it will not show up on the screen
If b_HasFocus = True Then ' if the control has focus
Call ShowFocusRect ' paint it!
End If
End Sub
:) :D :) :D