0

How to create a button or something with title looking like a link, on Windows applications's windows, dialog boxes, etc? It should receive keyboard focus, and when mouse pointer is over it, its text should be underlined. I am using C++ Builder. This can be done using TLabel or TStaticText. But the problem is that, it does not receive keyboard focus. I would like it to receive keyboard focus and be tab-stopped. Thanks.

  • 4
    Use a [Command Link](https://msdn.microsoft.com/en-us/library/windows/desktop/dn742403.aspx) control. It provides all features you are looking for, with the exception of underlining the text on hover. – IInspectable Jul 20 '15 at 14:07
  • Thanks. I know about it. I think it is supported on Vista and above. –  Jul 20 '15 at 14:37
  • Correct. Command Links are available for any supported Windows platform. – IInspectable Jul 20 '15 at 14:45
  • I just need text links that can receive keyboard focus. –  Jul 20 '15 at 14:53
  • 2
    [SysLink control?](https://msdn.microsoft.com/en-us/library/windows/desktop/bb760704%28v=vs.85%29.aspx) – andlabs Jul 20 '15 at 16:30
  • 2
    `TStaticText` is a windowed control (`TWinControl` descendant) and thus can receive keyboard input. – Remy Lebeau Jul 20 '15 at 16:52
  • Thanks for your comments. It must not necessarily be hypertext to URL. I tried with TStaticText but it does not receive focus. Focused controls usually are surrounded by dashed frame, and when you press enter at that time, it is selected/executed. Please take a look at Skype's Profile window for examples. –  Jul 21 '15 at 03:51
  • 1
    Controls implement focus grabbing and drawing the focus rect themselves. If you are using TStaticText or implementing your own window class for this, you will need to call `SetFocus()` in `WM_LBUTTONDOWN` and have `WS_TABSTOP` in each instance, call [`DrawFocusRect()`](https://msdn.microsoft.com/en-us/library/dd162479%28v=vs.85%29.aspx) in `WM_PAINT` if the window does have focus, and handle `WM_SETFOCUS` and `WM_KILLFOCUS` to redraw your window. – andlabs Jul 21 '15 at 12:51
  • Sounds good, thanks. I will try. –  Jul 22 '15 at 03:41
  • `DrawFocusRect()` uses XOR and is designed to be lightweight. In `WM_SETFOCUS` and `WM_KILLFOCUS` just `DrawFocusRect()`, not a full repaint. Likewise, only do `DrawFocusRect()` in `WM_PAINT` if the background gets erased. – Renate Jul 10 '20 at 15:48

0 Answers0