На главную

On-line справка по Win32 API

Написать письмо
БЕСПЛАТНАЯ ежедневная online лотерея! Выигрывай каждый день БЕСПЛАТНО!
Список всех статей A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z | Скачать Вниз

Creating a Tabbed Dialog Box



The example in this section creates a dialog box that uses tabs to provide multiple pages of controls. The main dialog box is a modal dialog box. Each page of controls is defined by a dialog box template that specifies the WS_CHILD style. When a tab is selected, the example creates a modeless dialog box for the incoming page and destroys the dialog box for the outgoing page.



Note In many cases, you can implement multiple-page dialog boxes more easily by using property sheets. For more information about property sheets, see Property Sheets.



The template for the main dialog box simply defines two button controls. When processing the WM_INITDIALOG message, the dialog box procedure creates a tab control and loads the dialog template resources for each of the child dialog boxes.
The information is saved in an application-defined structure called DLGHDR. A pointer to this structure is associated with the dialog box window by using the SetWindowLong function. The structure is defined in the application's header file, as follows.

#define C_PAGES 3

typedef struct tag_dlghdr {
HWND hwndTab; // tab control
HWND hwndDisplay; // current child dialog box
RECT rcDisplay; // display rectangle for the tab control
DLGTEMPLATE *apRes[C_PAGES];
} DLGHDR;


The following function processes the WM_INITDIALOG message for the main dialog box. The function allocates the DLGHDR structure, loads the dialog template resources for the child dialog boxes, and creates the tab control.
The size of each child dialog box is specified by the DLGTEMPLATE structure. The function examines the size of each dialog box and uses the macro for the TCM_ADJUSTRECT message to calculate an appropriate size for the tab control. Then it sizes the dialog box and positions the two buttons accordingly. This example sends TCM_ADJUSTRECT by using the TabCtrl_AdjustRect macro.

VOID WINAPI OnTabbedDialogInit(HWND hwndDlg)
{
DLGHDR *pHdr = (DLGHDR *) LocalAlloc(LPTR, sizeof(DLGHDR));
DWORD dwDlgBase = GetDialogBaseUnits();
int cxMargin = LOWORD(dwDlgBase) / 4;
int cyMargin = HIWORD(dwDlgBase) / 8;
TC_ITEM tie;
RECT rcTab;
HWND hwndButton;
RECT rcButton;
int i;

// Save a pointer to the DLGHDR structure.
SetWindowLong(hwndDlg, GWL_USERDATA, (LONG) pHdr);

// Create the tab control.

InitCommonControls();
pHdr->hwndTab = CreateWindow(
WC_TABCONTROL, "",
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, 100, 100,
hwndDlg, NULL, g_hinst, NULL
);
if (pHdr->hwndTab == NULL) {
// handle error
}

// Add a tab for each of the three child dialog boxes.
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
tie.pszText = "First";
TabCtrl_InsertItem(pHdr->hwndTab, 0, &tie);

tie.pszText = "Second";
TabCtrl_InsertItem(pHdr->hwndTab, 1, &tie);
tie.pszText = "Third";
TabCtrl_InsertItem(pHdr->hwndTab, 2, &tie);

// Lock the resources for the three child dialog boxes.
pHdr->apRes[0] = DoLockDlgRes(MAKEINTRESOURCE(DLG_FIRST));
pHdr->apRes[1] = DoLockDlgRes(MAKEINTRESOURCE(DLG_SECOND));
pHdr->apRes[2] = DoLockDlgRes(MAKEINTRESOURCE(DLG_THIRD));

// Determine the bounding rectangle for all child dialog boxes.

SetRectEmpty(&rcTab);
for (i = 0; i < C_PAGES; i++) {
if (pHdr->apRes[i]->cx > rcTab.right)
rcTab.right = pHdr->apRes[i]->cx;
if (pHdr->apRes[i]->cy > rcTab.bottom)
rcTab.bottom = pHdr->apRes[i]->cy;
}
rcTab.right = rcTab.right * LOWORD(dwDlgBase) / 4;
rcTab.bottom = rcTab.bottom * HIWORD(dwDlgBase) / 8;

// Calculate how large to make the tab control, so
// the display area can accomodate all the child dialog boxes.

TabCtrl_AdjustRect(pHdr->hwndTab, TRUE, &rcTab);
OffsetRect(&rcTab, cxMargin - rcTab.left,
cyMargin - rcTab.top);

// Calculate the display rectangle.
CopyRect(&pHdr->rcDisplay, &rcTab);
TabCtrl_AdjustRect(pHdr->hwndTab, FALSE, &pHdr->rcDisplay);

// Set the size and position of the tab control, buttons,
// and dialog box.
SetWindowPos(pHdr->hwndTab, NULL, rcTab.left, rcTab.top,
rcTab.right - rcTab.left, rcTab.bottom - rcTab.top,

SWP_NOZORDER);

// Move the first button below the tab control.
hwndButton = GetDlgItem(hwndDlg, BTN_CLOSE);
SetWindowPos(hwndButton, NULL,
rcTab.left, rcTab.bottom + cyMargin, 0, 0,
SWP_NOSIZE | SWP_NOZORDER);

// Determine the size of the button.
GetWindowRect(hwndButton, &rcButton);
rcButton.right -= rcButton.left;
rcButton.bottom -= rcButton.top;

// Move the second button to the right of the first.

hwndButton = GetDlgItem(hwndDlg, BTN_TEST);
SetWindowPos(hwndButton, NULL,
rcTab.left + rcButton.right + cxMargin,
rcTab.bottom + cyMargin, 0, 0,
SWP_NOSIZE | SWP_NOZORDER);

// Size the dialog box.
SetWindowPos(hwndDlg, NULL, 0, 0,
rcTab.right + cyMargin +
2 * GetSystemMetrics(SM_CXDLGFRAME),
rcTab.bottom + rcButton.bottom + 2 * cyMargin +
2 * GetSystemMetrics(SM_CYDLGFRAME) +

GetSystemMetrics(SM_CYCAPTION),
SWP_NOMOVE | SWP_NOZORDER);

// Simulate selection of the first item.
OnSelChanged(hwndDlg);
}

