Set focus not working

I have a very simple child window that contains 2 edit box and 2 buttons. However, I'm not able to set the to one of those edit boxes by clicking on it. When I remove the window style WS_CHILDWINDOW from the creation of the window, I can set the focus by clicking but cannot move the focus among those items by using the TAB button.

The creation of the child window is as below:

1
2
3
4
5
this->hWnd = CreateWindowEx (0, this->wndClass.lpszClassName, wndTitle_p,
                             WS_CHILDWINDOW | WS_BORDER | WS_TILEDWINDOW | 
                             WS_OVERLAPPED | WS_TABSTOP, 
                             x_p, y_p, width_p, hight_p, 
                             prntWnd_p->hWnd, NULL, this->hInst, NULL);


The creation of the edit boxes is as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
ebStyle_v = WS_CHILD | WS_VISIBLE | WS_TABSTOP;

// Edit Box with Id 1 will be the beginning of 1st group
if (this->ebClass.ctrlId == 1)
    ebStyle_v = ebStyle_v | WS_GROUP;

this->ebField = CreateWindowEx (WS_EX_CLIENTEDGE | WS_EX_CONTROLPARENT,
                                TEXT("Edit"), TEXT(""),
                                ebStyle_v, 
                                x_v, y_v, width_v, hight_v,
                                prntWnd_p->BsolGetWndHandle(), 
                                (HMENU)this->ebClass.ctrlId, 
                                prntWnd_p->BsolGetInstance(), 0);


The creation of the buttons is as below:

1
2
3
4
5
6
7
8
9
10
11
12
bStyle_v = BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP;

// Button with Id 1 will be the beginning of new group
if (this->BtnClass.ctrlId == 1)
    bStyle_v = bStyle_v | WS_GROUP;

this->Btn = CreateWindowEx (WS_EX_WINDOWEDGE, TEXT("Button"), text_v, 
                            bStyle_v, 
                            X_v, Y_v, width_v, hight_v, 
                            prntWnd_p->BsolGetWndHandle(), 
                            (HMENU)this->BtnClass.ctrlId, 
                            prntWnd_p->BsolGetInstance(), 0);


Please let me know what is missing in my code.
Regards,
Ahmad
So you are creating an overlapped window and you want it to behave like a control window?? What is it that you want to accomplish here, exactly?
And you do know you need an IsDialogMessage() call within your message pump if you want auto tab working right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
 char szClassName[]="Form3";
 WNDCLASSEX wc;
 MSG messages;
 HWND hWnd;

 AttachEventHandlers();
 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=CS_DBLCLKS;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);     wc.hInstance=hInstance;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BTNSHADOW;    wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 hWnd=CreateWindowEx(0,szClassName,szClassName,WS_OVERLAPPEDWINDOW,100,400,410,250,HWND_DESKTOP,0,hInstance,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {     //Is it not a Dialog Message?
    if(!IsDialogMessage(hWnd,&messages)) //Added this because it does special character code translations that
    {                                    //allows the use of the tab key to move focus among child window
       TranslateMessage(&messages);      //controls with the WS_TABSTOP style on the main Form.
       DispatchMessage(&messages);
    }
 }

 return messages.wParam;
}
Last edited on
webjose: I just want to have a window containing those items. Actually i don't know what window style to use. so if you can propose the correct window style, I would be thankful :) Beside to this, do I have anything missing in the style of the control items??

Freddie, I tried your suggestion and it gave good results but I'm not able any more to find neither WM_NCLBUTTONDOWN nor WM_KEYDOWN messages are issued.
In addition to this, I noticed that when I use WS_CHILD or WS_CHILDWINDOW, I'm not able to set focus by clicking on the edit while I can click on the buttons and set the focus to the button normally. So do you have any clue about this?

Regards,
Ahmad
Well, I'm really not a Windows-GUI-in-C++ kind of guy, so I am not 100% sure of the styles. I do think, however, that you should only use WS_CHILD | WS_VISIBLE | WS_TABSTOP to create a child control, and then add borders and such with extended styles. The other window styles you specify are normally seen in top level windows only.
If I recall correctly, when you utilize the WS_TABSTOP child window style in conjunction with the IsDialogMessage() Api Function in your message pump to enable automatic tabbing between your controls, you will lose various messages from the parent Window Procedure such as WM_KEYDOWN. I believe that is what you are seeing. If you need those messages that are 'eaten' by IsDialogMessage(), then you simply can't utilize the WS_TABSTOP / IsDialogMessage() technique of allowing automatic keyboard navigation. You need to do it the hard way. And that is by hooking the internal Window Procedures for your child window controls through window subclassing.

