Tab key issue in Tab Control in Win32 SDK



The original post is present at
http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/9b170b5f-c4b9-4792-9205-2791aa2ab288/

I have got no solution so have repeated the post contents here. Would
appreciate if someone can help me out on this issue.

I am making simple tab control in visual studio 2005 express edition.

I do the following steps:-

1. I have a main dialog box made using resource editor with OK and Cancel
button.
2. On this i have made a tab contol by dragging it to the dialog box shape.
3. The tab control is used to display some options.
4. Inorder to display the different options i create a dialog box with
following parameters which will be child to the tab control
1. Border - None
2. Style - child
3. Title bar - false
5. I use this dialog box to display on the tab control using
CreateDialogParam() function.
6. The child dialog (in step 4) has some edit contols to have user input.
7. I want to ensure that user can use the TAB key to move between the
edit controls on the tab control and the OK and Cancel button of the main
dialog.
8. If i press the TAB key the control only switches between the OK and
Cancel button.

I have attached the full code including the resource file code. I hope some
one will help me troubleshoot the issue.

// TabControl_Win32.h code

#pragma once

#pragma comment(linker,"/manifestdependency:\"type='win32'
name='Microsoft.Windows.Common-Controls' version='6.0.0.0'
processorArchitecture='x86' publicKeyToken='6595b64144ccf1df'
language='*'\"")
#pragma comment(lib,"comctl32.lib")

#include <windows.h>
#include <commctrl.h>

INT_PTR CALLBACK MainDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam);
BOOL CenterWindow(HWND hwnd);
void TabControlSetup(HWND hTabControl);
INT_PTR CALLBACK TabDialogOne(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM
lParam);
INT_PTR CALLBACK TabDialogTwo(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM
lParam);



// TabControl_Win32.cpp code


#include "TabControl_Win32.h"
#include "resource.h"

HINSTANCE hInstance =NULL;
HWND hMainDialog = NULL;
HWND hTabMainControl=NULL;
HWND hTabControlList[10];


//entry point
int WINAPI WinMain (HINSTANCE hInstan, HINSTANCE hPrevInstance, PSTR
szCmdLine, int iCmdShow)
{

MSG msg ;

InitCommonControls();
hInstanhInstance = hInstan;
//create dialog box
hMainDialog = CreateDialog(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),
NULL, (DLGPROC)MainDlgProc);

if (hMainDialog == NULL)
{
// Notified your about the failure
MessageBox(NULL, TEXT("Application failed to load!"),
TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
// Set the return value
return FALSE;
}
else
{
ShowWindow (hMainDialog, SW_SHOW) ;
UpdateWindow(hMainDialog);
}

while (GetMessage (&msg, NULL, 0, 0))
{
if (!IsDialogMessage(hMainDialog, &msg))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return (int)msg.wParam ;
}


INT_PTR CALLBACK MainDlgProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
switch (message)
{
case WM_INITDIALOG:
hMainDialog = hwnd;
SetWindowText(hwnd,TEXT("Tab Control"));
CenterWindow(hwnd);
hTabMainControl = GetDlgItem(hwnd,IDC_TAB1);
TabControlSetup(hTabMainControl);
break;

case WM_NOTIFY:
{
NMHDR *nmhdr = NULL;
nmhdr = (NMHDR*)lParam;

if(hTabMainControl == nmhdr->hwndFrom)
{
switch(nmhdr->code)
{
case TCN_SELCHANGE:
{
switch(SendMessage(nmhdr->hwndFrom,(UINT)
TCM_GETCURFOCUS,0,0))
{
case 0:
{
ShowWindow (hTabControlList[1] , SW_HIDE) ;
ShowWindow (hTabControlList[0] , SW_SHOW) ;
UpdateWindow(hTabControlList[0] );
}
break;
case 1:
{
ShowWindow (hTabControlList[0] , SW_HIDE) ;
ShowWindow (hTabControlList[1] , SW_SHOW) ;
UpdateWindow(hTabControlList[1] );
}
break;
}

}
break;
}
}
}
break;

case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
break;
case IDCANCEL:
SendMessage(hwnd,WM_CLOSE,0,0);
break;

}
break;
}

case WM_CLOSE:
DestroyWindow(hTabControlList[0]);
DestroyWindow(hTabControlList[1]);
DestroyWindow(hwnd);
break;

case WM_DESTROY:
PostQuitMessage (0) ;
break;

}
return FALSE ;
}

