Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Win32 API - Tab Control Not Working

Win32 API - Tab Control Not Working

Scheduled Pinned Locked Moved C / C++ / MFC
jsonlounge
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    capricious_001
    wrote on last edited by
    #1

    Hi guys, I was having a bit of trouble with certain tabs not displaying their appropriate dialog controls. The options Dialog appears and shows it has 2 tabs. However when the 2nd tab is clicked, it becomes "on top" or becomes focused, but the dialog control associated with it does not appear below the tab. Basically if I click any tab, it shows only the dialog control associated with the general tab (1st tab) and not the dialog controls associated with the tab that was chosen by the user. Below is the code for your perusal: BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND tab1_hwnd = NULL; HWND tab2_hwnd = NULL; HWND CurrentTabHwnd = NULL; int CurrentTabIndex; HWND hwndTab = NULL; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), hwndTab, General_OptionsDialogProc, 0); tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_ADVANCED), hwndTab, Advanced_OptionsDialogProc, 0); CurrentTabIndex = 0; CurrentTabHwnd = tab1_hwnd; ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); } break; case WM_NOTIFY: { LPNMHDR lpnmhdr = (LPNMHDR) lParam; switch(lpnmhdr->code){ case TCN_SELCHANGING: { ShowWindow(CurrentTabHwnd, SW_HIDE); SendMessage(hwndTab, TCM_GETCURSEL, 0, 0); switch(TabCtrl_GetCurSel(hwndTab)){ case 0: ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 0; break; case 1: ShowWindow(tab2_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 1; break; } } break; default: return FALSE; } } break; case WM_COMMAND: { switch(LOWORD(wParam)){ case IDOK: EndDialog(hwnd,0); break; case IDCANCEL: EndDialog(hwnd,0); break; } } break; case WM_CLOSE: EndDialog(hwnd, 0); break; default: return FALSE; } return 0; }

    L V 2 Replies Last reply
    0
    • C capricious_001

      Hi guys, I was having a bit of trouble with certain tabs not displaying their appropriate dialog controls. The options Dialog appears and shows it has 2 tabs. However when the 2nd tab is clicked, it becomes "on top" or becomes focused, but the dialog control associated with it does not appear below the tab. Basically if I click any tab, it shows only the dialog control associated with the general tab (1st tab) and not the dialog controls associated with the tab that was chosen by the user. Below is the code for your perusal: BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND tab1_hwnd = NULL; HWND tab2_hwnd = NULL; HWND CurrentTabHwnd = NULL; int CurrentTabIndex; HWND hwndTab = NULL; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), hwndTab, General_OptionsDialogProc, 0); tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_ADVANCED), hwndTab, Advanced_OptionsDialogProc, 0); CurrentTabIndex = 0; CurrentTabHwnd = tab1_hwnd; ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); } break; case WM_NOTIFY: { LPNMHDR lpnmhdr = (LPNMHDR) lParam; switch(lpnmhdr->code){ case TCN_SELCHANGING: { ShowWindow(CurrentTabHwnd, SW_HIDE); SendMessage(hwndTab, TCM_GETCURSEL, 0, 0); switch(TabCtrl_GetCurSel(hwndTab)){ case 0: ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 0; break; case 1: ShowWindow(tab2_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 1; break; } } break; default: return FALSE; } } break; case WM_COMMAND: { switch(LOWORD(wParam)){ case IDOK: EndDialog(hwnd,0); break; case IDCANCEL: EndDialog(hwnd,0); break; } } break; case WM_CLOSE: EndDialog(hwnd, 0); break; default: return FALSE; } return 0; }

      L Offline
      L Offline
      Laxman Auti
      wrote on last edited by
      #2

      wrote:

      HWND tab1_hwnd = NULL; HWND tab2_hwnd = NULL; HWND CurrentTabHwnd = NULL; int CurrentTabIndex; HWND hwndTab = NULL;

      The HWND variables are destroyed when you return from the callback procedure.. Due to which when next time the procedure called it declares the HWND variables and WM_INITDIALOG message fire only once.. solution for this is make the variables as static/global. Knock out 't' from can't, You can if you think you can :cool:

      C 1 Reply Last reply
      0
      • C capricious_001

        Hi guys, I was having a bit of trouble with certain tabs not displaying their appropriate dialog controls. The options Dialog appears and shows it has 2 tabs. However when the 2nd tab is clicked, it becomes "on top" or becomes focused, but the dialog control associated with it does not appear below the tab. Basically if I click any tab, it shows only the dialog control associated with the general tab (1st tab) and not the dialog controls associated with the tab that was chosen by the user. Below is the code for your perusal: BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { HWND tab1_hwnd = NULL; HWND tab2_hwnd = NULL; HWND CurrentTabHwnd = NULL; int CurrentTabIndex; HWND hwndTab = NULL; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), hwndTab, General_OptionsDialogProc, 0); tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_ADVANCED), hwndTab, Advanced_OptionsDialogProc, 0); CurrentTabIndex = 0; CurrentTabHwnd = tab1_hwnd; ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); } break; case WM_NOTIFY: { LPNMHDR lpnmhdr = (LPNMHDR) lParam; switch(lpnmhdr->code){ case TCN_SELCHANGING: { ShowWindow(CurrentTabHwnd, SW_HIDE); SendMessage(hwndTab, TCM_GETCURSEL, 0, 0); switch(TabCtrl_GetCurSel(hwndTab)){ case 0: ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 0; break; case 1: ShowWindow(tab2_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 1; break; } } break; default: return FALSE; } } break; case WM_COMMAND: { switch(LOWORD(wParam)){ case IDOK: EndDialog(hwnd,0); break; case IDCANCEL: EndDialog(hwnd,0); break; } } break; case WM_CLOSE: EndDialog(hwnd, 0); break; default: return FALSE; } return 0; }

        V Offline
        V Offline
        Viorel
        wrote on last edited by
        #3

        Maybe you should combine SW_SHOWDEFAULT for current sub-dialog with SW_HIDE for the rest ones, for instance:

        ShowWindow(tab1_hwnd, SW_SHOWDEFAULT);
        ShowWindow(tab2_hwnd, SW_HIDE);
        
        1 Reply Last reply
        0
        • L Laxman Auti

          wrote:

          HWND tab1_hwnd = NULL; HWND tab2_hwnd = NULL; HWND CurrentTabHwnd = NULL; int CurrentTabIndex; HWND hwndTab = NULL;

          The HWND variables are destroyed when you return from the callback procedure.. Due to which when next time the procedure called it declares the HWND variables and WM_INITDIALOG message fire only once.. solution for this is make the variables as static/global. Knock out 't' from can't, You can if you think you can :cool:

          C Offline
          C Offline
          capricious_001
          wrote on last edited by
          #4

          Hey guys, Thanks a lot for your help, it actually works now. Declaring the handles as static actually fixed the darn thing. I never realized the callback procedure kills the handles everytime its returned but I can see why that problem was occuring. Im just glad we were able to fix it and now I can move on to bigger and better things. I will include my code for future reference for anyone searching for a tab control example in C++ because I was never able to find an example like this anywhere on the net. BOOL CALLBACK OptionsDialogProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND tab1_hwnd = NULL; static HWND tab2_hwnd = NULL; static HWND CurrentTabHwnd = NULL; static int CurrentTabIndex; static HWND hwndTab = NULL; switch(msg) { case WM_INITDIALOG: { LPSTR tab1_title = "General"; LPSTR tab2_title = "Advanced"; INITCOMMONCONTROLSEX tabctl; tabctl.dwSize = sizeof(tabctl); tabctl.dwICC = ICC_TAB_CLASSES; InitCommonControlsEx(&tabctl); hwndTab = GetDlgItem(hwnd,IDC_TAB1); TCITEM ItemStruct; ItemStruct.mask = TCIF_TEXT; ItemStruct.pszText = tab1_title; ItemStruct.cchTextMax = sizeof(tab1_title); ItemStruct.iImage = 0; ItemStruct.lParam = 0; SendMessage(hwndTab, TCM_INSERTITEM, 0, (LPARAM)&ItemStruct); ItemStruct.pszText = tab2_title; ItemStruct.cchTextMax = sizeof(tab2_title); SendMessage(hwndTab, TCM_INSERTITEM, 1, (LPARAM)&ItemStruct); tab1_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_GENERAL), hwndTab, General_OptionsDialogProc, 0); tab2_hwnd = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_OPTIONS_ADVANCED), hwndTab, Advanced_OptionsDialogProc, 0); CurrentTabIndex = 0; CurrentTabHwnd = tab1_hwnd; ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); } break; case WM_NOTIFY: { LPNMHDR lpnmhdr = (LPNMHDR) lParam; switch(lpnmhdr->code){ case TCN_SELCHANGE: { ShowWindow(CurrentTabHwnd, SW_HIDE); switch(TabCtrl_GetCurSel(hwndTab)){ case 0: ShowWindow(tab1_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab1_hwnd; CurrentTabIndex = 0; break; case 1: ShowWindow(tab2_hwnd,SW_SHOWDEFAULT); CurrentTabHwnd = tab2_hwnd; CurrentTabIndex = 1; break; } } break; default: retur

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups