[高分+在线等待]如何将现成的窗口添加Tab Control(win32 sdk)上?

csShooter 2005-10-11 02:38:22
被迫使用Win32 写一个窗口程序。

我写啊写啊写啊写啊写啊写啊写啊写,结果自己做的Dialog就是不能添加到TabControl上去!

pHdr->hwndTab = GetDlgItem(hwndDlg,IDC_TAB1);
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
tie.pszText = "注册用户";
TabCtrl_InsertItem(pHdr->hwndTab, 0, &tie);
tie.pszText = "新增注册";
TabCtrl_InsertItem(pHdr->hwndTab, 1, &tie);
tie.pszText = "安全管理";
TabCtrl_InsertItem(pHdr->hwndTab, 2, &tie);

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


// DLG_FIRST|second|third都是自己新建的对话框!。。。运行后能看到标签(注册用户|新增注册..),但只是一个空Tab!

各位师兄,有实例否? 指点。。
...全文
312 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Aizz 2005-10-11
楼主别客气,我也是试试看吧。

1. 如果单独显示创建的 dialog 是否可以显示?
2. 创建的 dialog 的父窗口是否是拥有 tab 的窗口?

OnSelChange时切换 tab 可以不用 DestroyWindow,把不需要的 dialog 隐藏效率会更好些。
  • 打赏
  • 举报
回复
csShooter 2005-10-11


Aizz兄,Create 应该算是成功的吧!

监视pHdr->hwndDisplay 得到的值为: 0x00200634
  • 打赏
  • 举报
回复
csShooter 2005-10-11
感谢Aizz...
  • 打赏
  • 举报
回复
Aizz 2005-10-11
// Create the new child dialog box.
pHdr->hwndDisplay = CreateDialogIndirect(hInst,
pHdr->apRes[iSel], hwndDlg, ChildDialogProc);

Create成功否?成功后ShowWindow()试试?
  • 打赏
  • 举报
回复
csShooter 2005-10-11
干脆把整个文件代码贴出来,各位师兄,看那儿有问题,我点标签就是没有东东出来!

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

HWND hEditResult;
HINSTANCE hInst;

#define C_PAGES 3
#define FORM_WITH 400
#define FORM_HEIGHT 215

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;


DLGTEMPLATE * WINAPI DoLockDlgRes_David(LPCSTR lpszResName);
void WINAPI OnTabbedDialogInit_David(HWND hwndDlg);
VOID WINAPI OnSelChanged(HWND hwndDlg);
INT_PTR CALLBACK ChildDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
VOID WINAPI OnChildDialogInit(HWND hwndDlg);

INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HWND hwndEdit;
HWND hwndRegCode;
TCHAR szBuffer[256];
TCHAR szRegCode[256];

RECT rcClient;
HWND hwndTab;
TCITEM tie;
int i;
char g_achTemp[]="测试";





// 主窗口消息循环
switch (uMsg)
{
case WM_INITDIALOG:

// 获取注册号
/*hwndRegCode = GetDlgItem(hwndDlg,IDC_EDIT_REGCODE);
SetWindowText(hwndRegCode,"GDFESFESFEFSFDSF");*/

// 创建Tab Control子页

// Add tabs for each day of the week.


//tie.mask = TCIF_TEXT | TCIF_IMAGE;
//tie.iImage = -1;
//tie.pszText = g_achTemp;

//hwndTab = GetDlgItem(hwndDlg,IDC_TAB1);
///*for (i = 0; i < 7; i++)
//{
// LoadString(g_hinst, IDS_FIRSTDAY + i,
// g_achTemp, sizeof(g_achTemp)/sizeof(g_achTemp[0]));
// if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1)
// {
// DestroyWindow(hwndTab);
// return NULL;
// }
//} */

//TabCtrl_InsertItem(hwndTab,0,&tie);
//TabCtrl_InsertItem(hwndTab,1,&tie);

OnTabbedDialogInit_David(hwndDlg);

break;
case WM_COMMAND:

// 关闭窗口,退出程序
if (LOWORD(wParam) == IDCANCEL) EndDialog( hwndDlg, 0 );


// 注册按钮
/*if (LOWORD(wParam) == IDC_BUTTON_REG)
{
hwndEdit = GetDlgItem ( hwndDlg, IDC_EDIT_SIERIAL );
GetWindowText ( hwndEdit, szBuffer, 256 );
MessageBox ( hwndDlg, szBuffer, TEXT("DDDD"), MB_OK );
}*/

}
return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
hInst=hInstance;
DialogBox( hInstance,(LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC) DialogProc);
return 0;
}


