Tab控件代码示例--主窗口
#include <windows.h>
#include <windowsx.h>
#include <CommCtrl.h>
#include <winioctl.h>
#include <tchar.h>
#include <stdio.h>
#include "resource.h"
#include "ProcessDlg.h"
#include "ServiceDlg.h"
#include "global.h"
#pragma comment(lib,"comctl32")
#define Refresh(A) RedrawWindow((A),NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_ALLCHILDREN|RDW_UPDATENOW);
typedef INT_PTR (WINAPI *DIALOGPROC)(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);
DIALOGPROC DlgProc[]={ProcessDialogProc,ServiceDialogProc};
const C_PAGES=sizeof(DlgProc)/sizeof(DlgProc[0]);
HINSTANCE g_hInst;
TCHAR g_szClassName[] = TEXT("SysinfoClass");
HWND g_hwndTab;
HWND g_hListView[C_PAGES];
HWND g_hDlg[C_PAGES];
static int iCurrentPage=0; /* 当前页 */
//
void Cls_OnSize(HWND hwnd, UINT state, int cx, int cy)
{
RECT rc;
GetClientRect(g_hwndTab,&rc);
MoveWindow(g_hwndTab,0,0,cx,cy,FALSE);
for (int i=0;i<C_PAGES;i++)
{
MoveWindow(g_hDlg[i],0,28,cx,cy,FALSE);
}
Refresh(hwnd);
}
//
BOOL Cls_OnInitDialog(HWND hDlg, HWND hwndFocus, LPARAM lParam )
{
TCITEM tie;
TCHAR szTitle[30];
g_hwndTab=GetDlgItem(hDlg,IDC_TAB1);
tie.mask = TCIF_TEXT | TCIF_IMAGE;
tie.iImage = -1;
tie.pszText = szTitle;
_sntprintf(szTitle,29,TEXT("进程"));
TabCtrl_InsertItem(g_hwndTab, 0, &tie);
_sntprintf(szTitle,29,TEXT("服务"));
TabCtrl_InsertItem(g_hwndTab, 1, &tie);
RECT rc;
GetClientRect(g_hwndTab,&rc);
for (int i=0;i<C_PAGES;i++)
{
g_hDlg[i]=CreateDialog(g_hInst,MAKEINTRESOURCE(IDD_DIALOG1+i),g_hwndTab,DlgProc[i]);
MoveWindow(g_hDlg[i],0,28,rc.right - rc.left,rc.bottom - rc.top,FALSE);
}
ShowWindow(g_hDlg[0],SW_SHOW);
return TRUE;
}
//
void Cls_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
switch(id)
{
case IDM_EXIT:
EndDialog(hwnd,0);
break;
case IDM_REFRESH:
SendMessage(g_hDlg[iCurrentPage],WM_REFRESH,0,0);
break;
}
}
//
INT_PTR WINAPI MainWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
HANDLE_MSG(hwnd,WM_INITDIALOG,Cls_OnInitDialog);
HANDLE_MSG(hwnd,WM_COMMAND,Cls_OnCommand);
HANDLE_MSG(hwnd,WM_SIZE,Cls_OnSize);
case WM_CLOSE:
EndDialog(hwnd,0);
break;
case WM_NOTIFY:
if ((int)wParam==IDC_TAB1)
{
if (((LPNMHDR)lParam)->code==TCN_SELCHANGE)
{
ShowWindow(g_hDlg[iCurrentPage],SW_HIDE);
iCurrentPage= TabCtrl_GetCurSel(g_hwndTab);
ShowWindow(g_hDlg[iCurrentPage],SW_SHOW);
}
}
break;
}
return FALSE;
}
//
int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
InitCommonControls();
g_hInst=hInstance;
DialogBox( hInstance,MAKEINTRESOURCE(IDD_DLG_MAIN), NULL, MainWndProc );
return 0;
}