Archived

This topic is now archived and is closed to further replies.

bilsa

Tabs - pure win32 api ?

Recommended Posts

Hi! I have been looking on resources all over the web for any tutorials on creating TABS with pure win 32 api. I don''t want to use the MFC or any other library... because I would like to learn how to create tabs from the basics. I would really appreciate if anyone could point out any good resources on the web or if you could explain how to create tabs. I already have knowledge about creating buttons, scrollbars, checkbuttons, Dialog boxes and basic windows stuff like that. Thank you!

Share this post


Link to post
Share on other sites
I don''t really know the answer. But my suggestions.

Google and MSDN
http://www.google.com
http://msdn.microsoft.com

I am sure someone else would answer, but part of programming is actually finding stuff out on your own. The better you can do that, the better programmer you will be in the long run.

*note i''m not saying not to ask questions. But be sure to search really hard before you do*

BTW, Bilsa, that statement above wasn''t directed toward you, I know you said you searched, I am just trying to let people know.


Hope you have luck in finding the answer :D

Share this post


Link to post
Share on other sites
A tabbed window is just a window with a series of buttons on top of it. Whenever one of the buttons is pressed the button is highlighted and the content of the window changes.

Share this post


Link to post
Share on other sites
I like having a multi-GB library sourcecode of pure Win32 API.

Here is a good example:
This basically creates a dialogbox, creats a dialogbox within another dialogbox and when the message to change tabs is recieved, the original one is defocused, if the new one isn''t already created, it is created, and then focus set on it. You can use a different dialog callback for each "tab" state dialogbox view and that is just it.

Main Source

#include <windows.h>
#include <commctrl.h>

#include "resource.h"


#pragma comment(linker, "/opt:nowin98")


///////////////////////////////////////////////////////////////////////////////


BOOL CALLBACK general_DialogProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
return TRUE;
}
return FALSE;
}


///////////////////////////////////////////////////////////////////////////////



void main_SetPage(HWND hwnd,int page)
{
// 0. get the hwnd''s of the two pages!


HWND hGen = GetDlgItem(hwnd,IDD_PAGE_GENERAL);
HWND hOpt = GetDlgItem(hwnd,IDD_PAGE_OPTIONS);

// deal with hGet


if(page == 0)
{
if(!hGen)
{
hGen = CreateDialog((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDD_PAGE_GENERAL),hwnd,general_DialogProc);
SetWindowLong(hGen,GWL_ID,IDD_PAGE_GENERAL);
}

SetWindowPos(hGen,0,0,0,0,0,SWP_SHOWWINDOW|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE);
SetFocus(GetWindow(hGen,GW_CHILD));

}
else if(hGen)
{
SetWindowPos(hGen,0,0,0,0,0,SWP_HIDEWINDOW|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE);
}

if(page == 1)
{
if(!hOpt)
{
hOpt = CreateDialog((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDD_PAGE_OPTIONS),hwnd,general_DialogProc);
SetWindowLong(hOpt,GWL_ID,IDD_PAGE_OPTIONS);
}

SetWindowPos(hOpt,0,0,0,0,0,SWP_SHOWWINDOW|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE);
SetFocus(GetWindow(hOpt,GW_CHILD));
}
else if(hOpt)
{
SetWindowPos(hOpt,0,0,0,0,0,SWP_HIDEWINDOW|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE);
}
}




void main_SetupTabControl(HWND hwnd)
{
HWND hctl = GetDlgItem(hwnd,IDC_TAB);

TCITEM item;

item.mask = TCIF_TEXT;
item.pszText = "General";

TabCtrl_InsertItem(hctl,0,&item);

item.pszText = "Options";

TabCtrl_InsertItem(hctl,1,&item);
}



void main_HandleTabNotify(HWND hwnd,LPNMHDR pnmhdr)
{
HWND hctl = GetDlgItem(hwnd,IDC_TAB);

if(pnmhdr->code == TCN_SELCHANGE)
{
int p = TabCtrl_GetCurSel(hctl);

main_SetPage(hwnd,p);
}
}


BOOL CALLBACK main_DialogProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
main_SetupTabControl(hwnd);
main_SetPage(hwnd,0);
// main_Create

return TRUE;

case WM_NOTIFY:

if(wParam == IDC_TAB)
main_HandleTabNotify(hwnd,(LPNMHDR)lParam);
else
return FALSE;

return TRUE;

case WM_CLOSE:
EndDialog(hwnd,0);
return TRUE;
}
return FALSE;
}



int WINAPI WinMain(
HINSTANCE hInst,
HINSTANCE,
LPSTR pszCmdLine,
int nCmdShow)
{
INITCOMMONCONTROLSEX ice;
ice.dwSize = sizeof(ice);
ice.dwICC = ICC_TAB_CLASSES|ICC_UPDOWN_CLASS;
InitCommonControlsEx(&ice);
return DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_MAIN),NULL,main_DialogProc,0);
}


EXTERN_C int WINAPI WinMainCRTStartup()
{
return WinMain(GetModuleHandle(NULL),NULL,GetCommandLine(),SW_SHOWDEFAULT);
}


Resource IDs

//{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

// Used by apitabtest.rc

//