// DoLockDlgRes - loads and locks a dialog template resource.
// Returns a pointer to the locked resource.
// lpszResName - name of the resource

DLGTEMPLATE * WINAPI DoLockDlgRes(LPCSTR lpszResName)
{
HRSRC hrsrc = FindResource(NULL, lpszResName, RT_DIALOG);
HGLOBAL hglb = LoadResource(g_hinst, hrsrc);

return (DLGTEMPLATE *) LockResource(hglb);
}


The following function processes the TCN_SELCHANGE notification message for the main dialog box. The function destroys the dialog box for the outgoing page, if any. Then it uses the CreateDialogIndirect function to create a modeless dialog box for the incoming page.

// OnSelChanged - processes the TCN_SELCHANGE notification.
// hwndDlg - handle of the parent dialog box

VOID WINAPI OnSelChanged(HWND hwndDlg)
{
DLGHDR *pHdr = (DLGHDR *) GetWindowLong(
hwndDlg, GWL_USERDATA);
int iSel = TabCtrl_GetCurSel(pHdr->hwndTab);

// Destroy the current child dialog box, if any.
if (pHdr->hwndDisplay != NULL)
DestroyWindow(pHdr->hwndDisplay);

// Create the new child dialog box.
pHdr->hwndDisplay = CreateDialogIndirect(g_hinst,

pHdr->apRes[iSel], hwndDlg, ChildDialogProc);
}


The following function processes the WM_INITDIALOG message for each of the child dialog boxes. You cannot specify the position of a dialog box created using the CreateDialogIndirect function. This function uses the SetWindowPos function to position the child dialog within the tab control's display area.

// OnChildDialogInit - Positions the child dialog box to fall
// within the display area of the tab control.

VOID WINAPI OnChildDialogInit(HWND hwndDlg)
{
HWND hwndParent = GetParent(hwndDlg);
DLGHDR *pHdr = (DLGHDR *) GetWindowLong(
hwndParent, GWL_USERDATA);
SetWindowPos(hwndDlg, HWND_TOP,
pHdr->rcDisplay.left, pHdr->rcDisplay.top,
0, 0, SWP_NOSIZE);
}



Пригласи друзей и счет твоего мобильника всегда будет положительным!
Предыдущая статья
 
Сайт Народ.Ру Интернет
Следующая статья
Пригласи друзей и счет твоего мобильника всегда будет положительным!

Создание Табулированного Диалогового Ящика



Пример в этой секции создает диалоговый ящик, который использует таб., чтобы обеспечивать многочисленные страницы элементов управления. Основной диалоговый ящик является модальным диалоговым ящиком. Каждая страница элементов управления определена диалоговым шаблоном ящика, который определяет стиль WS_CHILD. Когда таб. выбрано, пример создает независимый диалоговый ящик для поступающей страницы и уничтожает диалоговый ящик для исходящей страницы.



Примечание Во многих случаях, Вы можете осуществить многочисленные-страничные диалоговые ящики более легко используя листы собственности. Более подробно о листах собственности, смотри Листы Собственности.



