Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Petzold's win32API book and the Checker4.c prog

Slogging my way through Petzold's Programming Windows 5th edition.

I'm really stumped on the Checker4.c program. How does
SetFocus(GetDlgItem (hwnd, idFocus)) get a value for idFocus? As far as I
can see idFocus is initialized to 0 and retains this value up to that
call. GetDlgItem requires it's second parameter to be the Child ID.
How does idFocus suddenly become that value?

The relevant code goes like this:

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndChild[DIVISIONS][DIVISIONS] ;
int cxBlock, cyBlock, x, y ;

switch (message)
{
case WM_CREATE :
for (x = 0 ; x < DIVISIONS ; x++)
for (y = 0 ; y < DIVISIONS ; y++)
hwndChild[x][y] = CreateWindow (szChildClass, NULL,
WS_CHILDWINDOW | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) (y << 8 | x),
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
return 0 ;

case WM_SIZE :
cxBlock = LOWORD (lParam) / DIVISIONS ;
cyBlock = HIWORD (lParam) / DIVISIONS ;

for (x = 0 ; x < DIVISIONS ; x++)
for (y = 0 ; y < DIVISIONS ; y++)
MoveWindow (hwndChild[x][y],
x * cxBlock, y * cyBlock,
cxBlock, cyBlock, TRUE) ;
return 0 ;

case WM_LBUTTONDOWN :
MessageBeep (0) ;
return 0 ;

// On set-focus message, set focus to child window

case WM_SETFOCUS:
SetFocus (GetDlgItem (hwnd, idFocus)) ;
return 0 ;

You can find the full program at the bottom of this page:

http://it-reading.org/ebook/win32 api/content/ch07e.htm

TIA.

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]If you look closely at the link you provided you will see this immediately before the line with SetFocus:[/color]
    [code]idFocus = y << 8 | x ;[/code]
  • PetzoldNovicePetzoldNovice Member Posts: 2
    : [color=Blue]If you look closely at the link you provided you will
    : see this immediately before the line with SetFocus:[/color]
    : [code]: idFocus = y << 8 | x ;[/code]:

    That code actually comes later though.

    First the program gives this:

    case WM_SETFOCUS:
    SetFocus (GetDlgItem (hwnd, idFocus));
    return 0;

    // On key-down message, "possibly change" the focus window (quotes mine)

    case WM_KEYDOWN: ...

    it then goes into the code section you refer to. Now, if you are saying that the program would have to go into the key-down case before it ever went into the setfocus case, then I would understand. But that is not the impression I get with this program.

    Thanks for responding.
  • zibadianzibadian Member Posts: 6,349
    : : [color=Blue]If you look closely at the link you provided you will
    : : see this immediately before the line with SetFocus:[/color]
    : : [code]: : idFocus = y << 8 | x ;[/code]: :
    :
    : That code actually comes later though.
    :
    : First the program gives this:
    :
    : case WM_SETFOCUS:
    : SetFocus (GetDlgItem (hwnd, idFocus));
    : return 0;
    :
    : // On key-down message, "possibly change" the focus window
    : (quotes mine)
    :
    : case WM_KEYDOWN: ...
    :
    : it then goes into the code section you refer to. Now, if you are
    : saying that the program would have to go into the key-down case
    : before it ever went into the setfocus case, then I would understand.
    : But that is not the impression I get with this program.
    :
    : Thanks for responding.
    :
    If you read the description correctly, then the child to receive the focus is set by pressing a key (creating a WM_KEYDOWN event). If the program receives its first focus, the first child (idFocus = 0) is focused.
    Remember that events can come in any sequence. Thus WM_SETFOCUS and WM_KEYDOWN can be interchanged in any order depending on the user's actions.
Sign In or Register to comment.