none
custom controls and focus RRS feed

  • Question

  • I'm writing a custom control and I don't understand how the focus is set and when. My main question is, if I have to do something when getting a WM_ACTIVATE or WM_MOUSEACTIVATE to get the focus or not. I tryed not to do anything, but this doesn't work... so my guess is, that I forgot something. What could that be? Maybe the WS_TABSTOP? Is this needed? Or is the other guess right, that I have to handle WM_ACTIVATE somehow? What else could be the problem?

    Rudolf

    Sunday, August 16, 2015 1:50 PM

Answers

All replies

  • On 8/16/2015 9:50 AM, Rudolf Meier wrote:

    I'm writing a custom control and I don't understand how the focus is set and when. My main question is, if I have to do something when getting a WM_ACTIVATE or WM_MOUSEACTIVATE to get the focus or not.

    I'm pretty sure these messages are only sent to top-level windows, which yours most likely isn't.

    You likely want to take focus (by calling SetFocus) when your control is clicked, that is, when you receive WM_LBUTTONDOWN.


    Igor Tandetnik
    Sunday, August 16, 2015 1:56 PM
  • You likely want to take focus (by calling SetFocus) when your control is clicked, that is, when you receive WM_LBUTTONDOWN.


    Igor Tandetnik
    I don't know everything about the focus on dialogs. But what I know is, that you should NEVER call SetFocus (except when handling WM_INITDIALOG).
    Sunday, August 16, 2015 2:46 PM
  • On 8/16/2015 10:46 AM, Rudolf Meier wrote:

    You likely want to take focus (by calling SetFocus) when your control is clicked, that is, when you receive WM_LBUTTONDOWN.

    I don't know everything about the focus on dialogs. But what I know is, that you should NEVER call SetFocus (except when handling WM_INITDIALOG).

    Who is "you" in this sentence? The dialog procedure indeed rarely needs to call SetFocus explicitly. But that's because each individual control calls it as necessary.

    In particular, the dialog procedure doesn't see mouse messages sent to individual controls. It's up to the control to grab focus when clicked, if it's so inclined. The dialog has no way to know whether your custom control wants to behave like, say, an edit box (takes focus when clicked) or like a static control (doesn't take focus).

    Keyboard messages are another matter. Tabbing is handled by the dialog, based on WS_TABSTOP style. This style is usually assigned to controls by the dialog's designer; it's not a responsibility of each control.


    Igor Tandetnik
    Sunday, August 16, 2015 4:35 PM
  • the "you" is the control ... WM_NEXTDLGCTL should be used
    • Marked as answer by Shu 2017 Monday, August 24, 2015 2:21 AM
    Sunday, August 16, 2015 6:12 PM