title: Set full focus on a button (`SetFocus()` is not enough)
body: <p>Is there a way to set true full focus on a push button (<code>button</code> window class) in WinAPI?</p>
  
  <p><code>SetFocus()</code> <em>somewhat</em> sets focus (the button gets an inner dotted border), but the button is actually <em>partially</em> focused and still cannot be pressed with the <code>Enter</code> key, only the <code>Spacebar</code> key works. At the same time, if I move focus to a sibling button with the <code>Tab</code> key, then this sibling button (as well as the first button if I then return focus to it using <code>Shift+Tab</code>) gets a <em>true</em> focus (visually, not just <em>inner</em> dotted focus border is added to the really focused button, but its main outer border becomes <em>blue</em> [Windows 7]), and now it reacts to <code>Enter</code> as intended.</p>
  
  <p>How to make a button such <em>fully</em> focused programmatically?</p>
  
  <p>Screenshot of the three button states:</p>
  
  <p><img src="https://i.stack.imgur.com/ttkW3.png" alt="Screenshot of the three button states">  </p>
  
  <p>Some background for clarity: there is a window (created with the regular combination of <code>WNDCLASSEX</code> / <code>RegisterClassEx</code> / <code>CreateWindowEx()</code> with <code>WS_OVERLAPPEDWINDOW</code> as its style) with a multiline edit box (<code>edit</code> window class with <code>ES_MULTILINE</code> style) and several push buttons. To implement keyboard navigation using the <code>Tab</code> key, I process the <code>WM_KEYDOWN</code> event in the edit box's procedure (subclassed via <code>SetWindowLong()</code>), otherwise I could navigate between buttons and from buttons to the edit box, but not from the edit box to a button. All the controls have <code>WS_TABSTOP</code> style. The issue with the button focus takes place when I set focus using <code>SetFocus()</code> when the <code>Tab</code> key is pressed on the edit box having focus and caret.</p>
  
  <p>Minimal relevant C++ code:</p>
  
  <p></p>
  
  <pre><code>HWND mainWindow;
  WNDPROC defaultEditCallback = NULL;
  
  int WINAPI WinMain(HINSTANCE instance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
      WNDCLASSEXW wc;
  
      wchar_t windowClass[] = L"testcase";
  
      wc.cbSize        = sizeof(WNDCLASSEXW);
      wc.style         = 0;
      wc.lpfnWndProc   = mainWindowCallback;
      wc.cbClsExtra    = 0;
      wc.cbWndExtra    = 0;
      wc.hInstance     = instance;
      wc.hIcon         = NULL;
      wc.hCursor       = LoadCursorW(NULL, IDC_ARROW);
      wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
      wc.lpszMenuName  = NULL;
      wc.lpszClassName = (LPCWSTR)windowClass;
      wc.hIconSm       = NULL;
  
      RegisterClassExW(&amp;wc);
  
      mainWindow = CreateWindowW(
          (LPCWSTR)windowClass, (LPCWSTR)windowClass, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, CW_USEDEFAULT, 400, 200, NULL, NULL, instance, NULL
      );
  
      HWND edit = CreateWindowExW(
          WS_EX_CLIENTEDGE, (LPCWSTR)L"edit", NULL,
          WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | WS_TABSTOP,
          0, 0, 0, 0, mainWindow, (HMENU) 10,
          (HINSTANCE) GetWindowLongPtrW(mainWindow, GWLP_HINSTANCE),
          NULL
      );
  
      defaultEditCallback = (WNDPROC)SetWindowLongPtrW(edit, GWLP_WNDPROC, (LONG)editCallback);
  
      HWND firstButton  = createButton(mainWindow, 20, L"First",  buttonWidth, buttonHeight);
      HWND secondButton = createButton(mainWindow, 30, L"Second", buttonWidth, buttonHeight);
      HWND thirdButton  = createButton(mainWindow, 40, L"Third",  buttonWidth, buttonHeight);
  
      // [Skipped] Sizing and positioning controls.
  
      ShowWindow(mainWindow, nCmdShow);
      UpdateWindow(mainWindow);
  
      MSG msg;
  
      while (GetMessageW(&amp;msg, NULL, 0, 0) &gt; 0) {
          if (!IsDialogMessage(mainWindow, &amp;msg)) {
              TranslateMessage(&amp;msg);
              DispatchMessageW(&amp;msg);
          }
      }
  
      return (int)msg.wParam;
  }
  
  LRESULT CALLBACK mainWindowCallback(HWND window, UINT msg, WPARAM wParam, LPARAM lParam) {
      if (WM_DESTROY == msg) {
          PostQuitMessage(0);
      }
  
      return DefWindowProcW(window, msg, wParam, lParam);
  }
  
  LRESULT CALLBACK editCallback(HWND control, UINT msg, WPARAM wParam, LPARAM lParam) {
      if (WM_KEYDOWN == msg &amp;&amp; VK_TAB == wParam) {
          HWND next = GetNextDlgTabItem(mainWindow, control, (int)(GetKeyState(VK_SHIFT) &amp; 0x8000));
          SetFocus(next);
          return 0;
      }
  
      return CallWindowProc(defaultEditCallback, control, msg, wParam, lParam);
  }
  
  
  HWND createButton(HWND parentWindow, int id, wchar_t* caption, int width, int height) {
      return CreateWindowW(
          (LPCWSTR)L"button", (LPCWSTR)caption, WS_VISIBLE | WS_CHILD | WS_TABSTOP,
          0, 0, width, height, parentWindow, (HMENU)id, NULL, NULL
      );
  }
  </code></pre>
  
  <p>Thanks.</p>