Hola Jose,
I think that Wilco is interested not only in the focus but also in the look his button have.
Meaning the focus dotted line and the highlighted button edge.
I did some quick test on Windows 7 and 10, with Common control 6.0 and 5.82,
And SetFocus/KeyBd_Event, SetFocus/WM_CHANGEUISTATE, PostMessage/SetFocus.
Results are in the code, in the "Select Case" section. :-)
Pierre
#Define JumpCompiler "<D:\Free\64\fbc.exe>"
#Define JumpCompilerCmd "<-s gui "D:\Free\bas\~~Default.rc">"
#define unicode
#Include Once "windows.bi"
#Include Once "win\shellapi.bi" 'ExtractIcon()
ENUM
ButtonOk = 101
ButtonEsc
END ENUM
#Define AppName "Focus"
Dim Shared As HINSTANCE hInstance : hInstance = GetModuleHandle(NULL)
'______________________________________________________________________________
Function WndProc(hWnd As HWND, uMsg As UINT, wParam As WPARAM, lParam As LPARAM) As Integer
Static hButtonOk As HWND
Dim hButtonEsc As HWND
Static hFont As HFONT
Select Case (uMsg)
Case WM_CREATE
Function = False
hFont = GetStockObject(DEFAULT_GUI_FONT)
hButtonOk = CreateWindowEx(0, "Button", "&OK", _
WS_CHILD Or WS_VISIBLE Or WS_TABSTOP Or WS_GROUP Or _
BS_CENTER Or BS_VCENTER, _
25, 15, 80, 30, _
hWnd, Cast(HMENU, ButtonOk), _
hInstance, NULL)
SendMessage(hButtonOk, WM_SETFONT, Cast(UInteger, hFont), TRUE)
hButtonEsc = CreateWindowEx(0, "Button", "&Cancel", _
WS_CHILD Or WS_VISIBLE Or BS_CENTER Or WS_TABSTOP Or _
BS_NOTIFY Or BS_TEXT Or BS_VCENTER, _
135, 15, 80, 30, _
hWnd, Cast(HMENU, ButtonEsc), _
hInstance, NULL)
SendMessage(hButtonEsc, WM_SETFONT, Cast(UInteger, hFont), TRUE)
Dim As Long TryOption = 1 '+ 1 + 1
Select Case TryOption
Case 1
'Button "OK" start with dotted line and highlighted edge if CommonControl 6.0 (Win 10 and Win 7)
'Button "OK" start with dotted line and no highlight edge if CommonControl 5.82 (Win 10 and Win 7)
SetFocus(hButtonOk)
KeyBd_Event VK_TAB, 0, 0, 0
KeyBd_Event VK_TAB, 0, KEYEVENTF_KEYUP, 0
KeyBd_Event VK_SHIFT, 0, 0, 0
KeyBd_Event VK_TAB, 0, 0, 0
KeyBd_Event VK_TAB, 0, KEYEVENTF_KEYUP, 0
KeyBd_Event VK_SHIFT, 0, KEYEVENTF_KEYUP, 0
Case 2
'Button "OK" start with dotted line and no highlighted edge if CommonControl 6.0 (Win 10 and Win 7)
'Button "OK" start with dotted line and no highlighted edge if CommonControl 5.82 (Win 10 and Win 7)
SetFocus(hButtonOk) 'SetFocus alone is ok if no CommControl6
SendMessage(hWnd, WM_CHANGEUISTATE, MakeLong(UIS_CLEAR, UISF_HIDEFOCUS), 0)
Case 3
'Button "OK" start with no dotted line and no highlight edge if CommonControl 6.0 (Win 10 and Win 7)
'Button "OK" start with dotted line and no highlight edge if CommonControl 5.82 (Win 10 and Win 7)
PostMessage(hWnd, WM_APP, 0, 0)
End Select
Case WM_APP
SetFocus(hButtonOk)
Case WM_CHANGEUISTATE
Case WM_COMMAND
Select Case LoWord(wParam)
Case ButtonOk
If HiWord(wParam) = BN_CLICKED Then
_Beep(1500, 100)
EndIf
Case ButtonEsc, IDCANCEL
If HiWord(wParam) = BN_CLICKED Then
PostMessage(hWnd, WM_CLOSE, 0, 0)
Exit Function
EndIf
End Select
Case WM_DESTROY
DeleteObject(hFont)
PostQuitMessage(0)
Exit Function
End Select
Function = DefWindowProc(hWnd, uMsg, wParam, lParam)
End Function
'_____________________________________________________________________________
Function WinMain(hInstance As HINSTANCE, hPrevInst As HINSTANCE, _
CmdLine As WString Ptr, CmdShow As Integer) As UINT
Dim WinClass As WNDCLASS
Dim wMsg As MSG
Dim hWnd As HWND
Dim hIco As HICON
Dim WindowSize As SIZEL
Dim wsAppName As WSTRING * 128
wsAppName = AppName & " - " & SizeOf(UInteger) * 8
WindowSize.cx = 245
WindowSize.cy = 90
hIco = ExtractIcon(hInstance, "%SystemRoot%\System32\PowrPrOf.dll", 1)
WinClass.style = CS_HREDRAW Or CS_VREDRAW
WinClass.lpfnWndProc = ProcPtr(WndProc)
WinClass.cbClsExtra = 0
WinClass.cbWndExtra = 0
WinClass.hInstance = hInstance
WinClass.hIcon = hIco
WinClass.hCursor = LoadCursor(NULL, IDC_ARROW)
WinClass.hbrBackground = Cast(HGDIOBJ, COLOR_BTNFACE + 1) 'Default color
WinClass.lpszMenuName = NULL
WinClass.lpszClassName = @wsAppName
If (RegisterClass(@WinClass)) Then
hWnd = CreateWindowEx(WS_EX_WINDOWEDGE, _
wsAppName, wsAppName, _
WS_OVERLAPPED OR WS_CLIPCHILDREN Or WS_DLGFRAME Or WS_BORDER Or WS_VISIBLE Or WS_CAPTION Or _
WS_MAXIMIZEBOX Or WS_MINIMIZEBOX Or WS_SYSMENU , _
(GetSystemMetrics(SM_CXSCREEN) - WindowSize.cx) / 2, _ 'PosH
(GetSystemMetrics(SM_CYSCREEN) - WindowSize.cy) / 2, _ 'PosV
WindowSize.cx, WindowSize.cy, _ 'Width, height
NULL, NULL, hInstance, NULL)
ShowWindow(hWnd, SW_SHOW)
UpdateWindow(hWnd)
While GetMessage(@wMsg, ByVal NULL, 0, 0) > 0
If IsDialogMessage(hWnd, @wMsg) = 0 Then
TranslateMessage(@wMsg)
DispatchMessage(@wMsg)
End If
Wend
End If
DestroyIcon(hIco)
Function = wMsg.message
End Function
'_____________________________________________________________________________
End WinMain(hInstance, NULL, Command(), SW_NORMAL) 'Call main() and return the error code to the OS
'_____________________________________________________________________________
'