void TabControlSetup(HWND hTabControl)
{
LRESULT lResult = 0;
TC_ITEM tcItem = {0};
RECT tabControlRect = {0};

tcItem.mask = TCIF_STATE|TCIF_TEXT;

tcItem.pszText = TEXT("Tab Control 1");
lResult = SendMessage(hTabControl,(UINT) TCM_INSERTITEM,0,(LPARAM)
&tcItem );
if(-1==lResult)
{
MessageBox(hTabControl,TEXT("TabControl Insert
failed"),TEXT("Error"),0);
return;
}

hTabControlList[0] =
CreateDialogParam(hInstance,MAKEINTRESOURCE(IDD_TAB_PAGE_1), hTabControl,
(DLGPROC)TabDialogOne,0);
if (hTabControlList[0] == NULL)
{
// Notified your about the failure
MessageBox(NULL, TEXT("IDD_TAB_PAGE_1 failed to load!"),
TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
}
else
{
ShowWindow (hTabControlList[0] , SW_HIDE) ;
UpdateWindow(hTabControlList[0] );
}


tcItem.pszText = TEXT("Tab Control 2");
lResult = SendMessage(hTabControl,(UINT) TCM_INSERTITEM,1,(LPARAM)
&tcItem );
if(-1==lResult)
{
MessageBox(hTabControl,TEXT("TabControl Insert
failed"),TEXT("Error"),0);
return;
}

hTabControlList[1] =
CreateDialogParam(hInstance,MAKEINTRESOURCE(IDD_TAB_PAGE_2), hTabControl,
(DLGPROC)TabDialogTwo,0);
if (hTabControlList[1] == NULL)
{
// Notified your about the failure
MessageBox(NULL, TEXT("IDD_TAB_PAGE_2 failed to load!"),
TEXT("Error"), MB_OK | MB_ICONEXCLAMATION);
}
else
{
ShowWindow (hTabControlList[1] , SW_HIDE) ;
UpdateWindow(hTabControlList[1] );
}


lResult = SendMessage(hTabControl,(UINT) TCM_SETCURSEL,0,0);
if(-1==lResult)
{
MessageBox(hTabControl,TEXT("TabControl Selection
failed"),TEXT("Error"),0);
return;
}
else
{
ShowWindow (hTabControlList[0] , SW_SHOW) ;
UpdateWindow(hTabControlList[0] );
}


}


INT_PTR CALLBACK TabDialogOne(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM
lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
break;

case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_TAB1_BUTTON1:
MessageBox(hwndDlg,TEXT("tab 1"),TEXT("Button"),0);
break;
}
break;
}
}
return FALSE ;


}


INT_PTR CALLBACK TabDialogTwo(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM
lParam)
{

switch (uMsg)
{
case WM_INITDIALOG:
break;

case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_TAB2_BUTTON1:
MessageBox(hwndDlg,TEXT("tab 2"),TEXT("Button"),0);
break;
}
break;
}

}
return FALSE ;


}

BOOL CenterWindow(HWND hwnd)
{
HWND hwndParent;
RECT rect, rectP;
int width, height;
int screenwidth, screenheight;
int x, y;

//make the window relative to its desktop
hwndParent = GetDesktopWindow();

GetWindowRect(hwnd, &rect);
GetWindowRect(hwndParent, &rectP);

width = rect.right - rect.left;
height = rect.bottom - rect.top;

x = ((rectP.right-rectP.left) - width) / 2 + rectP.left;
y = ((rectP.bottom-rectP.top) - height) / 2 + rectP.top;

screenwidth = GetSystemMetrics(SM_CXSCREEN);
screenheight = GetSystemMetrics(SM_CYSCREEN);

//make sure that the dialog box never moves outside of
//the screen
if(x < 0) x = 0;
if(y < 0) y = 0;
if(x + width > screenwidth) x = screenwidth - width;
if(y + height > screenheight) y = screenheight - height;

MoveWindow(hwnd, x, y, width, height, FALSE);
SetActiveWindow(hwnd);

return TRUE;
}

// resource.h code

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDD_DIALOG1 100
#define IDD_TAB_PAGE_2 103
#define IDC_EDIT1 1000
#define IDC_TAB1 1000
#define IDC_TAB1_BUTTON1 1000
#define IDC_TAB2_BUTTON1 1001
#define IDC_EDIT2 1002
#define IDC_EDIT3 1003
#define IDD_TAB_PAGE_1 1033



// TabControl.rc code

// Generated by ResEdit 1.4.3
// Copyright (C) 2006-2008
// http://www.resedit.net

#include "resource.h"
#include <windows.h>
#include <commctrl.h>
#include <richedit.h>


//
// Dialog resources
//
LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
IDD_DIALOG1 DIALOGEX 0, 0, 273, 239
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_VISIBLE |
WS_BORDER | WS_CAPTION | WS_DLGFRAME | WS_POPUP | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "Tahoma", 400, 0, 0
BEGIN
DEFPUSHBUTTON "OK", IDOK, 161, 217, 50, 14
PUSHBUTTON "Cancel", IDCANCEL, 216, 217, 50, 14
CONTROL "", IDC_TAB1, WC_TABCONTROL, 0, 2, 5, 264, 210
END

LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
IDD_TAB_PAGE_2 DIALOGEX 1, 13, 260, 195
STYLE DS_3DLOOK | DS_SHELLFONT | WS_VISIBLE | WS_CHILDWINDOW
FONT 8, "Tahoma", 400, 0, 0
BEGIN
GROUPBOX "Tab 2 page", IDC_STATIC, 15, 26, 216, 151
PUSHBUTTON "Tab 2", IDC_TAB2_BUTTON1, 145, 143, 63, 19
EDITTEXT IDC_EDIT1, 53, 45, 128, 14, ES_AUTOHSCROLL
EDITTEXT IDC_EDIT2, 53, 76, 128, 14, ES_AUTOHSCROLL
EDITTEXT IDC_EDIT3, 53, 109, 128, 14, ES_AUTOHSCROLL
END

LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT
IDD_TAB_PAGE_1 DIALOGEX 1, 13, 260, 195
STYLE DS_3DLOOK | DS_SHELLFONT | WS_VISIBLE | WS_CHILDWINDOW
FONT 8, "Tahoma", 400, 0, 0
BEGIN
GROUPBOX "Tab 1 page", IDC_STATIC, 21, 30, 182, 116
PUSHBUTTON "Tab 1", IDC_TAB1_BUTTON1, 77, 74, 59, 27
END

.