Advertisement
Guest User

Holy.Cow Tab Control Texture

a guest
Nov 6th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.41 KB | None | 0 0
  1. ///////////////////////////////////////
  2. /// Resource file
  3.  
  4. 1 DIALOGEX 32, 32, 300, 200
  5. STYLE DS_MODALFRAME |  WS_POPUP |
  6.       WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
  7. CAPTION "Sample"
  8. FONT 8, "MS Shell Dlg"
  9. BEGIN
  10.     LTEXT "Hello World!",100,7,8,100,10
  11. END
  12.  
  13. 2 DIALOGEX DISCARDABLE  32, 32, 200, 76
  14. STYLE  WS_CHILD | DS_CONTROL
  15. CAPTION "Sample"
  16. FONT 8, "MS Shell Dlg"
  17. BEGIN
  18.  LTEXT "A mumbler is needed.",-1,7,8,100,10
  19.  AUTORADIOBUTTON "Do not &obtain a mumber now",
  20.                  100,17,24,180,10
  21.  AUTORADIOBUTTON "Obtain a mumbler auto&matically",
  22.                  101,17,34,180,10
  23.  AUTORADIOBUTTON "&Enter mumbler manually",
  24.                  102,17,44,180,10
  25.  DEFPUSHBUTTON "OK",IDOK,92,58,50,14
  26.  PUSHBUTTON "Cancel",IDCANCEL,146,58,50,14
  27. END
  28.  
  29.  
  30.  
  31. ////////////////////////////////////////
  32. /// Here's the code
  33. #include <windows.h>
  34. #include <windowsx.h>
  35. #include <uxtheme.h>
  36. #include <commctrl.h>
  37.  
  38. #pragma comment(lib, "uxtheme.lib")
  39. #pragma comment(lib, "comctl32.lib")
  40.  
  41. #pragma comment(linker, \
  42.     "\"/manifestdependency:type='Win32' "\
  43.     "name='Microsoft.Windows.Common-Controls' "\
  44.     "version='6.0.0.0' "\
  45.     "processorArchitecture='*' "\
  46.     "publicKeyToken='6595b64144ccf1df' "\
  47.     "language='*'\"")
  48.  
  49. #define  DYNACONTROL_WINDOW   "_ODynaControlWindow_"
  50.  
  51. HINSTANCE g_hInst = NULL;
  52. HWND g_hDlg = NULL;
  53. HWND g_hWndTab = NULL;
  54. HWND g_hStatic = NULL;
  55. HWND g_hChildDlg = NULL;
  56. HWND g_hCustomPanel = NULL;
  57.  
  58. INT_PTR CALLBACK DlgProc(HWND hdlg, UINT uMsg,
  59.     WPARAM wParam, LPARAM lParam);
  60.  
  61. INT_PTR CALLBACK ChildDlgProc(HWND hdlg, UINT uMsg,
  62.     WPARAM wParam, LPARAM lParam);
  63.  
  64. HRESULT OnSize(HWND hwndTab, LPARAM lParam);
  65.  
  66. BOOL OnNotify(HWND hwndTab, HWND hwndDisplay, LPARAM lParam);
  67.  
  68. HWND DoCreateTabControl(HWND hwndParent);
  69.  
  70. void CenterChildControls();
  71.  
  72. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  73. {
  74.     g_hInst = hInstance;
  75.     return DialogBox(hInstance, MAKEINTRESOURCE(1), 0, DlgProc);
  76. }
  77.  
  78. INT_PTR CALLBACK DlgProc(HWND hdlg, UINT uMsg,
  79.     WPARAM wParam, LPARAM lParam)
  80. {
  81.     switch (uMsg) {
  82.     case WM_INITDIALOG:
  83.         {
  84.             g_hDlg = hdlg;
  85.             CheckRadioButton(hdlg, 100, 102, 100);
  86.  
  87.             g_hWndTab = DoCreateTabControl(hdlg);
  88.  
  89.             g_hStatic = GetDlgItem(hdlg, 100);
  90.  
  91.             g_hChildDlg = CreateDialog(g_hInst, MAKEINTRESOURCE(2), g_hDlg, ChildDlgProc);
  92.             EnableThemeDialogTexture(g_hChildDlg, ETDT_ENABLETAB);
  93.  
  94.             WNDCLASS wndcls = {0};
  95.             wndcls.style            = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
  96.             wndcls.lpfnWndProc      = DefWindowProc;
  97.             wndcls.cbClsExtra       = wndcls.cbWndExtra = 0;
  98.             wndcls.hInstance        = g_hInst;
  99.             wndcls.hIcon            = NULL;
  100.             wndcls.hbrBackground    = NULL;
  101.             wndcls.lpszMenuName     = NULL;
  102.             wndcls.lpszClassName    = DYNACONTROL_WINDOW;
  103.  
  104.             if (RegisterClass(&wndcls))
  105.             {
  106.                 g_hCustomPanel = CreateWindowEx(WS_EX_CONTROLPARENT,
  107.                     DYNACONTROL_WINDOW, NULL,
  108.                     WS_CHILD | WS_TABSTOP | WS_GROUP,
  109.                     0, 0, 100, 100,
  110.                     g_hDlg,
  111.                     NULL,
  112.                     g_hInst,
  113.                     NULL
  114.                     );
  115.                 if ( g_hCustomPanel )
  116.                 {
  117.                     HWND hStatic = CreateWindow(WC_STATIC, "My Panel", WS_CHILD|WS_VISIBLE|WS_GROUP|SS_LEFT,
  118.                         10, 10, 100, 30,
  119.                         g_hCustomPanel,
  120.                         NULL, g_hInst, NULL);
  121.                     EnableThemeDialogTexture(g_hCustomPanel, ETDT_ENABLETAB);
  122.                 }
  123.             }
  124.             CenterChildControls();
  125.         }
  126.         return TRUE;
  127.     case WM_SIZE:
  128.         return OnSize(g_hWndTab, lParam);
  129.     case WM_NOTIFY:
  130.         return OnNotify(g_hWndTab, g_hStatic, lParam);
  131.     case WM_COMMAND:
  132.         switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  133.         case IDOK:
  134.             break;
  135.         case IDCANCEL:
  136.             EndDialog(hdlg, -1);
  137.             break;
  138.         }
  139.     }
  140.     return FALSE;
  141. }
  142.  
  143. INT_PTR CALLBACK ChildDlgProc(HWND hdlg, UINT uMsg,
  144.     WPARAM wParam, LPARAM lParam)
  145. {
  146.     switch (uMsg) {
  147.     case WM_INITDIALOG:
  148.         return TRUE;
  149.     }
  150.     return FALSE;
  151. }
  152.  
  153. BOOL OnNotify(HWND hwndTab, HWND hwndDisplay, LPARAM lParam)
  154. {
  155.     switch (((LPNMHDR)lParam)->code)
  156.     {
  157.     case TCN_SELCHANGING:
  158.         {
  159.             // Return FALSE to allow the selection to change.
  160.             return FALSE;
  161.         }
  162.     case TCN_SELCHANGE:
  163.         {
  164.             int iPage = TabCtrl_GetCurSel(hwndTab);
  165.  
  166.             ShowWindow(g_hStatic, 0 == iPage);
  167.             ShowWindow(g_hChildDlg, 1 == iPage);
  168.             ShowWindow(g_hCustomPanel, 2 == iPage);
  169.             break;
  170.         }
  171.     }
  172.     return TRUE;
  173. }
  174.  
  175. void CenterControl(HWND hWnd, const RECT& rcTab)
  176. {
  177.     RECT rc;
  178.     GetWindowRect(hWnd, &rc);
  179.     ScreenToClient(g_hDlg, (POINT*)&rc);
  180.     ScreenToClient(g_hDlg, ((POINT*)&rc)+1);
  181.     SetWindowPos(hWnd, HWND_TOP,
  182.         rcTab.left+(rcTab.right-rcTab.left-(rc.right-rc.left))/2,
  183.         rcTab.top+(rcTab.bottom-rcTab.top-(rc.bottom-rc.top))/2,
  184.         -1, -1, SWP_NOSIZE);
  185. }
  186.  
  187. void CenterChildControls()
  188. {
  189.     // Center the static control
  190.     RECT rcTab;
  191.     GetWindowRect(g_hWndTab, &rcTab);
  192.     ScreenToClient(g_hDlg, (POINT*)&rcTab);
  193.     ScreenToClient(g_hDlg, ((POINT*)&rcTab)+1);
  194.     TabCtrl_AdjustRect(g_hWndTab, FALSE, &rcTab);
  195.  
  196.     CenterControl(g_hStatic, rcTab);
  197.     CenterControl(g_hChildDlg, rcTab);
  198.     CenterControl(g_hCustomPanel, rcTab);
  199. }
  200.  
  201. #define TAB_GAP_TO_DLG  20
  202.  
  203. HRESULT OnSize(HWND hwndTab, LPARAM lParam)
  204. {
  205.     if (hwndTab == NULL)
  206.         return E_INVALIDARG;
  207.  
  208.     // Resize the tab control to fit the client are of main window.
  209.     if (!SetWindowPos(hwndTab, HWND_TOP, TAB_GAP_TO_DLG, TAB_GAP_TO_DLG, GET_X_LPARAM(lParam)-TAB_GAP_TO_DLG*2, GET_Y_LPARAM(lParam)-TAB_GAP_TO_DLG*2, SWP_SHOWWINDOW))
  210.         return E_FAIL;
  211.  
  212.     CenterChildControls();
  213.  
  214.     return S_OK;
  215. }
  216.  
  217. #define DAYS_IN_WEEK 7
  218.  
  219. HWND DoCreateTabControl(HWND hwndParent)
  220. {
  221.     RECT rcClient;
  222.     INITCOMMONCONTROLSEX icex;
  223.     HWND hwndTab;
  224.     TCITEM tie;
  225.     int i;
  226.  
  227.     // Initialize common controls.
  228.     icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
  229.     icex.dwICC = ICC_TAB_CLASSES;
  230.     InitCommonControlsEx(&icex);
  231.  
  232.     GetClientRect(hwndParent, &rcClient);
  233.     hwndTab = CreateWindow(WC_TABCONTROL, "",
  234.         WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
  235.         TAB_GAP_TO_DLG, TAB_GAP_TO_DLG, rcClient.right-TAB_GAP_TO_DLG*2, rcClient.bottom-TAB_GAP_TO_DLG*2,
  236.         hwndParent, NULL, g_hInst, NULL);
  237.     if (hwndTab == NULL)
  238.     {
  239.         return NULL;
  240.     }
  241.  
  242.     // Add tabs for each day of the week.
  243.     tie.mask = TCIF_TEXT | TCIF_IMAGE;
  244.     tie.iImage = -1;
  245.  
  246.     TCHAR* arrTabItems[] = {"No child dialog", "Child dialog", "Custom panel"};
  247.  
  248.     for (i = 0; i < _countof(arrTabItems); i++)
  249.     {
  250.         tie.pszText = arrTabItems[i];
  251.         if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1)
  252.         {
  253.             DestroyWindow(hwndTab);
  254.             return NULL;
  255.         }
  256.     }
  257.     return hwndTab;
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement