このセクションの例は、アプリケーションのメイン ウィンドウのクライアント領域にタブ コントロールを作成して表示する方法を示しています。 アプリケーションは、タブ コントロールの表示領域に 3 番目のウィンドウ (静的コントロール) を表示します。 親ウィンドウは、WM_SIZE メッセージを処理するときに、タブ コントロールと静的コントロールを配置してサイズ設定します。
この例では、曜日ごとに 1 つずつ、合計 7 つのタブがあります。 ユーザーがタブを選択すると、アプリケーションは、対応する曜日の名前を静的コントロールに表示します。
知っておくべきこと
テクノロジ
前提条件
- C/C++
- Windows ユーザー インターフェイス プログラミング
手順
メイン ウィンドウにタブ コントロールを作成する
次の関数は、タブ コントロールを作成し、曜日ごとに 1 つのタブを追加します。 曜日の名前は文字列リソースとして定義され、IDS_SUNDAY で始まる連続する番号が付けられています (アプリケーションのリソース ヘッダー ファイルで定義されます)。 親ウィンドウとタブ コントロールの両方に、WS_CLIPSIBLINGS ウィンドウ スタイルが設定されている必要があります。 アプリケーションの初期化関数が、メイン ウィンドウの作成後に、この関数を呼び出します。
#define DAYS_IN_WEEK 7
// Creates a tab control, sized to fit the specified parent window's client
// area, and adds some tabs.
// Returns the handle to the tab control.
// hwndParent - parent window (the application's main window).
//
HWND DoCreateTabControl(HWND hwndParent)
{
RECT rcClient;
INITCOMMONCONTROLSEX icex;
HWND hwndTab;
TCITEM tie;
int i;
TCHAR achTemp[256]; // Temporary buffer for strings.
// Initialize common controls.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_TAB_CLASSES;
InitCommonControlsEx(&icex);
// Get the dimensions of the parent window's client area, and
// create a tab control child window of that size. Note that g_hInst
// is the global instance handle.
GetClientRect(hwndParent, &rcClient);
hwndTab = CreateWindow(WC_TABCONTROL, L"",
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, rcClient.right, rcClient.bottom,
hwndParent, NULL, g_hInst, NULL);
if (hwndTab == NULL)
{
return NULL;
}
// Add tabs for each day of the week.
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
tie.pszText = achTemp;
for (i = 0; i < DAYS_IN_WEEK; i++)
{
// Load the day string from the string resources. Note that
// g_hInst is the global instance handle.
LoadString(g_hInst, IDS_SUNDAY + i,
achTemp, sizeof(achTemp) / sizeof(achTemp[0]));
if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1)
{
DestroyWindow(hwndTab);
return NULL;
}
}
return hwndTab;
}
次の関数は、タブ コントロールの表示領域に配置される静的コントロールを作成します。 アプリケーションの初期化関数が、メイン ウィンドウとタブ コントロールの作成後に、この関数を呼び出します。
スタティック コントロールはタブ コントロールの表示領域に配置されますが、それ自体はタブ コントロールの兄弟であり、子ではありません。 これにより、スタティック コントロールが共有親ウィンドウのタブ オーダーに参加できるようになります。 これはスタティック コントロールでは重要ではありませんが、ボタンのようなキーボードでアクセス可能なコントロールに置き換える場合に適しています。
// Creates a child window (a static control) to occupy the tab control's
// display area.
// Returns the handle to the static control.
// hwndTab - handle of the tab control.
//
HWND DoCreateDisplayWindow(HWND hwndTab)
{
HWND hwndStatic = CreateWindow(WC_STATIC, L"",
WS_CHILD | WS_VISIBLE | WS_BORDER,
100, 100, 100, 100, // Position and dimensions; example only.
GetParent(hwndTab), NULL, g_hInst, // g_hInst is the global instance handle
NULL);
return hwndStatic;
}
以下に例示する関数は、アプリケーションのウィンドウ プロシージャから呼び出されます。 アプリケーションは、WM_SIZE メッセージを処理するときに OnSize 関数を呼び出して、メイン ウィンドウのクライアント領域に合うようにタブ コントロールを配置してサイズ設定します。
タブが選択されると、タブ コントロールは、TCN_SELCHANGE 通知コードを指定して WM_NOTIFY メッセージを送信します。 アプリケーションの OnNotify 関数は、この静的コントロールのテキストを設定することにより、この通知コードを処理します。
// Handles the WM_SIZE message for the main window by resizing the
// tab control.
// hwndTab - handle of the tab control.
// lParam - the lParam parameter of the WM_SIZE message.
//
HRESULT OnSize(HWND hwndTab, LPARAM lParam)
{
RECT rc;
if (hwndTab == NULL)
return E_INVALIDARG;
// Resize the tab control to fit the client are of main window.
if (!SetWindowPos(hwndTab, HWND_TOP, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), SWP_SHOWWINDOW))
return E_FAIL;
return S_OK;
}
// Handles notifications from the tab control, as follows:
// TCN_SELCHANGING - always returns FALSE to allow the user to select a
// different tab.
// TCN_SELCHANGE - loads a string resource and displays it in a static
// control on the selected tab.
// hwndTab - handle of the tab control.
// hwndDisplay - handle of the static control.
// lParam - the lParam parameter of the WM_NOTIFY message.
//
BOOL OnNotify(HWND hwndTab, HWND hwndDisplay, LPARAM lParam)
{
TCHAR achTemp[256]; // temporary buffer for strings
switch (((LPNMHDR)lParam)->code)
{
case TCN_SELCHANGING:
{
// Return FALSE to allow the selection to change.
return FALSE;
}
case TCN_SELCHANGE:
{
int iPage = TabCtrl_GetCurSel(hwndTab);
// Note that g_hInst is the global instance handle.
LoadString(g_hInst, IDS_SUNDAY + iPage, achTemp,
sizeof(achTemp) / sizeof(achTemp[0]));
LRESULT result = SendMessage(hwndDisplay, WM_SETTEXT, 0,
(LPARAM) achTemp);
break;
}
}
return TRUE;
}
関連トピック