I really feel bad for you ahbazzi. Yet there is nothing I can do for you. You have decided to tackle Windows Api coding and you have made everything ten times more complicated for yourself by insisting on wrapping everything up in C++ classes. So not only do you have the inherent complexity of the low level C based Windows System and documentation to deal with, but you are having to figure out at the same time how to deal with it in terms of the class framework you have decided to construct on top of it. So you are really struggling with two difficult things at the same time. Number one is the Windows Api itself, and number two is how to wrap it with class wrappers. I know for certain that approach would be too much for me.
1st I would like to say Happy new year for all.

Thanks guys for your help and thank you Freddie for feeling bad for me since i'm struggling with two difficult things as you stated. However, I believe that I can handle those issues specially that what I want is not that difficult to be applied. I think that I have completed half of the way in constructing my Form template but still I have a lot to do.

Anyway, I wish if you can help in setting the guide lines for me so I can implement the tabbing functionality correctly and I'll be thankful if you give me any clue before the end of this year :D. I can not use IsDialogMessage() call although it was very beneficial, because, as I stated before, it is eating some window messages that I'll have to use later on in my coding.

Besides to this, do you have any clue on the fact that when I use WS_CHILD or WS_CHILDWINDOW in the style of the Form window, I'm not able to set focus to the edit box by clicking on it. Moreover, when I tried pumping WM_SETFOCUS to the edit box, the cursor was set into that edit but still I was not able to type anything. I searched for several tutorials but none of them is setting the window to WS_CHILDWINDOW and none of them is handling the tabbing functionality.

I'll be waiting your feedback the soonest (hopefully in 2011 :))
Best Regards

Ahmad

Besides to this, do you have any clue on the fact that when I use WS_CHILD or WS_CHILDWINDOW in the style of the Form window, I'm not able to set focus to the edit box by clicking on it.


By 'Form window' do you mean a top level window, that is, your main frame window? If so, why would you want to set the WS_CHILD style to that window? By definition, its not a child window, but your top level window. The most applicable style for that window would be WS_OVERLAPPEDWINDOW. To directly answer your question (and assumming that is what you are doing), I'd have to put together an application to check it out and see what happens, as I've never set the WS_CHILD style to a top level window. Also, there is some interaction there with the hParent parameter of the CreateWindowEx() call. These text boxes that won't receive focus even when you click them, what was their parent set to when you created them? This could affect things. The behavior you are describing is quite dysfunctional, and my guess is that it is being caused by incorrectly setting window styles or hParent, or both.

In your learning so far, have you acquired any books or studied any tutorials? These would show the correct way to do these things.



To add to the conversation, you cannot move the keyboard focus by simply forcing a WM_SETFOCUS message down a window's window procedure. This message is really a notification ONLY, as you can clearly see it filed under Keyboard Input Notifications in MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms646283(v=vs.85).aspx ).

If you want to forcefully move the keyboard input, then you must use the SetFocus() function.
I've taken another look at the original styles you posted Ahmed and I have to say I don't do things like that. For my top level windows usually WS_OVERLAPPEDWINDOW is all I use. If I don't want a maximize button sometimes I'll xor that out.

For buttons I just use WS_CHILD | WS_VISIBLE. Edit controls are a bit more complex, depending on multiline vs single line, with or without scroll bars.

I believe one of my tutorials gives many examples of multiple forms with multiple child window controls. I believe its #40. Basically, the first examples start out with windows automatic tabbing with the WS_TABSTOP style, then show how to do it with subclassing. I think for most apps automatic tabbing through Windows facilities works best, but for that 'special touch' for specialized apps going the subclassing route is good. Its a good bit more work though.

So now WebJose has just told you the same thing I did in some other post of yours concerning SetFocus(). You don't usually want to be sending WM_KILLFOCUS/WM_SETFOCUS messages.
Topic archived. No new replies allowed.