Tab Control Win32 API

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qhimq
    New Member
    • May 2007
    • 48

    Tab Control Win32 API

    Hi, could someone tell me how to fix this? The edit control does not show up however the mouse changes around the position i want it to display.

    https://netfiles.uiuc.e du/qmitche2/www/tabs.exe

    Code:
    #include <windows.h>
    #include <commctrl.h>
    #include <stdio.h>
    
    HWND hwndMain;
    HWND hwndTab;
    HWND hwnd0;
    HWND hwnd1;
    HWND hwnd2;
    HWND hwnd3;
    HINSTANCE hinst;
    
    LRESULT CALLBACK ChildrenProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    
    void CloseWindows()
    {
    	DestroyWindow( hwnd0 );
    	DestroyWindow( hwnd1 );
    	DestroyWindow( hwnd2 );
    	DestroyWindow( hwnd3 );
    }
    
    void MakeWindow(int num)
    {
    	if(num==0)
    	{
    		hwnd0=CreateWindowEx(0, 
    				"TabChild", 
    				"", 
    				WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS, 
    				0, 0, 
    				300, 300, 
    				hwndMain, 
    				NULL, 
    				hinst,
    				NULL); 
    	
    		HWND hwnd01=CreateWindow(
    					"EDIT",
    					"Just",
    					WS_VISIBLE | WS_CHILD | WS_TABSTOP | WS_BORDER,
    					0, 50, 100, 25,
    					hwnd0, 
    					NULL,
    					hinst,
    					NULL);
    
    		SetWindowPos(hwnd0,HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);
    		SetWindowPos(hwnd01,HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
    	}
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
    		case WM_NOTIFY:
                switch (((LPNMHDR)lParam)->code)
                {
                case TCN_SELCHANGE:
                    { 
    					int iPage = TabCtrl_GetCurSel(hwndTab);
    					CloseWindows();
    					if(iPage == 0)
    						MakeWindow(0);
    					if(iPage == 1)
    						MakeWindow(1);
    					if(iPage == 2)
    						MakeWindow(2);
    					if(iPage == 3)
    						MakeWindow(3);
                        break;
                    } 
                }
    		break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    	hinst=hInstance;
    
        
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = "myWindowClass";
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        RegisterClassEx(&wc);
    
       
        hwndMain = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            "myWindowClass",
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 350, 300,
            NULL, NULL, hInstance, NULL);
    
     
    
        ShowWindow(hwndMain, nCmdShow);
        UpdateWindow(hwndMain);
    	InitCommonControls();
    	hwndTab=CreateWindowEx(0, 
    				"SysTabControl32", 
    				"",
    				WS_CHILD|WS_CLIPSIBLINGS|WS_VISIBLE,
    				0, 0, 
    				300, 250, 
    				hwndMain, 
    				NULL, 
    				hInstance, 
    				NULL); 
    	
    	TCITEM tie;
        tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi1"; 
    	TabCtrl_InsertItem(hwndTab, 0, &tie);
    	
    	tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi2"; 
    	TabCtrl_InsertItem(hwndTab, 1, &tie);
    	
    	tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi3"; 
    	TabCtrl_InsertItem(hwndTab, 2, &tie);
    	
    	tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi4"; 
    	TabCtrl_InsertItem(hwndTab, 3, &tie);
    	
    	TabCtrl_SetCurSel(hwndTab, 1);
    	ShowWindow(hwndTab, SW_SHOW);
        UpdateWindow(hwndTab);
    	
    	WNDCLASSEX wcx;
    	wcx.cbSize        = sizeof(WNDCLASSEX);
        wcx.style         = 0;
        wcx.lpfnWndProc   = ChildrenProc;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hInstance     = hInstance;
        wcx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wcx.lpszMenuName  = NULL;
        wcx.lpszClassName = "TabChild";
        wcx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        RegisterClassEx(&wcx);
    	
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
  • qhimq
    New Member
    • May 2007
    • 48

    #2
    ah figured it out.

    Code:
    #include <windows.h>
    #include <commctrl.h>
    
    HWND hwndMain;
    HWND hwndTab;
    HWND hwnd0;
    HWND hwnd1;
    HWND hwnd2;
    HWND hwnd3;
    HINSTANCE hinst;
    
    LRESULT CALLBACK ChildrenProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    
    void CloseWindows()
    {
    	ShowWindow(hwnd0,SW_HIDE);
    	ShowWindow(hwnd1,SW_HIDE);
    	ShowWindow(hwnd2,SW_HIDE);
    	ShowWindow(hwnd3,SW_HIDE);
    }
    
    void MakeWindows()
    {
    	hwnd0=CreateWindow(
    					"EDIT",
    					"Just1",
    					WS_CHILD | WS_TABSTOP | WS_BORDER,
    					0, 50, 100, 25,
    					hwndMain, 
    					NULL,
    					hinst,
    					NULL);
    	hwnd1=CreateWindow(
    					"EDIT",
    					"Just2",
    					WS_CHILD | WS_TABSTOP | WS_BORDER,
    					0, 50, 100, 25,
    					hwndMain, 
    					NULL,
    					hinst,
    					NULL);
    	hwnd2=CreateWindow(
    					"EDIT",
    					"Just3",
    					WS_CHILD | WS_TABSTOP | WS_BORDER,
    					0, 50, 100, 25,
    					hwndMain, 
    					NULL,
    					hinst,
    					NULL);
    	hwnd3=CreateWindow(
    					"EDIT",
    					"Just4",
    					WS_CHILD | WS_TABSTOP | WS_BORDER,
    					0, 50, 100, 25,
    					hwndMain, 
    					NULL,
    					hinst,
    					NULL);
    	
    	
    }
    
    void SHWindow(int num)
    {
    	if(num==0)
    	{
    		ShowWindow(hwnd0,SW_SHOW);
    		SetFocus(hwnd0);
    	}
    	if(num==1)
    	{
    		ShowWindow(hwnd1,SW_SHOW);
    		SetFocus(hwnd1);
    	}
    	if(num==2)
    	{
    		ShowWindow(hwnd2,SW_SHOW);
    		SetFocus(hwnd2);
    	}
    	if(num==3)
    	{
    		ShowWindow(hwnd3,SW_SHOW);
    		SetFocus(hwnd3);
    	}
    	
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
            case WM_CLOSE:
                DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
    		case WM_NOTIFY:
                switch (((LPNMHDR)lParam)->code)
                {
                case TCN_SELCHANGE:
                    { 
    					int iPage = TabCtrl_GetCurSel(hwndTab);
    					CloseWindows();
    					if(iPage == 0)
    						SHWindow(0);
    					if(iPage == 1)
    						SHWindow(1);
    					if(iPage == 2)
    						SHWindow(2);
    					if(iPage == 3)
    						SHWindow(3);
                        break;
                    } 
                }
    		break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
    {
        WNDCLASSEX wc;
        HWND hwnd;
        MSG Msg;
    	hinst=hInstance;
    
        
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = "myWindowClass";
        wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
        RegisterClassEx(&wc);
    
       
        hwndMain = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            "myWindowClass",
            "The title of my window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 350, 300,
            NULL, NULL, hInstance, NULL);
    
     
    
        ShowWindow(hwndMain, nCmdShow);
        UpdateWindow(hwndMain);
    	InitCommonControls();
    	hwndTab=CreateWindowEx(0, 
    				"SysTabControl32", 
    				"",
    				WS_CHILD|WS_CLIPSIBLINGS|WS_VISIBLE,
    				0, 0, 
    				300, 250, 
    				hwndMain, 
    				NULL, 
    				hInstance, 
    				NULL); 
    	
    	TCITEM tie;
        tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi1"; 
    	TabCtrl_InsertItem(hwndTab, 0, &tie);
    	
    	tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi2"; 
    	TabCtrl_InsertItem(hwndTab, 1, &tie);
    	
    	tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi3"; 
    	TabCtrl_InsertItem(hwndTab, 2, &tie);
    	
    	tie.mask = TCIF_TEXT; 
        tie.pszText = "Hi4"; 
    	TabCtrl_InsertItem(hwndTab, 3, &tie);
    	
    	
    	MakeWindows();
    	TabCtrl_SetCurSel(hwndTab, 1);
    	SHWindow(1);
    	
    	
    	ShowWindow(hwndTab, SW_SHOW);
        UpdateWindow(hwndTab);
    	
    
    	
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    Kinda crappy solution...but it works.

    Comment

    • qhimq
      New Member
      • May 2007
      • 48

      #3
      Sorry for reviving an old thread but heres a way better/cooler solution with more functionality
      Code:
      #include <windows.h>
      #include <commctrl.h>
      #include <stdio.h>
      
      HWND hwndMain;
      HWND hwndTab;
      HINSTANCE hinst;
      int oldTabPage;
      
      int NumToPower(int num, int power)
      {
      	if(power==1)
      		return num;
      	
      	return num*NumToPower(num, power-1);
      }
      
      int getlines(int Tab)
      {
      	HWND hwndEdit;
      	int Res=NumToPower(10, Tab+3);
      	int lines=-1;
      	do
      	{
      		hwndEdit=GetDlgItem(hwndMain, (Res+lines+1));
      		lines++;
      	}while(hwndEdit!=0);
      		
      	return lines;
      }
      
      
      
      void HideWindowsOfTab(int Tab)
      {
          HWND hwndEdit;
      	int Res=NumToPower(10, Tab+3);
      	int lines=getlines(Tab);
      	
      	for(int i=0; i<lines; i++)
      	{
      		hwndEdit=GetDlgItem(hwndMain, (Res+i));
      		ShowWindow(hwndEdit,SW_HIDE);
      	}
      }
      
      void MakeVisibleWindows(int Tab)
      {
      	HWND hwndEdit;
      	int Res=NumToPower(10, Tab+3);
      	int lines=getlines(Tab);
      	
      	for(int i=0; i<lines; i++)
      	{
      		hwndEdit=GetDlgItem(hwndMain, (Res+i));
      		ShowWindow(hwndEdit,SW_SHOW);
      	}
      }
      
      void AddRow()
      {
      	int iPage = TabCtrl_GetCurSel(hwndTab);
      	int lines = getlines(iPage);
      	int Res=NumToPower(10,iPage+3);
      	HWND a=CreateWindow(
                          "EDIT",
                          "Just1",
                          WS_CHILD | WS_TABSTOP | WS_BORDER |WS_VISIBLE ,
                          10, 100+lines*25, 100, 25,
                          hwndMain,
                          (HMENU)(Res+lines), 
                          hinst,
                          NULL);
      	SetWindowPos(a, HWND_TOP, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
      
      }
      
      void MakeNewTab()
      {
      	int max=TabCtrl_GetItemCount(hwndTab);
      	TCITEM tie;
          tie.mask = TCIF_TEXT;
          tie.pszText = "Hi";
          TabCtrl_InsertItem(hwndTab, max+1, &tie);
      }
       
      LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
      {
          switch(msg)
          {
              case WM_CLOSE:
                  DestroyWindow(hwnd);
              break;
              case WM_DESTROY:
                  PostQuitMessage(0);
              break;
      		case WM_COMMAND:
      			if(LOWORD(wParam)==2)
      				AddRow();
      			if(LOWORD(wParam)==3)
      				MakeNewTab();
      		break;
              case WM_NOTIFY:
                  switch (((LPNMHDR)lParam)->code)
                  {
                  case TCN_SELCHANGE:
                  {
                      HideWindowsOfTab(oldTabPage);
      				int iPage = TabCtrl_GetCurSel(hwndTab);
                      MakeVisibleWindows(iPage);
      				oldTabPage=iPage;
                  }
      			break;
                  }
              break;
              default:
                  return DefWindowProc(hwnd, msg, wParam, lParam);
          }
          return 0;
      }
      
      void init()
      {
      	InitCommonControls();
          hwndTab=CreateWindowEx(0,
                      "SysTabControl32",
                      "",
                      WS_CHILD|WS_CLIPSIBLINGS,
                      0, 50,
                      300, 250,
                      hwndMain,
                      NULL,
                      hinst,
                      NULL);
      	SetWindowPos(hwndTab, HWND_BOTTOM, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
         
          TCITEM tie;
          tie.mask = TCIF_TEXT;
          tie.pszText = "Hi1";
          TabCtrl_InsertItem(hwndTab, 0, &tie);
         
           /*This should be changed.*/
          CreateWindow(
                          "EDIT",
                          "",
                          WS_CHILD,
                          10, 100, 100, 25,
                          hwndMain,
                          (HMENU)1, //open buffer
                          hinst,
                          NULL);
      	/*This should be changed*/
      	CreateWindow(
                          "BUTTON",
                          "Add Tab",
                          WS_CHILD | WS_TABSTOP | WS_VISIBLE,
                          0, 0, 75, 25,
                          hwndMain,
                          (HMENU)3, 
                          hinst,
                          NULL);
      	
      	HWND a=CreateWindow(
                          "BUTTON",
                          "Add Row",
                          WS_CHILD | WS_TABSTOP | WS_VISIBLE,
                          150, 100, 75, 25,
                          hwndMain,
                          (HMENU)2, 
                          hinst,
                          NULL);
      	SetWindowPos(a, HWND_TOP, 0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
      
      	TabCtrl_SetCurSel(hwndTab, 0);
          MakeVisibleWindows(0);
      	oldTabPage=0;
         
          ShowWindow(hwndTab, SW_SHOW);
          UpdateWindow(hwndTab);
      }
      
      int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
          LPSTR lpCmdLine, int nCmdShow)
      {
          WNDCLASSEX wc;
          HWND hwnd;
          MSG Msg;
          hinst=hInstance;
       
         
          wc.cbSize        = sizeof(WNDCLASSEX);
          wc.style         = 0;
          wc.lpfnWndProc   = WndProc;
          wc.cbClsExtra    = 0;
          wc.cbWndExtra    = 0;
          wc.hInstance     = hInstance;
          wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
          wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
          wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
          wc.lpszMenuName  = NULL;
          wc.lpszClassName = "myWindowClass";
          wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
       
          RegisterClassEx(&wc);
       
         
          hwndMain = CreateWindowEx(
              WS_EX_CLIENTEDGE,
              "myWindowClass",
              "The title of my window",
              WS_OVERLAPPEDWINDOW,
              CW_USEDEFAULT, CW_USEDEFAULT, 350, 400,
              NULL, NULL, hInstance, NULL);
       
       
       
          ShowWindow(hwndMain, nCmdShow);
          UpdateWindow(hwndMain);
      
      	init();
         
          while(GetMessage(&Msg, NULL, 0, 0) > 0)
          {
              TranslateMessage(&Msg);
              DispatchMessage(&Msg);
          }
          return Msg.wParam;
      }

      Comment

      Working...