Code:
#ifndef WINDOW_CLASSES_H
#define WINDOW_CLASSES_H
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
LRESULT CALLBACK Default (HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
return 0;
}
HWND defaultWnd;
HINSTANCE instance;
WNDCLASSEX wc;
int InitWindowClasses (HINSTANCE progInstance)
{
instance = progInstance;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = Default;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = progInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+15);
wc.lpszMenuName = NULL;
wc.lpszClassName = "WindowClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
return 0;
}
class WINDOW
{
private:
HWND *controls;
HWND winCreated;
int index, count;
bool firstControl;
public:
int MakeWindow (const char *WinTitle, int xPos, int yPos, int xSize, int ySize, WNDPROC wndproc)
{
wc.lpfnWndProc = wndproc;
if (!RegisterClassEx (&wc))
return -1;
winCreated = CreateWindow (
"WindowClass",
WinTitle,
WS_OVERLAPPEDWINDOW,
xPos,
yPos,
xSize,
ySize,
NULL,
NULL,
instance,
NULL);
index = 0;
count = 1;
controls = (HWND *) malloc (1);
firstControl = true;
UnregisterClass ("WindowClass", instance);
return 0;
}
int AddButton (const char *ButtonText, int xPos, int yPos, int xSize, int ySize, int id)
{
HWND button;
HWND *temp;
button = CreateWindow (
"BUTTON",
ButtonText,
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
xPos,
yPos,
xSize,
ySize,
winCreated,
(HMENU) id,
instance,
NULL);
controls[index] = button;
++index;
++count;
temp = (HWND *) realloc (controls, count * sizeof (HWND));
if (temp == NULL)
{
return -1;
}
controls = temp;
return 0;
}
int AddTextBox (const char *starttext, int xPos, int yPos, int xSize, int ySize, int id)
{
HWND textbox;
HWND *temp;
textbox = CreateWindow (
"EDIT",
starttext,
WS_CHILD | WS_VISIBLE | WS_BORDER |ES_AUTOHSCROLL | WS_GROUP | WS_TABSTOP,
xPos,
yPos,
xSize,
ySize,
winCreated,
(HMENU) id,
instance,
NULL);
controls[index] = textbox;
++index;
++count;
temp = (HWND *) realloc (controls, count * sizeof (HWND));
if (temp == NULL)
{
return -1;
}
controls = temp;
return 0;
}
int AddComboBox (const char *data[], int arrayItems, int xPos, int yPos, int xSize, int ySize, int id)
{
HWND combobox;
HWND *temp;
combobox = CreateWindow (
"COMBOBOX",
"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_HASSTRINGS | CBS_DROPDOWNLIST | WS_TABSTOP,
xPos,
yPos,
xSize,
ySize,
winCreated,
(HMENU) id,
instance,
NULL);
for (int added = 0; added < arrayItems; added++)
{
SendMessage (combobox, CB_ADDSTRING, 0, (LPARAM) data[added]);
}
controls[index] = combobox;
++index;
++count;
temp = (HWND *) realloc (controls, count * sizeof (HWND));
if (temp == NULL)
{
return -1;
}
controls = temp;
return 0;
}
int AddStaticText (const char *text, int xPos, int yPos, int xSize, int ySize, int id)
{
HWND statictext;
HWND *temp;
statictext = CreateWindow (
"STATIC",
text,
WS_CHILD | WS_VISIBLE,
xPos,
yPos,
xSize,
ySize,
winCreated,
(HMENU) id,
instance,
NULL);
controls[index] = statictext;
++index;
++count;
temp = (HWND *) realloc (controls, count * sizeof (HWND));
if (temp == NULL)
{
return -1;
}
controls = temp;
return 0;
}
void visible (int show)
{
ShowWindow (winCreated, show);
UpdateWindow (winCreated);
}
HWND GetHandle (void)
{
return winCreated;
}
int destroy ()
{
while (index >= 0)
{
DestroyWindow (controls[index]);
--index;
}
DestroyWindow (winCreated);
}
};
#endif
that is the class so far, and believe it or not, it actually works. Now, here is what i want to add to it. I want to make it so that in the main loop you can do something like:
Code:
#define BUT_1 0x100
#define BUT_2 0x200
int main ()
{
Window Win1;
Win1.MakeWindow ("Test", 10, 10, 10, 10);
Win1.AddButton ("Hi", 5, 5, 5, 5, BUT_1);
Win1.AddButton ("Exit", 10, 10, 5, 5, BUT_2);
while (GetMessage (&msg, NULL, 0, 0))
{
if (Win1.ButtonPressed (BUT_1))
MessageBox (NULL, "Hi", "All", MB_OK;
else if (Win1.ButtonPressed (BUT_2))
PostQuitMessage (0);
}
}
I have too many thoughts chasing in my head to think clearly on this one. if you can steer me in the right direction, i would be very appreciative. i know there are open source libraries for this sort of thing, but i would really like to do it myself and i know that my code is very inefficient at this point, but i need a guiding hand. I figured on doing something like making variables within the class that track the events, but i just cant work out how it's all going to work for some reason. i hit a mental block here