Шаблон для основного диалогового ящика просто определяет два элемента управления кнопки. При обработке сообщения WM_INITDIALOG, диалоговая процедура ящика создает управление таб. и загружает диалоговые ресурсы шаблона для каждых диалоговых ящиков ребенка.
Информация сохранена в определенной прикладной структуре вызвавшей DLGHDR. Указатель в эту структуру связан диалоговым окном блока используя функцию SetWindowLong. Структура определена в прикладном файле заголовка, следующим образом.

#define C_PAGES 3

typedef struct tag_dlghdr { HWND hwndTab; // управление таб. HWND hwndDisplay; // диалоговый блок текущего ребенка RECT rcDisplay; // дисплейный прямоугольник для управления таб. DLGTEMPLATE *после[C_PAGES];
} DLGHDR;


Следующее функции обрабатывает сообщение WM_INITDIALOG для основного диалогового блока. Функция распределяет структуру DLGHDR, загружает диалоговые ресурсы шаблона для диалоговых блоков ребенка и создает управление таб..
Размер каждого диалогового блока ребенка определен структурой DLGTEMPLATE. Функция изучает размер каждого диалога боксировать и использует макро для сообщения TCM_ADJUSTRECT, чтобы вычислять подходящий размер для управления таб.. Затем это измеряет диалогового блока и позиционирует две кнопки соответственно. Этот пример посылает TCM_ADJUSTRECT используя макро TabCtrl_AdjustRect.

