How to get ID number of focused window?

I created a variable and 100 text edit windows like such:

1
2
3
4
int focus_id;

for(int n = 0; n < 100; n++)
numbox[n] = CreateWindow("EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS | ES_READONLY, 0, 43 + n*ENTRYH, 100, 22, mainwindow[n], (HMENU)(100 + n), inst, NULL);


Now I want to set the value of focus_id to the 'id number' of the focused window whenever there's a mouseclick inside one of the windows.
So when I click inside numbox[32], focus_id should be 32.

What would be the most straightforward method to check for this 'id number'? I thought the hMenu specification in CreateWindow could help me, but couldn't figure out how.

GetFocus() returns a HWND type, so I guess this is useless here.
Use GetDlgCtrlID() to retrieve de hMenu identifier passed to CreateWindowEx and do the calculation yourself (substracting 100 in this exaample).
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645478(v=vs.85).aspx


Alernatively, you can do GetWindowLongPtr (hwnd_edit, GWLP_ID);
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633585(v=vs.85).aspx


Use the handle returned by GetFocus as HWND parameter.
Last edited on
Well, when you click on one of your Edit controls it will handle the WM_LBUTTONDOWN message itself. So if you need to do custom handling of this message you will need to use subclassing.

But when you click on an Edit control it will be given the keyboard focuses which causes it to send its parent window a WM_COMMAND message with the EN_SETFOCUS notification code, plus its control ID and HWND.

So this sort of thing might work in your case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        case WM_COMMAND:
        {
            // get command ID and notification code
            int id   = LOWORD(wParam);
            int code = HIWORD(wParam);

            if((start_id <= id) && (id < (start_id + ctrl_count)))
            {
                if(code == EN_SETFOCUS)
                {
                    focus_id = (id - start_id);
                    return 0;
                }
            }
        }
        break;


(where the meaning of the variables start_id and ctrl_count is hopefully clear enough?)

Andy
Last edited on
Thank you!

But the real problem is that I don't know when to call this. It has to happen exactly when focus is changed to another window. I thought the message WM_SETFOCUS is called whenever I click inside one of the text edit windows, but it turns out it's only called once, at the program's startup.

I use GetMessage like this:

1
2
3
while(GetMessage(&msg, NULL, 0, 0)){
	TranslateMessage(&msg);
	DispatchMessage(&msg);}


Messages are received like this:

1
2
3
4
5
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch(message){
		case WM_SETFOCUS:

....


Any idea on this?
You will receive EN_SETFOCUS every time a edit control has the focus in WM_COMMAND handler. You don't need to process WM_SETFOCUS:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb761685%28v=vs.85%29.aspx


andywestken has already posted a sample code.
Oops, I don't know why, but I didn't see Andy's post before I wrote my previous post. All is clear with Andy's example.

Thanks to both of you!
Topic archived. No new replies allowed.