How to add or remove focus rectangle to controls
Here is how to remove the “Focus Rectangle” from any or all controls on a form. It can also be used, when using XP visual styles, to put the “Focus Rectangle”, back on controls, that is missing from.
Add these declarations to the main (common) module.
vb Code:
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Function MakeLong(ByVal wLow As Integer, _
ByVal wHigh As Integer) As Long
MakeLong = wHigh * &H10000 + wLow
End Function
Public Const WM_ChangeUIState As Long = &H127
Public Const UIS_HideRectangle As Integer = &H1
Public Const UIS_ShowRectangle As Integer = &H2
Public Const UISF_FocusRectangle As Integer = &H1
Now add one of these to the form load event of each form.
vb Code:
Call SendMessage(Me.hwnd, WM_ChangeUIState, _
MakeLong(UIS_HideRectangle, UISF_FocusRectangle), ByVal 0)
Call SendMessage(Me.hwnd, WM_ChangeUIState, _
MakeLong(UIS_ShowRectangle, UISF_FocusRectangle), ByVal 0)
If you want to be selective, you can use these commands on individual controls, instead of in the form load event. When you turn it off, it will stay off, until turned on again, and visa versa.
IMMPORTANT NOTE Some controls, such as command buttons, list boxes, and combo boxes, etc., will work, both in design, and run time. Other controls, such as check boxes, and radio option buttons, etc., will only work at run time, i.e. You must first compile the programme, and run it from the “.EXE” file.
They will work on standard, or XP visual style, controls, but with one exception I have found so far. When a command button has a picture loaded, it will revert to standard style, if using XP styles, and the focus rectangle will always be visible. If you don’t want it, the only way I can find round this, is to not use a command button at all, but use 2 or 3 picture boxes instead, for the normal, (focused), and pressed states. The focused state can then be just a thin black outline, around the image, the same as a normal button, with the focus rectangle removed.
Hope this is useful to someone, Trevor.
Re: How to add or remove focus rectangle to controls
Re: How to add or remove focus rectangle to controls
Interesting, didn't know about that message. Here's the official documentation.
Recommend using the standard documented names for the constants.
Link also describes the hierarchy applied.
Note: WM_QUERYUISTATE returns the current setting.