// Tab Control 运行
void WINAPI OnTabbedDialogInit_David(HWND hwndDlg)
{
DLGHDR *pHdr = (DLGHDR *) LocalAlloc(LPTR, sizeof(DLGHDR));
DWORD dwDlgBase = GetDialogBaseUnits();
int cxMargin = LOWORD(dwDlgBase) / 2;
int cyMargin = HIWORD(dwDlgBase) / 4;
TCITEM 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 = GetDlgItem(hwndDlg,IDC_TAB1);/*CreateWindow(
WC_TABCONTROL, "",
WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
0, 0, 100, 100,
hwndDlg, NULL, 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 = "注册用户";
TabCtrl_InsertItem(pHdr->hwndTab, 0, &tie);
tie.pszText = "新增注册";
TabCtrl_InsertItem(pHdr->hwndTab, 1, &tie);
tie.pszText = "安全管理";
TabCtrl_InsertItem(pHdr->hwndTab, 2, &tie);

// Lock the resources for the three child dialog boxes.
pHdr->apRes[0] = DoLockDlgRes_David(MAKEINTRESOURCE(DLG_FIRST));
pHdr->apRes[1] = DoLockDlgRes_David(MAKEINTRESOURCE(DLG_SECOND));
pHdr->apRes[2] = DoLockDlgRes_David(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 = FORM_WITH;/*rcTab.right * LOWORD(dwDlgBase) / 4; */
rcTab.bottom = FORM_HEIGHT;/*rcTab.bottom * HIWORD(dwDlgBase) / 8; */

// Calculate how large to make the tab control, so
// the display area can accommodate 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);


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

SetWindowPos(hwndButton, NULL,
rcTab.right - 158, rcTab.bottom + cyMargin, 0, 0,
SWP_NOSIZE | SWP_NOZORDER);

// Move the second button to the right of the first.
hwndButton = GetDlgItem(hwndDlg, BTN_TEST);
SetWindowPos(hwndButton, NULL,
rcTab.right - 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 box template resource.
// Returns the address of the locked resource.
// lpszResName - name of the resource

DLGTEMPLATE * WINAPI DoLockDlgRes_David(LPCSTR lpszResName)
{
HRSRC hrsrc = FindResource(NULL, lpszResName, RT_DIALOG);
HGLOBAL hglb = LoadResource(hInst, hrsrc);
return (DLGTEMPLATE *) LockResource(hglb);
}

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(hInst,
pHdr->apRes[iSel], hwndDlg, ChildDialogProc);
}

INT_PTR CALLBACK ChildDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_INITDIALOG:
//HWND hWndLabe = CreateWindow("EDIT", NULL, WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 0, 0, 100, 100,hwndDlg, NULL, hInst, NULL);
OnChildDialogInit(hwndDlg);

break;

}
return 0;
}

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);
MessageBox(hwndDlg,"dddd","fdfdfdf",MB_OK);
}

  • 打赏
  • 举报
回复
相关推荐
发帖
界面

1.5w+

社区成员

VC/MFC 界面
社区管理员
  • 界面
加入社区
帖子事件
创建了帖子
2005-10-11 02:38
社区公告
暂无公告