ПУСТОТА WINAPI OnTabbedDialogInit(HWND hwndDlg)
{
DLGHDR *pHdr = (DLGHDR *) LocalAlloc(LPTR, sizeof(DLGHDR));
DWORD dwDlgBase = GetDialogBaseUnits();
int cxMargin = LOWORD(dwDlgBase) / 4;
int cyMargin = HIWORD(dwDlgBase) / 8;
соединение TC_ITEM;
RECT rcTab;
HWND hwndButton;
RECT rcButton;
int i;

// Сохраните указатель в структуру DLGHDR.
SetWindowLong(hwndDlg, GWL_USERDATA, (ДОЛГО (ДЛИНОЙ)) pHdr);

// Создайте управление таб..

InitCommonControls();
pHdr->hwndTab = CreateWindow( WC_TABCONTROL, "", WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, 100, 100,
hwndDlg, НЕДЕЙСТВИТЕЛЬНЫЙ, g_hinst, НЕДЕЙСТВИТЕЛЬНЫЙ
);
если (pHdr->hwndTab == НЕДЕЙСТВИТЕЛЬНЫЙ) { // ошибка ручки
}

// Добавьте таб. для каждых трех диалоговых блоков ребенка.
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
tie.pszText = "Сначала";
TabCtrl_InsertItem(pHdr->hwndTab, 0, &соединение);

tie.pszText = "Секунда";
TabCtrl_InsertItem(pHdr->hwndTab, 1, &соединение);
tie.pszText = "Третий";
TabCtrl_InsertItem(pHdr->hwndTab, 2, &соединение);

// Блокировка ресурсы для трех диалоговых блоков ребенка.
pHdr->apRes[0] = DoLockDlgRes(MAKEINTRESOURCE(DLG_FIRST));
pHdr->apRes[1] = DoLockDlgRes(MAKEINTRESOURCE(DLG_SECOND));
pHdr->apRes[2] = DoLockDlgRes(MAKEINTRESOURCE(DLG_THIRD));

// Определите прямоугольник bounding для всех диалоговых блоков ребенка.

SetRectEmpty(&rcTab);
для (i = 0; я < C_PAGES; я++) { если (pHdr->apRes[i]->cx > rcTab.right) rcTab.right = pHdr->apRes[i]->cx;
если (pHdr->apRes[i]->cy > rcTab.bottom) rcTab.bottom = pHdr->apRes[i]->cy;
}
rcTab.right = rcTab.right * LOWORD(dwDlgBase) / 4;
rcTab.bottom = rcTab.bottom * HIWORD(dwDlgBase) / 8;

// Вычислите как большой, чтобы делать таб. управлять, так что // дисплейная область может accomodate все диалоговые блоки ребенка.

TabCtrl_AdjustRect(pHdr->hwndTab, ИСТИНА, &rcTab);
OffsetRect(&rcTab, cxMargin - rcTab.left, cyMargin - rcTab.top);

// Вычислите дисплейный прямоугольник.
CopyRect(&pHdr->rcDisplay, &rcTab);
TabCtrl_AdjustRect(pHdr->hwndTab, ЛОЖЬ, &pHdr->rcDisplay);

// Установленный размер и позиция управления таб., кнопок, // и диалоговый блок.
SetWindowPos(pHdr->hwndTab, НЕДЕЙСТВИТЕЛЬНЫЙ, rcTab.left, rcTab.top, rcTab.right - rcTab.left, rcTab.bottom - rcTab.top,

SWP_NOZORDER);

// Перемещение первая кнопка ниже управления таб..
hwndButton = GetDlgItem(hwndDlg, BTN_CLOSE);
SetWindowPos(hwndButton, НЕДЕЙСТВИТЕЛЬНЫЙ, rcTab.left, rcTab.bottom + cyMargin, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

// Определите размер кнопки.
GetWindowRect(hwndButton, &rcButton);
rcButton.right -= rcButton.left;
rcButton.bottom -= rcButton.top;

// Переместите вторую кнопку вправо от первого.

hwndButton = GetDlgItem(hwndDlg, BTN_TEST);
SetWindowPos(hwndButton, НЕДЕЙСТВИТЕЛЬНЫЙ, rcTab.left + rcButton.right + cxMargin, rcTab.bottom + cyMargin, 0, 0, SWP_NOSIZE | SWP_NOZORDER);

// Размер диалоговый блок.
SetWindowPos(hwndDlg, НЕДЕЙСТВИТЕЛЬНЫЙ, 0, 0, rcTab.right + cyMargin + 2 * GetSystemMetrics(SM_CXDLGFRAME), rcTab.bottom + rcButton.bottom + 2 * cyMargin + 2 * GetSystemMetrics(SM_CYDLGFRAME) +

GetSystemMetrics(SM_CYCAPTION), SWP_NOMOVE | SWP_NOZORDER);

// Сымитируйте выбор первого пункта.
OnSelChanged(hwndDlg);
}

// DoLockDlgRes - ЗАГРУЖАЕТ и запирает диалоговый ресурс шаблона.
// Возврат указатель в закрытый ресурс.
// lpszResName - ИМЯ ресурса

DLGTEMPLATE * WINAPI DoLockDlgRes(LPCSTR lpszResName)
{
HRSRC hrsrc = FindResource(НЕДЕЙСТВИТЕЛЬНЫЙ, lpszResName, RT_DIALOG);
HGLOBAL hglb = LoadResource(g_hinst, hrsrc);

возврат (DLGTEMPLATE *) LockResource(hglb);
}


Следующее функции обрабатывает сообщение уведомления TCN_SELCHANGE для основного диалогового блока. Функция уничтожает диалогового блока для исходящей страницы, если имеется. Затем это использует функцию CreateDialogIndirect, чтобы создавать независимого диалогового блока для поступающей страницы.

// OnSelChanged - ОБРАБАТЫВАЕТ уведомление TCN_SELCHANGE.
// hwndDlg - Ручка родительского диалогового блока

ПУСТОТА WINAPI OnSelChanged(HWND hwndDlg)
{
DLGHDR *pHdr = (DLGHDR *) GetWindowLong( hwndDlg, GWL_USERDATA);
int iSel = TabCtrl_GetCurSel(pHdr->hwndTab);

// Уничтожьте диалогового блока текущего ребенка, если имеется.
если (pHdr->hwndDisplay != НЕДЕЙСТВИТЕЛЬНЫЙ) DestroyWindow(pHdr->hwndDisplay);

// Создайте диалогового блока нового ребенка.
pHdr->hwndDisplay = CreateDialogIndirect(g_hinst,

pHdr->apRes[iSel], hwndDlg, ChildDialogProc);
}


Следующее функции обрабатывает сообщение WM_INITDIALOG для каждых диалоговых блоков ребенка. Вы не можете определить позицию диалогового блока созданного используя функцию CreateDialogIndirect. Эта функция использует функцию SetWindowPos, чтобы позиционировать диалог ребенка в пределах управляющей дисплейной области таб..

// OnChildDialogInit - ПОЗИЦИОНИРУЕТ диалогового блока ребенка, чтобы падать // в пределах дисплейной области управления таб..

ПУСТОТА WINAPI OnChildDialogInit(HWND hwndDlg)
{
HWND hwndParent = GetParent(hwndDlg);
DLGHDR *pHdr = (DLGHDR *) GetWindowLong( hwndParent, GWL_USERDATA);
SetWindowPos(hwndDlg, HWND_TOP, pHdr->rcDisplay.left, pHdr->rcDisplay.top, 0, 0, SWP_NOSIZE);
}



Вверх Version 1.3, Oct 26 2010 © 2007, 2010, mrhx Вверх
 mrhx software  Русский перевод OpenGL  Русский перевод Win32 API
 
Используются технологии uCoz