【C】简单的Tab控件实例
#include<Windows.h>#include<CommCtrl.h>
HINSTANCE g_hInst=NULL;
void AddItemToTabCtl(HWND hTab,int iItem,UINT mask,DWORD dwState,DWORD dwStateMask,LPTSTR pszText,int cchTextMax,int iImage,LPARAM lParam)
{
TCITEM Item;
Item.mask=mask;
Item.dwState=dwState;
Item.dwStateMask=dwStateMask;
Item.pszText=pszText;
if(cchTextMax)
Item.cchTextMax=cchTextMax;
else
Item.cchTextMax=lstrlen(pszText);
Item.iImage=iImage;
Item.lParam=lParam;
TabCtrl_InsertItem(hTab,iItem,&Item);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT Msg,WPARAM wp,LPARAM lp)
{
switch(Msg)
{
default:
return DefWindowProc(hWnd,Msg,wp,lp);
case WM_CREATE:
InitCommonControls();
do{
HWND hTab=CreateWindowEx(0,WC_TABCONTROL,TEXT("tab"),WS_CHILD|WS_VISIBLE,0,0,600,100,hWnd,(HMENU)233,g_hInst,NULL);//窗口句柄
AddItemToTabCtl(hTab,0,TCIF_TEXT,0,0,TEXT("选项1"),0,-1,0);
AddItemToTabCtl(hTab,1,TCIF_TEXT,0,0,TEXT("选项2"),0,-1,0);
AddItemToTabCtl(hTab,2,TCIF_TEXT,0,0,TEXT("选项3"),0,-1,0);
AddItemToTabCtl(hTab,3,TCIF_TEXT,0,0,TEXT("选项4"),0,-1,0);
AddItemToTabCtl(hTab,4,TCIF_TEXT,0,0,TEXT("选项5"),0,-1,0);
//Tab刚创建出来的时候字体像屎一样,所以要设置成系统默认字体。
SendMessage(hTab,WM_SETFONT,(WPARAM)GetStockObject(DEFAULT_GUI_FONT),TRUE);
}while(0);
break;
case WM_NOTIFY:
if(LOWORD(wp)==233)
{
//正要改变选项
if(((LPNMHDR)lp)->code==TCN_SELCHANGING)
{
switch(TabCtrl_GetCurSel(((LPNMHDR)lp)->hwndFrom))
{
case 0:MessageBox(hWnd,TEXT("选项1"),TEXT("之前的选项"),MB_OK);break;
case 1:MessageBox(hWnd,TEXT("选项2"),TEXT("之前的选项"),MB_OK);break;
case 2:MessageBox(hWnd,TEXT("选项3"),TEXT("之前的选项"),MB_OK);break;
case 3:MessageBox(hWnd,TEXT("选项4"),TEXT("之前的选项"),MB_OK);break;
case 4:MessageBox(hWnd,TEXT("选项5"),TEXT("之前的选项"),MB_OK);break;
}
}
//已经改变选项
if(((LPNMHDR)lp)->code==TCN_SELCHANGE)
{
switch(TabCtrl_GetCurSel(((LPNMHDR)lp)->hwndFrom))
{
case 0:MessageBox(hWnd,TEXT("选项1"),TEXT("新的选项"),MB_OK);break;
case 1:MessageBox(hWnd,TEXT("选项2"),TEXT("新的选项"),MB_OK);break;
case 2:MessageBox(hWnd,TEXT("选项3"),TEXT("新的选项"),MB_OK);break;
case 3:MessageBox(hWnd,TEXT("选项4"),TEXT("新的选项"),MB_OK);break;
case 4:MessageBox(hWnd,TEXT("选项5"),TEXT("新的选项"),MB_OK);break;
}
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return 0;
}
int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR szCmd,int nCmd)
{
WNDCLASSEX WCEx={sizeof(WNDCLASSEX),0,WndProc,0,0,g_hInst=hInst,NULL,LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)),(HBRUSH)COLOR_BTNFACE,NULL,TEXT("tuttab"),NULL};//窗口类
MSG msg;
HWND hWnd=CreateWindowEx(0,MAKEINTATOM(RegisterClassEx(&WCEx)),TEXT("tuttab"),WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,700,200,NULL,NULL,hInst,NULL);//窗口句柄
ShowWindow(hWnd,nCmd);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,0,0))//消息循环
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
//美化控件
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endifBIN:
SRC:
TAB控件其实并不是一个容器,它就是帮你显示了一个选项卡而已。你需要做的,就是在创建了Tab以后,通过切换选项卡的过程调用的回调函数,将不同的子窗口显示到最顶部。
所以你需要创建若干子窗口,把控件布局放进子窗口,然后在Tab被点击的时候,将指定的子窗口显示出来(并把其它子窗口藏起来)就行了。
其实Tab选项卡控件是支持带小图标的选项卡的。。。但是这有点画蛇添足了。其实要弄的话,添加图片的方法和工具栏(ToolBar)是一样的。 厉害额
页:
[1]