Code:
#define TASKBAR_RESTORE 0x2000
using namespace std;
class SimpleWindowClass : protected WNDCLASSEX
{
public:
SimpleWindowClass (const TCHAR * className);
virtual bool Register ();
virtual const TCHAR * className () const { return lpszClassName; }
protected:
};
class AbstractWindow
{
protected:
HWND _hwnd;
LPCSTR _className;
list<HWND> children;
public:
//default constructor
AbstractWindow () { }
//member functions
static LRESULT CALLBACK msgRouter (HWND, UINT, WPARAM, LPARAM);
virtual bool Create (char *, int, int, int, int);
virtual bool OnCommand (int ctrlId, int notifyCode) { return false; }
virtual bool OnSysCommand (int ctrlId, int notifyCode) { return false; }
virtual bool OnDestroy (void) { ::PostQuitMessage (0); return false; }
virtual bool OnClose (void) { DestroyWindow (_hwnd); return false; }
virtual bool OnNCCreate (void) { return false; }
virtual bool OnTaskBar (int notifyCode) { return false; }
bool AddButton (char *, int, int, int, int, int);
bool AddTextBox (char *, int, int, int, int, int);
bool AddComboBox (char**, int, int, int, int, int);
bool AddStaticText (char *, int, int, int, int, int);
};
class SimpleWindow : public AbstractWindow
{
private:
NOTIFYICONDATA icon_dat;
public:
SimpleWindow (const TCHAR *className);
void Show ();
void Hide ();
virtual bool OnCommand (int ctrlId, int notifyCode);
virtual bool OnSysCommand (int ctrlId, int notifyCode);
virtual bool OnTaskBar (int notifyCode);
};
bool AbstractWindow::Create (char *name, int x, int y, int width, int height)
{
_hwnd = CreateWindowEx (0, _className, name, WS_OVERLAPPEDWINDOW, x, y, width, height,
NULL, NULL, GetModuleHandle (NULL), this);
if (!_hwnd)
return false;
return true;
}
LRESULT CALLBACK AbstractWindow::msgRouter(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
AbstractWindow *wnd = 0;
if (msg == WM_NCCREATE)
{
wnd = (AbstractWindow *) ((LPCREATESTRUCT) lParam)->lpCreateParams;
::SetWindowLong (hwnd, GWL_USERDATA, long (wnd));
wnd->OnNCCreate ();
}
wnd = (AbstractWindow *) (::GetWindowLong (hwnd, GWL_USERDATA));
if (wnd)
{
switch (msg)
{
case WM_COMMAND:
MessageBox (NULL, "Command action", "Hey!", MB_OK);
return wnd->OnCommand (LOWORD (wParam), HIWORD (wParam));
case WM_SYSCOMMAND:
MessageBox (NULL, "SysCommand action", "Hey!", MB_OK);
return wnd->OnSysCommand (LOWORD (wParam), HIWORD (wParam));
case WM_CLOSE:
MessageBox (NULL, "Close action", "Hey!", MB_OK);
return wnd->OnClose ();
case WM_DESTROY:
MessageBox (NULL, "Destroy action", "Hey!", MB_OK);
return wnd->OnDestroy ();
case TASKBAR_RESTORE:
MessageBox (NULL, "TaskBar action", "Hey!", MB_OK);
return wnd->OnTaskBar (LOWORD (lParam));
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
}
else
return DefWindowProc (hwnd, msg, wParam, lParam);
}
bool AbstractWindow::AddButton (char *caption, int xPos, int yPos, int width, int height, int id)
{
HWND child = CreateWindowEx (0, "BUTTON", caption, WS_CHILD | WS_VISIBLE, xPos, yPos,
width, height, _hwnd, (HMENU) id, GetModuleHandle (NULL), NULL);
if (child)
{
children.push_front (child);
return true;
}
else
{
DestroyWindow (child);
return false;
}
}
bool AbstractWindow::AddTextBox (char *initial, int xPos, int yPos, int width, int height, int id)
{
HWND child = CreateWindowEx (0, "EDIT", initial, WS_CHILD | WS_VISIBLE, xPos, yPos,
width, height, _hwnd, (HMENU) id, GetModuleHandle (NULL), NULL);
if (child)
{
children.push_front (child);
return true;
}
else
{
DestroyWindow (child);
return false;
}
}
SimpleWindowClass::SimpleWindowClass (const TCHAR *className)
{
hInstance = GetModuleHandle (NULL);
lpfnWndProc = AbstractWindow::msgRouter;
lpszClassName = className;
lpszMenuName = 0;
cbSize = sizeof (WNDCLASSEX);
cbClsExtra = 0;
cbWndExtra = 0;
style = 0;
hIcon = ::LoadIcon (NULL, IDI_APPLICATION);
hIconSm = ::LoadIcon (NULL, IDI_APPLICATION);
hCursor = ::LoadCursor (NULL, IDC_ARROW);
hbrBackground = (HBRUSH)(COLOR_BACKGROUND+15);
}
bool SimpleWindowClass::Register (void)
{
if (::RegisterClassEx (this))
return true;
else
return false;
}
SimpleWindow::SimpleWindow (const TCHAR *className) : AbstractWindow ()
{
_className = className;
icon_dat.cbSize = sizeof (NOTIFYICONDATA);
icon_dat.hWnd = _hwnd;
icon_dat.uID = 10;
icon_dat.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
icon_dat.uCallbackMessage = TASKBAR_RESTORE;
icon_dat.hIcon = LoadIcon (NULL, IDI_APPLICATION);
strcpy (icon_dat.szTip, "App");
}
void SimpleWindow::Show (void)
{
ShowWindow (_hwnd, SW_SHOW);
UpdateWindow (_hwnd);
}
void SimpleWindow::Hide (void)
{
ShowWindow (_hwnd, SW_HIDE);
UpdateWindow (_hwnd);
}
bool SimpleWindow::OnCommand (int ctrlId, int notifyCode)
{
switch (notifyCode)
{
case BN_CLICKED:
if (ctrlId == 0)
MessageBox (NULL, "You pressed OK!", "Notification", MB_OK);
else if (ctrlId == 1)
DestroyWindow (_hwnd);
break;
default:
break;
}
return true;
}
bool SimpleWindow::OnTaskBar (int notifyCode)
{
MessageBox (NULL, "You did something in the taskbar", "Hey!", MB_OK);
switch (notifyCode)
{
case WM_LBUTTONDOWN:
Shell_NotifyIcon (NIM_DELETE, &icon_dat);
this->Show ();
break;
}
return false;
}
bool SimpleWindow::OnSysCommand (int ctrlId, int notifyCode)
{
switch (notifyCode)
{
case SC_MINIMIZE:
Shell_NotifyIcon (NIM_ADD, &icon_dat);
this->Hide ();
break;
}
return false;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
SimpleWindowClass wndClass ("WinClass");
wndClass.Register();
SimpleWindow myWin ("WinClass");
myWin.Create ("Hello, world!", 10, 10, 300, 300);
myWin.AddButton ("Ok", 10, 10, 50, 30, 0);
myWin.AddButton ("Exit", 60, 10, 50, 30, 1);
myWin.Show ();
MSG msg;
int status;
while ((status = ::GetMessage (&msg, 0, 0, 0)) != 0)
{
if (status == -1)
break;
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
Sorry its alot of code. Can anyone tell me why my code for this is wrong? Any other errors feel free to let me know.