Results 1 to 40 of 40

Thread: Focus Rect

  1. #1

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Question Focus Rect

    Hello again everyone ...

    Like before , i have another question to ask , this time about command button.
    My question is , how to make the focus rect invisible when a command button got focus? thanks.

    Regards,
    Ferry

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Focus Rect

    just use an image. it has a click event that you can use like a button

  3. #3

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Focus Rect

    thanks dglienna,

    but what i need is make the focus rect on command button not visible.
    but thanks anyway.
    Can anyone else help me?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Focus Rect

    draw a rectangle around it that is a different color?

    or display a picture of a button?

  5. #5
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Focus Rect

    I know I found this somewhere here, don't remember who posted it though :

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function DrawEdge Lib "user32" (ByVal hDC As Long, qrc As RECT, ByVal Edge As Long, ByVal grfFlags As Long) As Long
    4.  
    5. Private Type RECT
    6.     Left As Long
    7.     Top As Long
    8.     Right As Long
    9.     Bottom As Long
    10. End Type
    11.  
    12. Private Const BDR_RAISEDOUTER = &H1
    13. Private Const BDR_SUNKENOUTER = &H2
    14. Private Const BDR_RAISEDINNER = &H4
    15. Private Const BDR_SUNKENINNER = &H8
    16. Private Const EDGE_RAISED = (BDR_RAISEDOUTER Or BDR_RAISEDINNER)
    17. Private Const EDGE_SUNKEN = (BDR_SUNKENOUTER Or BDR_SUNKENINNER)
    18. Private Const EDGE_ETCHED = (BDR_SUNKENOUTER Or BDR_RAISEDINNER)
    19. Private Const EDGE_BUMP = (BDR_RAISEDOUTER Or BDR_SUNKENINNER)
    20.  
    21. Private Const BF_LEFT = &H1
    22. Private Const BF_TOP = &H2
    23. Private Const BF_RIGHT = &H4
    24. Private Const BF_BOTTOM = &H8
    25. Private Const BF_RECT = (BF_LEFT Or BF_TOP Or BF_RIGHT Or BF_BOTTOM)
    26.  
    27. Dim Button1Border As Long
    28. Private Sub Form_Load()
    29.     Button1Border = EDGE_RAISED
    30. End Sub
    31. Private Sub Picture1_DblClick()
    32.     Button1Border = EDGE_SUNKEN
    33.     Picture1.Refresh
    34. End Sub
    35. Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    36.     Button1Border = EDGE_SUNKEN
    37.     Picture1.Refresh
    38. End Sub
    39. Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    40.     Button1Border = EDGE_RAISED
    41.     Picture1.Refresh
    42. End Sub
    43. Private Sub Picture1_Paint()
    44.     Dim r As RECT
    45.     Picture1.Cls
    46.     r.Bottom = Picture1.ScaleHeight
    47.     r.Right = Picture1.ScaleWidth
    48.     DrawEdge Picture1.hDC, r, Button1Border, BF_RECT
    49. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Focus Rect

    Another way that has been posted here, again I don't remember who

    VB Code:
    1. Private Type RECT
    2.     Left As Long
    3.     Top As Long
    4.     Right As Long
    5.     Bottom As Long
    6. End Type
    7.  
    8. Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    9. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    10. Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    11.  
    12. Private Sub Command1_GotFocus()
    13.     Dim udtRect As RECT
    14.     Dim lngHDC As Long
    15.     Dim lngStatus As Long
    16.     udtRect.Left = 4
    17.     udtRect.Top = 4
    18.     udtRect.Bottom = ScaleY(Command1.Height, Me.ScaleMode, vbPixels) - udtRect.Top
    19.     udtRect.Right = ScaleX(Command1.Width, Me.ScaleMode, vbPixels) - udtRect.Left
    20.     DoEvents
    21.     lngHDC = GetDC(Command1.hwnd)
    22.     lngStatus = DrawFocusRect(lngHDC, udtRect)
    23.     ReleaseDC Command1.hwnd, lngHDC
    24. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  7. #7

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Focus Rect

    it doesn't work . but i think that code used for draw a rectangle not to make it dissappeared , all i want is to make it(focus rectangle) dissappeared.

    But thanks anyway. Please help me to resolved this.
    Last edited by LeonX; Feb 3rd, 2005 at 10:35 PM.

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Focus Rect

    you can't use a picture of a button that doesn't have focus?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    You need to subclass the command button and intercept the WM_SETFOCUS
    message and change the message so its a WM_KILLFOCUS message.

    VB Code:
    1. Private Const WM_KILLFOCUS As Long = &H8
    2. Private Const WM_SETFOCUS As Long = &H7
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Focus Rect

    I think subclassing might create a bit of overhead. Personally I love using DrawEdge() function - works nice. It gives you great deal of flexibility: you may have completely empty form in design filled up with many "controls" at runtime that could be functional but it may become quite tricky, tough.

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Yes, subclassing is allot of work and hard to debug. But if you subclass the
    form then you can sniff out all the control s you want to kill.

    Personally I wouldn't do it, but if the poster is not satisfied with the previous
    answers then I offered it as an alternative.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Focus Rect

    Quote Originally Posted by RobDog888
    ... Personally I wouldn't do it, but if the poster is not satisfied with the previous answers then I offered it as an alternative.

    HTH
    Oh, yes yes, of course.
    I was thinking of a completely different "overhead" ...

  13. #13

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Resolved Re: Focus Rect

    thanks RobDog888
    for your advice, now what i've got what i want, but we don't have to subclassing those command button, i can do that with this code:

    VB Code:
    1. Private Sub cmd_GotFocus(Index As Integer)
    2.     SendMessage cmd(Index).hwnd, WM_KILLFOCUS, cmd(Index).hwnd, 0&
    3. End Sub

    Are those code will causes problems? BTW what is HTH stand for?

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Sorry, I just didnt have the time to create an example for you but are you saying that the code you posted works?
    It would be strange if it did because I dont think the third parameter will be the command button handle again.
    I'm not exactly sure what the correct parameters would be for the third and fourth parameters but I thought if
    this is what you wanted we could try to figure it out.

    Btw, Hope This Helps.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Unhappy Re: Focus Rect

    yes , its work.
    But i found a little problem, when i click the 'button without focus' for the first time, the event click did not triggered. And if i show up a msgbox when i click the button then i close the msgbox, the focus rect visible again at the button.
    can anyone figure it out for me, please ... .

    I have an idea but do not know how to do that, something like this:
    VB Code:
    1. [color=#00007f]Private[/color] [color=#00007f]Sub[/color] cmd_GotFocus(Index [color=#00007f]As[/color] [color=#00007f]Integer[/color])
    2.     If button_message = mouse_click Then ' i do not know how to get this message
    3.         ' simulate mouse_click to this button first
    4.     End If
    5.     ' then kill the focus
    6.     SendMessage cmd(Index).hwnd, WM_KILLFOCUS, cmd(Index).hwnd, 0&
    7. End Sub

  16. #16
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    This is why I believe it needs to be handled with subclassing and intercepting the WM_SETFOCUS message.
    If I get some time I will try to whip up an example for you. I currently have 3
    examples to finish writting. So I will see if I can squeeze it in before bedtime.
    If not I will do it over the weekend.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Focus Rect

    My first idea works ?
    Attached Images Attached Images  

  18. #18
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    No it doesnt work because the focus rectangle is still visible. If you use a picture of a button
    that will not allow for resizing or color changes or font/language changes.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  19. #19
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Ok, here's the deal. In order to kill the focus rectangle you only need to prevent
    the message handling by windows and basically cancel it out. This was not
    as bad as I thought, but it does require subclassing.

    Note: when debugging a subclassed project do not click the stop or pause
    buttions in the VB IDE as it will crash your project. ALWAYS stop a project by
    clicking on the forms x cancel button.

    In case you dont want to download the code this example will kill the focus
    rectangle of command2 command button when you click command1 button.

    VB Code:
    1. 'BEHIND A FORM (FORM1)
    2. Option Explicit
    3.  
    4. Private Sub Command1_Click()
    5.     'PREVENT THE FOCUS RECTANGLE ON COMMAND2 COMMAND BUTTON
    6.     SubClassHwnd Command2.hWnd
    7. End Sub
    8.  
    9.  
    10. 'INSIDE A MODULE (MODULE1)
    11. Option Explicit
    12. '<RR VB/OUTLOOK GURU 02/04/2005 - PREVENT FOCUS RECTANGLE>
    13.  
    14. 'GETWINDOWLONG CONSTANTS
    15. Private Const GWL_WNDPROC   As Long = (-4)
    16.  
    17. 'WINDOWS MESSAGE CONSTANTS
    18. Private Const WM_NOTIFY     As Long = &H4E
    19. Private Const WM_DESTROY    As Long = &H2
    20. Private Const WM_SETFOCUS   As Long = &H7
    21. Private Const WM_KILLFOCUS  As Long = &H8
    22.  
    23. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, _
    24. ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    25.  
    26. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, _
    27. ByVal dwNewLong As Long) As Long
    28.  
    29. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, _
    30. ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    31.  
    32. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    33.  
    34. Private mlPrevWndProc As Long
    35.  
    36. Private Function WindowProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    37.    
    38.         Select Case Msg
    39.         Case WM_SETFOCUS
    40.             'KILL THE FOCUS RECTANGLE
    41.             Exit Function
    42.         Case WM_DESTROY
    43.             'REMOVE SUBCLASSING WHEN COMMAND BUTTON IS DESTROYED (FORM UNLOADED)
    44.             WindowProc = CallWindowProc(mlPrevWndProc, hWnd, Msg, wParam, lParam)
    45.             Call SetWindowLong(hWnd, GWL_WNDPROC, mlPrevWndProc)
    46.             Exit Function 'EXIT FUNCTION WILL PREVENT WINDOWS FROM HANDLING IT ON THE LAST LINE OF CODE
    47.     End Select
    48.     'CALL DEFAULT WINDOW HANDLER
    49.     WindowProc = CallWindowProc(mlPrevWndProc, hWnd, Msg, wParam, lParam)
    50.  
    51. End Function
    52.  
    53. Public Sub SubClassHwnd(ByVal hWnd As Long)
    54.     mlPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
    55. End Sub
    Enjoy!

    VB/Office Guru™
    Attached Files Attached Files
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  20. #20

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Focus Rect

    Thank you, RobDog888
    you help me so much ...
    But, one problem solve , another problem show up.
    I have an array of command button , do i need to subclass it one by one , that's mean i have to write:

    Private mlPrevWndProc1 As Long
    Public Sub SubClassHwnd1
    Private Function WindowProc1

    Private mlPrevWndProc2 As Long
    Public Sub SubClassHwnd2
    Private Function WindowProc2

    Private mlPrevWndProc3 As Long
    Public Sub SubClassHwnd3
    Private Function WindowProc3

    ...
    ...
    ...

    ???
    Is there any other way?


    Regards,
    Ferry

  21. #21
    Hyperactive Member boku's Avatar
    Join Date
    Dec 2004
    Posts
    386

    Re: Focus Rect

    im not sure but i think you can do something like this

    Private mlPrevWndProc1, mlPrevWndProc2, mlPrevWndProc3 As Long

    you have to test it for the other cos im not sure.
    -BoKu-

  22. #22
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Yes, you would have to subclass each one.

    You could also just subclass the entire form, but you would then have to
    parse out the message parameters to determine which control was receiving
    the focus.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  23. #23
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Quote Originally Posted by boku
    im not sure but i think you can do something like this

    Private mlPrevWndProc1, mlPrevWndProc2, mlPrevWndProc3 As Long

    you have to test it for the other cos im not sure.
    You cant dimension the variables that way because your only dimming the
    last one as long. The first and second ones are undefined so they would be
    dimmed as Variant.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  24. #24
    Hyperactive Member boku's Avatar
    Join Date
    Dec 2004
    Posts
    386

    Re: Focus Rect

    oh ok, cheers for that learn somin new lol.
    -BoKu-

  25. #25
    Junior Member
    Join Date
    Dec 2005
    Posts
    23

    Re: Focus Rect

    I know this is an old thread.. but I have to throw this in

    Normally if someone wants to do this it is for asthetic reasons ... and the simple way to do it would be to just set the focus to something else through the click event ... the button does not have focus anymore - but that obviously is not an issue because the user would not really know what has focus anyway.

  26. #26
    Junior Member
    Join Date
    Dec 2005
    Posts
    23

    Re: Focus Rect

    .. (or through the onfocus event ...)

  27. #27
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Yes, but if they dont realize that another control actually hs the focus it may cause unexpected results for the user when they try to activate the button by normal means. Killing the focus via subclassing is the best and most solid method for this.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  28. #28
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Focus Rect

    RD...

    I have a control array of command buttons - and changed your code to this

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        'PREVENT THE FOCUS RECTANGLE ON COMMAND2 COMMAND BUTTON
        SubClassHwnd Command2(0).hWnd
        SubClassHwnd Command2(1).hWnd
    End Sub
    Subclassing each index of the control array.

    And that seems to work - without having to add additional WindowProc routines.

    Thanks for the code!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  29. #29
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    No prob. Thanks for the update.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  30. #30
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Focus Rect

    LeonX's & szlamany's Problems are already solved i guess. Thanks to Rob's Subclassing Technique.

    But there seem to be another method. That is to draw a line over the focus rect using the backcolor of the object.

    In my code, i can erase my own focusrect drawn on the form. I can apply this technique to the command button as well. Problem is, i don't no how to get the rect of the focus rect drawn by the system on the command button.

    Code:
    Option Explicit
    
    Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    
    Private Declare Function CreatePen Lib "gdi32" _
    (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    
    Private Declare Function GetBkcolor Lib "gdi32.dll" Alias "GetBkColor" (ByVal hdc As Long) As Long
    
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
    
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) 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 rt As RECT
    Private pt As POINTAPI
    
    Private Const PS_SOLID = 0 'Brush Type
    
    Private lpn As Long
    Private lOldObj As Long
    Code:
    Private Sub Command1_Click()
    rt.Top = 10
    rt.Left = 10
    rt.Right = 100
    rt.Bottom = 100
    DrawFocusRect Me.hdc, rt
    End Sub
    Code:
    Private Sub EraseRect()
    lpn = CreatePen(PS_SOLID, 4, GetBkcolor(Form1.hdc))
    'create a pen
    
    lOldObj = SelectObject(Form1.hdc, lpn)
    'Select a Brush in to the DC
    
    MoveToEx Form1.hdc, rt.Left, rt.Top, pt
    LineTo Form1.hdc, rt.Right, rt.Top
    
    MoveToEx Form1.hdc, rt.Right, rt.Top, pt
    LineTo Form1.hdc, rt.Right, rt.Bottom
    
    MoveToEx Form1.hdc, rt.Right, rt.Bottom, pt
    LineTo Form1.hdc, rt.Left, rt.Bottom
    
    MoveToEx Form1.hdc, rt.Left, rt.Bottom, pt
    LineTo Form1.hdc, rt.Left, rt.Top
    
    DeleteObject lpn
    ReleaseDC Me.hwnd, Me.hdc
    
    End Sub
    Code:
    Private Sub Command2_Click()
    EraseRect
    End Sub
    Edited : OOoOooos, Now only noticed. A thread created way back in 2005
    Attached Files Attached Files
    Last edited by Fazi; Dec 23rd, 2007 at 07:07 AM.

  31. #31
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Focus Rect

    @fazi - doesn't matter if the thread is 2 years old - anything you can add to the discusion is always appreciated

    We already subclass several aspects of our user interface - capture mouse wheel scrolling so that the flexgrid the mouse is over gets scrolled nicely...

    Capture WM_APPACTIVATE so that when our app re-gains focus from some other task running on the PC we know about it - something no form event tells you about.

    So adding this additional subclass for WM_SETFOCUS was a simple consideration.

    Our command buttons are small - so having that FOCUS RECTANGLE interferes with the "caption" on the button - simply doesn't look good. So we decided that changing the color of the button "selected" would work nicely.

    Most visually appealling would be the color without the FOCUS RECTANGLE.

    This image shows where we were going with all this (note that we are currently working on getting rid of the "bleed" of color that appears above the frame)
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  32. #32

  33. #33
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Quote Originally Posted by RhinoBull
    So much about basically nothing - place controls inside picturebox and set focus to container. That's all.
    That wont work as the focus is still desired and its function, just its appearance is not desired. If by using your method you press the space bar or enter key then there would be no action of the button since the picturebox has the focus.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  34. #34
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Quote Originally Posted by Fazi
    LeonX's & szlamany's Problems are already solved i guess. Thanks to Rob's Subclassing Technique.

    But there seem to be another method. That is to draw a line over the focus rect using the backcolor of the object.

    In my code, i can erase my own focusrect drawn on the form. I can apply this technique to the command button as well. Problem is, i don't no how to get the rect of the focus rect drawn by the system on the command button.

    Code:
    Option Explicit
    
    Private Declare Function DrawFocusRect Lib "user32" (ByVal hdc As Long, lpRect As RECT) As Long
    
    Private Declare Function CreatePen Lib "gdi32" _
    (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    
    Private Declare Function GetBkcolor Lib "gdi32.dll" Alias "GetBkColor" (ByVal hdc As Long) As Long
    
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
    
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) 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 rt As RECT
    Private pt As POINTAPI
    
    Private Const PS_SOLID = 0 'Brush Type
    
    Private lpn As Long
    Private lOldObj As Long
    Code:
    Private Sub Command1_Click()
    rt.Top = 10
    rt.Left = 10
    rt.Right = 100
    rt.Bottom = 100
    DrawFocusRect Me.hdc, rt
    End Sub
    Code:
    Private Sub EraseRect()
    lpn = CreatePen(PS_SOLID, 4, GetBkcolor(Form1.hdc))
    'create a pen
    
    lOldObj = SelectObject(Form1.hdc, lpn)
    'Select a Brush in to the DC
    
    MoveToEx Form1.hdc, rt.Left, rt.Top, pt
    LineTo Form1.hdc, rt.Right, rt.Top
    
    MoveToEx Form1.hdc, rt.Right, rt.Top, pt
    LineTo Form1.hdc, rt.Right, rt.Bottom
    
    MoveToEx Form1.hdc, rt.Right, rt.Bottom, pt
    LineTo Form1.hdc, rt.Left, rt.Bottom
    
    MoveToEx Form1.hdc, rt.Left, rt.Bottom, pt
    LineTo Form1.hdc, rt.Left, rt.Top
    
    DeleteObject lpn
    ReleaseDC Me.hwnd, Me.hdc
    
    End Sub
    Code:
    Private Sub Command2_Click()
    EraseRect
    End Sub
    Edited : OOoOooos, Now only noticed. A thread created way back in 2005
    Good idea but takes more code and when the form is moved or invalidated the focus rectangle will be redrawn again by the system.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  35. #35
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Focus Rect

    Quote Originally Posted by RobDog888
    That wont work as the focus is still desired and its function, just its appearance is not desired. If by using your method you press the space bar or enter key then there would be no action of the button since the picturebox has the focus.
    So? Use the mouse. Most of skinned apps don't allow setting focus at all...
    What's the big deal? Use picturebox with images to create your own button so it can get the focus but going through that much subcalssing troubles for just a "silly" focus rectangle is way too much if you ask me.

  36. #36
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Well thats your opinion and if subclassing is already established in the project then its no extra trouble at all. Adding an extra picturebox for each button is making your form heavier and changes the functionality.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  37. #37
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Focus Rect

    Quote Originally Posted by RobDog888
    ...Adding an extra picturebox for each button is making your form heavier and changes the functionality.
    How would substituting (note: not adding an extra picturebox) button with picturebox make "form heavier and change the functionality"?

  38. #38
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    This thread is getting off topic but a picturebox is considered a heavy control. Then if you have severl buttons to do that suggestion with it adds even more. As you have stated the focus not being on the button anymore will force the user to use the mouse to click it, that a change in functionality.

    Later
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  39. #39
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Focus Rect

    Quote Originally Posted by RobDog888
    This thread is getting off topic but a picturebox is considered a heavy control...
    I think we stay right on topic. However, who considers picturebox to be heavy? It's "heavier" than image control but only for one reason - picturebox is a window and image control is not. Command button just like picturebox is a window.
    By substituting command button with picturebox you don't add much weight to your form in this case - you won't be processing large images.

    But opinions vary so I figured I mention mine so everyone see the difference and have an alternative way (and btw far not a bad one).

    Regards to all.

  40. #40
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Focus Rect

    Well this was an old thread and debating which is the better method is not on topic. If you want to converse about that topic more then a new thread is needed.

    Otherwise any on topic replies in regards to szlamany's thread can be made there.
    http://vbforums.com/showthread.php?t=501882

    Thread Closed
    Last edited by RobDog888; Dec 23rd, 2007 at 02:54 PM. Reason: Added link to szlamany's thread
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width