Page 1 of 1

MASM - Using Tab Controls

#1 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 859
  • View blog
  • Posts: 2,308
  • Joined: 28-March 11

Posted 01 September 2011 - 06:22 PM

Tabs are a cool control, but how do you get them to show different windows when clicked? Ah, I will show you how!
I will keep it simple

The attached sample contains complete source for the example.

In your Main window proc (the parent of the tab control) is where you will create the control
        ;#####  Create Tab Control
        invoke  CreateWindowEx, WS_EX_CONTROLPARENT, \
                            offset szWndClsTab, \
                            NULL, \
                            WS_CHILD or WS_VISIBLE or TCS_FOCUSONBUTTONDOWN, \
                            5, 5, \
                            590, 440, \
                            hWin, NULL, \
                            hInst, NULL
        mov     hTab, eax
        mov     esi, eax
        
        ;#####  Add the tabs
        invoke  HeapAlloc, hHeap, HEAP_ZERO_MEMORY, sizeof TCITEM
        mov     ebx, eax
        
        ;#####  Tab 1
        mov     (TCITEM PTR [ebx]).imask, TCIF_TEXT
        mov     (TCITEM PTR [ebx]).pszText, offset sz1stTab
        invoke  SendMessage, esi, TCM_INSERTITEM, 1, ebx

        ;#####  Tab 2
        mov     (TCITEM PTR [ebx]).pszText, offset sz2ndTab
        invoke  SendMessage, esi, TCM_INSERTITEM, 2, ebx


        ;#####  Create windows for tabs
        ;#####  Tab #1 window
        invoke  CreateWindowEx, WS_EX_CONTROLPARENT, \
                                offset szWndClsTab1, \
                                NULL, \
                                WS_CHILDWINDOW or WS_VISIBLE, \
                                LEFT_TAB_CHILD, TOP_TAB_CHILD, \
                                WIDTH_TAB_CHILD, HEIGHT_TAB_CHILD, \
                                hTab, NULL, \
                                hInst, NULL
        mov     hTab1, eax
        
        ;#####  Tab #2 window
        invoke  CreateWindowEx, WS_EX_CONTROLPARENT, \
                                offset szWndClsTab2, \
                                NULL, \
                                WS_CHILDWINDOW or WS_VISIBLE, \
                                LEFT_TAB_CHILD, TOP_TAB_CHILD, \
                                WIDTH_TAB_CHILD, HEIGHT_TAB_CHILD, \
                                hTab, NULL, \
                                hInst, NULL
        mov     hTab2, eax

        
        mov     WhichTabChosen, 0
        invoke  ShowWindow, hTab2, SW_HIDE



When creating the tabs, you will notice that I fill in the structure fields that are needed for the first tab, then for the second tab, only change
the displayed string for the second tab. You do not have to fill all the structure fields for each tab since the structure keeps the info between SendMessage Calls.

WhichTabChosen will be how we keep track of.... take a guess.... Which tab was chosen. I NEVER create variable names like a, b, etc... Make your variable names descriptive so you know
exactly what it is for.

When your main window is displayed, the last tab window created will be the one visible, so I just hide it.

Now, the "magic" happens in WM_NOTIFY

    .elseif eax == WM_NOTIFY
        ;#####  This is where the "magic" happens
        mov     eax, lParam
        mov     eax, (NMHDR ptr [eax]).code
        .if eax == TCN_SELCHANGE
            mov     ebx, WhichTabChosen
            mov     eax, [TabHandles + ebx * 4]
            invoke  ShowWindow, eax, SW_HIDE
            invoke  SendMessage, hTab, TCM_GETCURSEL, 0, 0
            mov     WhichTabChosen, eax
            mov     ebx, [TabHandles + eax * 4]
            invoke  ShowWindow, ebx, SW_SHOW
        .endif



We will only handle the TCH_SELCHANGE message in this example, but WM_NOTIFY is where you can prevent a tab from changing, check which tab the user is leaving etc.
TabHandles is a structure to hold our tab handles to make it easier to change...

line 7 - 8 we get the handle from our struct of the tab that HAD the selction and hide it

line 9 - 12 we get the index of the selected tab, get its handle from our struct and show it.

Attached File(s)



Is This A Good Question/Topic? 1
  • +

Page 1 of 1