Hey guys. I've been working on this project of mine lately yet I have encountered an issue that neither my friends nor myself could solve. I am trying to make a little (nothing too fancy) wrapper for a few controls that I need. My current problem is that when I create and use my TabControl class, it doesn't show the control on the main window. The tab just won't show up, which is quite annoying. I've tried using my error logger class but everything succeeds everywhere so I must have missed something important elsewhere :
///////////////////////////////////////////////////////////
// //
// Interface module //
// //
// The interface module regroups many classes that //
// represent the different controls used in the //
// application.
// //
///////////////////////////////////////////////////////////
class IControl : public IMMObject
{
protected:
IControl();
virtual ~IControl() { }
public:
virtual void OnResize() = 0;
virtual void OnEnable() = 0;
virtual void OnDisable() = 0;
public:
static HWND hwnd_top_parent;
bool enabled;
HWND hwnd_self;
int control_id;
};
class TabControl : public IControl
{
public:
TabControl() { };
void OnResize();
void OnEnable();
void OnDisable();
void Create(HWND, const char*, int, RECT);
void Create(HWND, const char*, int, int, int, int, int);
void AddTab(LPTSTR, int);
protected:
struct Parent
{
Parent() : old_wnd_proc(0) { }
std::list<SmartPtr<TabControl> > tab_list;
WNDPROC old_wnd_proc;
};
static LRESULT CALLBACK SubWndProc(HWND, UINT, WPARAM, LPARAM);
static std::map<HWND, Parent> tab_list;
RECT display_area;
bool active;
};
///////////////////////////////////////////////////////////
// //
// Interface module //
// //
///////////////////////////////////////////////////////////
// static variable
HWND IControl::hwnd_top_parent;
IControl::IControl() : enabled(true)
{
INITCOMMONCONTROLSEX icc = { 0 };
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_TAB_CLASSES;
bool err = InitCommonControlsEx(&icc);
ErrorLogger.WriteMessage(__FILE__,
__LINE__,
TL_FATAL,
err == true,
"Initializing common controls");
}
// static variable
std::map<HWND, TabControl::Parent> TabControl::tab_list;
void TabControl::OnResize()
{
if(active && enabled) SendMessage(hwnd_self,
TCM_ADJUSTRECT,
(WPARAM)true,
(LPARAM)&display_area);
}
void TabControl::OnEnable()
{
// Provided for compatibility with IControl
}
void TabControl::OnDisable()
{
// Provided for compatibility with IControl
}
void TabControl::Create(HWND hwnd, const char* caption, int id, RECT rc)
{
Parent* p = &tab_list[hwnd];
hwnd_self = CreateWindowEx(0,
WC_TABCONTROL,
caption,
WS_CHILD | WS_VISIBLE,
rc.left,
rc.top,
rc.right - rc.left,
rc.bottom - rc.top,
hwnd,
NULL,
GetModuleHandle(0),
NULL);
ErrorLogger.WriteMessage(__FILE__,
__LINE__,
TL_WARNING,
hwnd_self != INVALID_HANDLE_VALUE,
"TabControl handle creation");
p->tab_list.push_back(this);
if(p->old_wnd_proc == NULL)
{
p->old_wnd_proc = (WNDPROC)SetWindowLongPtr(hwnd_self,
GWLP_WNDPROC,
(LONG_PTR)SubWndProc);
ErrorLogger.WriteMessage(__FILE__,
__LINE__,
TL_WARNING,
p != NULL,
"TabControl sub-classing operation");
}
display_area = rc;
}
void TabControl::Create(HWND hwnd, const char* caption,
int id, int x, int y, int x2, int y2)
{
RECT rc = { x, y, x2, y2 };
Create(hwnd, caption, id, rc);
}
void TabControl::AddTab(LPTSTR txt, int id)
{
TCITEM tci = { 0 };
tci.mask = TCIF_TEXT;
tci.pszText = txt;
int err = TabCtrl_InsertItem(hwnd_self, id, &tci);
ErrorLogger.WriteMessage(__FILE__,
__LINE__,
TL_WARNING,
err != -1,
"TabControl adding a tab");
}
LRESULT CALLBACK TabControl::SubWndProc(HWND hwnd, UINT msg, WPARAM wprm, LPARAM lprm)
{
Parent* p = &tab_list[hwnd];
std::list<SmartPtr<TabControl> >::iterator iter;
for(iter = p->tab_list.begin(); iter != p->tab_list.end(); iter++)
{
switch(msg)
{
case WM_DESTROY:
MessageBox(0,0,0,0);
break;
default:
return p->old_wnd_proc(hwnd, msg, wprm, lprm);
}
}
return 0;
}
Quote:[core.cpp:326] -> Initializing common controls succeeded
[main.cpp:54] -> Creating first tab succeeded
[core.cpp:372] -> TabControl handle creation succeeded
[core.cpp:385] -> TabControl sub-classing operation succeeded
[core.cpp:408] -> TabControl adding a tab succeeded
Do you guys have any idea ?
I want to answer it explicitly before anyone asks the odd and obvious question "did you call InitCommonControlsEx()", yes. It may not appear to you guys very easily since it's not that obvious but I placed it in IControl's empty constructor.
Many thanks.