#define IDD_MAIN 101
#define IDD_PAGE_GENERAL 102
#define IDD_PAGE_OPTIONS 103
#define IDC_TAB 1000
#define IDC_EDIT1 1001
#define IDC_EDIT2 1002
#define IDC_RADIO1 1003
#define IDC_RADIO2 1004
#define IDC_RADIO3 1005
#define IDC_RADIO4 1006

// Next default values for new objects

//

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 104
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1007
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif


Resources

//Microsoft Developer Studio generated resource script.

//

#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////

//

// Generated from the TEXTINCLUDE 2 resource.

//

#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////

#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////

// English (U.S.) resources


#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32


#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////

//

// TEXTINCLUDE

//


1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END

3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED



/////////////////////////////////////////////////////////////////////////////

//

// Dialog

//


IDD_MAIN DIALOG DISCARDABLE 0, 0, 308, 190
STYLE DS_CENTER | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU |
WS_THICKFRAME
CAPTION "My App"
FONT 8, "MS Sans Serif"
BEGIN
CONTROL "Tab1",IDC_TAB,"SysTabControl32",0x0,7,7,294,176
END

IDD_PAGE_GENERAL DIALOG DISCARDABLE 50, 50, 186, 94
STYLE DS_CONTROL | WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
EDITTEXT IDC_EDIT1,72,22,107,14,ES_AUTOHSCROLL
EDITTEXT IDC_EDIT2,72,59,107,14,ES_AUTOHSCROLL
LTEXT "Static",IDC_STATIC,7,25,19,8
LTEXT "Static",IDC_STATIC,7,62,19,8
END

IDD_PAGE_OPTIONS DIALOG DISCARDABLE 50, 50, 186, 95
STYLE DS_CONTROL | WS_CHILD
FONT 8, "MS Sans Serif"
BEGIN
CONTROL "Radio1",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON,7,15,39,
10
CONTROL "Radio2",IDC_RADIO2,"Button",BS_AUTORADIOBUTTON,7,34,39,
10
CONTROL "Radio3",IDC_RADIO3,"Button",BS_AUTORADIOBUTTON,7,52,39,
10
CONTROL "Radio4",IDC_RADIO4,"Button",BS_AUTORADIOBUTTON,7,66,39,
10
END


/////////////////////////////////////////////////////////////////////////////

//

// DESIGNINFO

//


#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO DISCARDABLE
BEGIN
IDD_MAIN, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 301
VERTGUIDE, 20
VERTGUIDE, 290
TOPMARGIN, 7
BOTTOMMARGIN, 183
HORZGUIDE, 30
HORZGUIDE, 173
END

IDD_PAGE_GENERAL, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 179
VERTGUIDE, 72
TOPMARGIN, 7
BOTTOMMARGIN, 87
HORZGUIDE, 29
HORZGUIDE, 66
END

IDD_PAGE_OPTIONS, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 179
TOPMARGIN, 7
BOTTOMMARGIN, 88
END
END
#endif // APSTUDIO_INVOKED


#endif // English (U.S.) resources

/////////////////////////////////////////////////////////////////////////////




#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////

//

// Generated from the TEXTINCLUDE 3 resource.

//



/////////////////////////////////////////////////////////////////////////////

#endif // not APSTUDIO_INVOKED




Hope that helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
uhmm, what exactly do you mean by "pure win32 api" and "don''t want to use any other library"?

if the common controls library falls under your definition of "pure win32 api" then the standard way of implementing tabbed dialogs is via property sheets.

Share this post


Link to post
Share on other sites
Well, I if you are talking to the poster then ignore this, but to me... pure Win32 API means not tainted with MFC, unless the commoncontrols external files are written in MFC. In that case as long as I don''t have to see it, I don''t care about it.

Share this post


Link to post
Share on other sites
Yeh, thats how I would say "pure win32 api" is to me... something that is not concerned with MFC or any other downloadable library.

Anyway, I have also been thinking of the way Wildfire was speaking about... that there are multiple windows on top of each other and when I press one of the buttons, depending on which, a corresponding window is activated and show, while the rest are hidden?

So... could it be like that.? Because to me it feels somewhat like "cheating". I mean... I wouldn''t want to "cheat" just because I don''t know how to do it... I would rather learn doing it the right way

Thx!

Share this post


Link to post
Share on other sites
For straight up win32 tabs, you set up a tab control (the previous post has a link to the setup on MSDN). For all the content in your tab, you will need to set up child windows. You will need to set up a function to switch between tabs (erase all current child windows and generate the appropriate ones) that can be called when you actually switch tabs.

That is the short explaination. Post here if you have any further questions about this setup.

Share this post


Link to post
Share on other sites
quote:

Anyway, I have also been thinking of the way Wildfire was speaking about... that there are multiple windows on top of each other and when I press one of the buttons, depending on which, a corresponding window is activated and show, while the rest are hidden?

So... could it be like that.? Because to me it feels somewhat like "cheating". I mean... I wouldn''t want to "cheat" just because I don''t know how to do it... I would rather learn doing it the right way


But that''s how it''s done ''under the hood''. When you press a tab button, the corresponding child window is set to visible, all others are invisible. And of course the button stays highlighted. You could do the same by creating/destroying child-windows constantly, but I don''t think that''d be wise regarding performance/memory fragmentation.

Share this post


Link to post
Share on other sites
Thx guys, I''ll be checking out the msdn web site for this.

Now... I actually did search for "tabs" on my msdn CD, but none of this was found on it.

Thx again!

Share this post


Link to post
Share on other sites