Results 1 to 40 of 40

Thread: Focus Rect

Threaded View

  1. #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

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