CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    9

    Focus Problem in MFC Dialog Project

    Hi everyone,

    I've created a dialog project to implement a simple calculator. I'm trying to set the focus to be on my dialog so I will be able to use the WM_KEYDOWN to capture keyboard input. I've enabled the ON_WM_SETFOCUS() and ON_WM_KILLFOCUS() options so I will know when my dialog gets the focus, and when then focus is taken away from it.

    I've tried setting the focus using SetFocus() in the following way:


    BOOL CMyCalculatorDlg::OnInitDialog()

    {

    CDialog::OnInitDialog();

    // Set the icon for this dialog. The framework does this automatically

    // when the application's main window is not a dialog

    SetIcon(m_hIcon, TRUE); // Set big icon

    SetIcon(m_hIcon, FALSE); // Set small icon


    CWnd::SetFocus();


    return FALSE; // return TRUE unless you set the focus to a control

    }



    However, when debugging the program I see that the focus is indeed given to the dialog at first ( ON_WM_SETFOCUS() is activated), but then the focus is immediately taken away from it ( ON_WM_KILLFOCUS() is activated ), and because of this the dialog doesn't respond to key pressings.

    What am I doing wrong?

    BTW: I'm using visual studio 2005 with an MFC project.

    Thanks alot!

  2. #2
    Join Date
    Feb 2009
    Posts
    42

    Re: Focus Problem in MFC Dialog Project

    Maybe this?

    return FALSE; // return TRUE unless you set the focus to a control

    Put it back to TRUE and see what is happening

  3. #3
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    No, it was TRUE to begin with, the behavior stays the same.

    I understood that returning FALSE tells the dialog manager (I'm not sure that's the name of it) that the focus was set to the current dialog, but I'm not sure.

  4. #4
    Join Date
    Feb 2009
    Posts
    42

    Re: Focus Problem in MFC Dialog Project

    Look, If you have made the dialog and select it that dialog have a focus, then press some key on the keyboard, message will be dispatched to your dialog.
    You need to have OnKeyDown event handled:

    OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)

    then you just handle nChar-s to call appropriate functions:
    if( nChar == '1' ) {
    Pressed(1);
    }

    if( nChar == VK_RETURN ) {
    Calculate();
    }

    etc...

    You don't need to bother with focus not focus problem.

  5. #5
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    I've already added the use of OnKeyDown. I press the keyboard upon startup of the dialog but this function is never entered. That what made me suspect the focus issue from the first place.
    Then I added the OnSetFocus and OnKillFocus to try and take a deeper look into this issue, and I saw that the dialog does get the focus on startup (OnSetFocus is entered), but is then stripped away from it (OnKillFocus is entered).

  6. #6
    Join Date
    Feb 2002
    Posts
    3,788

    Re: Focus Problem in MFC Dialog Project

    Quote Originally Posted by Ido_I View Post
    I've already added the use of OnKeyDown. I press the keyboard upon startup of the dialog but this function is never entered. That what made me suspect the focus issue from the first place.
    Then I added the OnSetFocus and OnKillFocus to try and take a deeper look into this issue, and I saw that the dialog does get the focus on startup (OnSetFocus is entered), but is then stripped away from it (OnKillFocus is entered).
    we need to understand what is the exact behavior that you are after and what part of that is not working.

  7. #7
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Focus Problem in MFC Dialog Project

    Quote Originally Posted by Ido_I View Post
    I've enabled the ON_WM_SETFOCUS() and ON_WM_KILLFOCUS() options so
    Correction: you did not enable anything. You have inserted WM_SETFOCUS and WM_KILLFOCUS handlers.

    [QUOTE= I'm trying to set the focus to be on my dialog so I will be able to use the WM_KEYDOWN to capture keyboard input. . .
    What am I doing wrong?

    BTW: I'm using visual studio 2005 with an MFC project.[/QUOTE]
    Quote Originally Posted by Ido_I View Post
    I would like the dialog box to control the keyboard input from one main place. The way I understand it, if I use OnKeyDown in the main dialog file (XXDlg.cpp), then every input of the keyboard should direct me to the OnKeyDown routine,
    Quote Originally Posted by Ido_I View Post
    I know I'm doing something wrong, as my knowledge of the MFC is very poor,
    It does not matter what version of VS you are using. Also, this is not MFC. It is a dialog default behavior that you are struggling against. By design, dialog that has at least one control, never has a keyboard focus.
    Quote Originally Posted by Ido_I View Post
    I know I'm doing something wrong, as my knowledge of the MFC is very poor,
    Proper way is to insert handlers for each control. Alternatively, you can use OnNotify or OnCommand if controls generate command or notification messages.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    I'm getting a little confused now - I'm trying all these advises but none is working. Let's start from the top:

    Let's leave the calculator aside. Suppose I've just created a totaly new MFC Application project called "Test". The project is created with the "OK" and "Cancel" buttons. I didn't touch anything yet.

    The question is simple - what are the steps I have to do (after creating the "Test" project from scratch) in-order to capture the keyboard input, when I start from this point?


    From all the comments I understand it should be simple, and eventually comes down to adding the WM_KEYDOWN event handler. But I add it and insert a breakpoint inside CTestDlg::OnKeyDown.
    But when I run this project, this breakpoint is never reached, meaning CTestDlg::OnKeyDown is never entered.

    so again, starting from scratch after creating the project, what are the steps I need to do?

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Focus Problem in MFC Dialog Project

    Quote Originally Posted by Ido_I View Post
    I'm getting a little confused now - I'm trying all these advises but none is working.
    ....
    From all the comments I understand it should be simple, and eventually comes down to adding the WM_KEYDOWN event handler. But I add it and insert a breakpoint inside CTestDlg::OnKeyDown.
    But when I run this project, this breakpoint is never reached, meaning CTestDlg::OnKeyDown is never entered.

    so again, starting from scratch after creating the project, what are the steps I need to do?
    I wonder, do you ever read the answers?
    JohnCz has just written you very clear:

    Quote Originally Posted by JohnCz View Post
    It does not matter what version of VS you are using. Also, this is not MFC. It is a dialog default behavior that you are struggling against. By design, dialog that has at least one control, never has a keyboard focus.
    Proper way is to insert handlers for each control. Alternatively, you can use OnNotify or OnCommand if controls generate command or notification messages.
    you could also try to override PreTranslateMessage method and handle WM_KEYDOWN there, although, it is not the best way and should be usually avoided, unless your actions on the pressed keys were the same independent on the control currently having focus.
    Victor Nijegorodov

  10. #10
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    Quote Originally Posted by JohnCz View Post
    Proper way is to insert handlers for each control. Alternatively, you can use OnNotify or OnCommand if controls generate command or notification messages.

    If I still want to do it straight-forward by inserting handlers, I can still the following handlers are available for each button control:
    BCN_HOTITEMCHANGE, BN_CLICKED, BN_DOUBLECLICKED, BN_KILLFOCUS, BN_SETFOCUS and NM_THEMECHANGED.

    How is any of these handlers related to keyboard input control?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Focus Problem in MFC Dialog Project

    Quote Originally Posted by Ido_I View Post
    If I still want to do it straight-forward by inserting handlers, I can still the following handlers are available for each button control:
    BCN_HOTITEMCHANGE, BN_CLICKED, BN_DOUBLECLICKED, BN_KILLFOCUS, BN_SETFOCUS and NM_THEMECHANGED.

    How is any of these handlers related to keyboard input control?
    None of them. They all are just notification messages
    You should subclass your controls and handle this message in the derived classes.
    Victor Nijegorodov

  12. #12
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Focus Problem in MFC Dialog Project

    Quote Originally Posted by Ido_I View Post
    I read the answers alright, there's no need to offend or to attack. As someone who was introduced to the whole OOP concept literaly just a few days ago,
    Good luck. Nevertheless question you have is not related to OOP paradigm. It is related to Windows programming.

    Quote Originally Posted by Ido_I View Post
    How is any of these handlers related to keyboard input control?
    They are not handlers. As Victor pointed they are notification codes, used in a WM_COMMAND message when button is pressed using either mouse or (having focus) enter or a space bar.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  13. #13
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    I read the answers alright, there's no need to offend or to attack. As someone who was introduced to the whole OOP concept literaly just a few days ago, I can only hope that the OOP veterans here can understand that trevial things to them might be very intrevial to me.

    My actions on the pressed keys are the same, independent on the control currently having focus, so I'll try and see what PreTranslateMessage means and try this direction.

  14. #14
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    The problem is solved using the PreTranslateMessage, thanks for all who bothered to help!

    It will still be nice to get an answer on my last question though...

  15. #15
    Join Date
    Feb 2009
    Posts
    9

    Re: Focus Problem in MFC Dialog Project

    OK, now it all makes sense!!

    Many thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured